-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/question-service/edit-question
- Loading branch information
Showing
7 changed files
with
170 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import QuestionCreate from "@/components/questions/question-create"; | ||
|
||
export default function QuestionCreatePage() { | ||
return <QuestionCreate />; | ||
} |
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,64 @@ | ||
"use client"; | ||
|
||
import { Question } from "@/lib/schemas/question-schema"; | ||
import QuestionForm from "@/components/questions/question-form"; | ||
import { useAuth } from "@/app/auth/auth-context"; | ||
import { useRouter } from "next/navigation"; | ||
import { useToast } from "@/components/hooks/use-toast"; | ||
|
||
export default function QuestionCreate() { | ||
const auth = useAuth(); | ||
const router = useRouter(); | ||
const { toast } = useToast(); | ||
|
||
const handleCreate = async (newQuestion: Question) => { | ||
try { | ||
const response = await fetch("http://localhost:8000/questions", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
title: newQuestion.title, | ||
description: newQuestion.description, | ||
category: newQuestion.category, | ||
complexity: newQuestion.complexity, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
if (response.status == 409) { | ||
throw new Error("A question with this title already exists."); | ||
} | ||
} | ||
|
||
toast({ | ||
title: "Success", | ||
description: "Question created successfully!", | ||
variant: "success", | ||
duration: 3000, | ||
}); | ||
|
||
router.push(`/app/questions/`); | ||
} catch (err) { | ||
toast({ | ||
title: "An error occured!", | ||
description: | ||
err instanceof Error ? err.message : "An unknown error occurred", | ||
variant: "destructive", | ||
duration: 5000, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="container mx-auto p-4"> | ||
<h1 className="text-2xl font-bold mb-4">Create a New Question</h1> | ||
<QuestionForm | ||
isAdmin={auth?.user?.isAdmin} | ||
handleSubmit={handleCreate} | ||
submitButtonText="Create Question" | ||
/> | ||
</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