-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from TeaByte/NewTestingWay
ADD Deno Kv
- Loading branch information
Showing
17 changed files
with
189 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import IconCircleCheckFilled from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/circle-check-filled.tsx"; | ||
import IconCircle from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/circle.tsx"; | ||
|
||
export default function ProgressCheck(props: {isDone : boolean}) { | ||
return props.isDone | ||
? <IconCircleCheckFilled aria-hidden="true" class="h-4 w-4" /> | ||
: <IconCircle aria-hidden="true" class="h-4 w-4" />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import Progress from "../islands/Progress.tsx"; | ||
import Icon from "./Icon.tsx"; | ||
import ProgressBar from "./ProgressBar.tsx"; | ||
|
||
export default function ProgressPageSplit() { | ||
return ( | ||
<> | ||
<div className="flex gap-2"> | ||
<h1 className="text-2xl font-bold">مرحباً بك في</h1> | ||
<Icon /> | ||
</div> | ||
<p className="text-lg"> | ||
وجهتك الأمثل لاكتساب مهارات جافاسكربت بسهولة وفعالية. رحلة تعليمية شيقة | ||
تمتد من الأساسيات إلى المستويات المتقدمة | ||
</p> | ||
<h2 className="text-xl font-bold">تقدمك في إنجاز الدروس:</h2> | ||
{/* TODO: Make this a component */} | ||
<Progress /> | ||
</> | ||
); | ||
} | ||
export default function ProgressPageSplit(props: { completed: number, total: number }) { | ||
return ( | ||
<> | ||
<div className="flex gap-2"> | ||
<h1 className="text-2xl font-bold">مرحباً بك في</h1> | ||
<Icon /> | ||
</div> | ||
<p className="text-lg"> | ||
وجهتك الأمثل لاكتساب مهارات جافاسكربت بسهولة وفعالية. رحلة تعليمية شيقة | ||
تمتد من الأساسيات إلى المستويات المتقدمة | ||
</p> | ||
<h2 className="text-xl font-bold">تقدمك في إنجاز الدروس:</h2> | ||
<div className="flex flex-col gap-2"> | ||
<h1 className="text-sm">لقد تعلمت {props.completed} من أصل {props.total} درس</h1> | ||
<ProgressBar progress={Math.floor((props.completed / props.total) * 100)} /> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { FreshContext } from "$fresh/server.ts"; | ||
import {getCookies, Cookie, setCookie} from "$std/http/mod.ts"; | ||
import { createStudent } from "../utils/KV.ts"; | ||
export async function handler( | ||
req: Request, | ||
ctx: FreshContext, | ||
) { | ||
const resp = await ctx.next(); | ||
const cookies = getCookies(req.headers); | ||
const sessionId = cookies["sessionId"] || ""; | ||
if (!sessionId) { | ||
try { | ||
const student = await createStudent(); | ||
const cookie: Cookie = { | ||
name: "sessionId", | ||
value: student.sessionId, | ||
} | ||
setCookie(resp.headers, cookie); | ||
} catch { | ||
return ctx.renderNotFound(); | ||
} | ||
} | ||
return resp; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { getCookies } from "$std/http/mod.ts"; | ||
import { addCompletedCourse } from "../../../utils/KV.ts"; | ||
interface FinshTest { | ||
courseslug: string; | ||
} | ||
export const handler: Handlers<FinshTest> = { | ||
async POST(req, _ctx) { | ||
try { | ||
const FinshTest = (await req.json() as FinshTest) | ||
const courseslug = FinshTest.courseslug | ||
const sessionId = getCookies(req.headers)?.sessionId; | ||
addCompletedCourse(sessionId, courseslug) | ||
} catch (error) { | ||
return new Response(error.message, { status: 500 }); | ||
} | ||
return new Response("ok"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { student, Result } from "./types.ts"; | ||
|
||
const kv = await Deno.openKv(); | ||
export const createStudent = async (): Promise<student> => { | ||
const student: student = { | ||
sessionId: crypto.randomUUID(), | ||
completedCourses: [], | ||
} | ||
const res = await kv.set(["student", student.sessionId], student) | ||
if (res.ok) { | ||
return student | ||
} else { | ||
throw new Error("Failed to create student") | ||
} | ||
} | ||
export const getStudent = async (sessionId: string): Promise<student> => { | ||
const student = await kv.get(["student", sessionId]) | ||
if (student) { | ||
return student.value as student | ||
} else { | ||
throw new Error("Failed to get student") | ||
} | ||
} | ||
export const updateStudent = async (sessionId: string, student: student): Promise<Result> => { | ||
const res = await kv.set(["student", sessionId], student) | ||
if (res.ok) { | ||
return res | ||
} else { | ||
throw new Error("Failed to update student") | ||
} | ||
} | ||
export const addCompletedCourse = async (sessionId: string, course: string) : Promise<Result> => { | ||
const student = await getStudent(sessionId) | ||
if (student.completedCourses.includes(course)) { | ||
throw new Error("Course already completed") | ||
} | ||
const completedCourses = student.completedCourses | ||
completedCourses.push(course) | ||
student.completedCourses = completedCourses | ||
try { | ||
const res = await updateStudent(sessionId, student) | ||
return res | ||
} catch { | ||
throw new Error("Failed to add completed course") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.