-
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 'feature/user-service/admin-edit-user' into feature/user…
…-service/admin-update-privilege
- Loading branch information
Showing
15 changed files
with
609 additions
and
309 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
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,68 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { ReactNode } from "react"; | ||
import { useAuth } from "@/app/auth/auth-context"; | ||
import { Button } from "@/components/ui/button"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
type AuthCheck = (user: { isAdmin: boolean } | undefined | null) => boolean; | ||
|
||
interface AuthPageWrapperProps extends React.HTMLProps<HTMLDivElement> { | ||
children: ReactNode; | ||
|
||
// User access rules | ||
authCheck?: AuthCheck; // Custom predicate which is true when user is to be granted access | ||
requireAdmin?: boolean; | ||
requireLoggedIn?: boolean; | ||
} | ||
|
||
const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({ | ||
children, | ||
...props | ||
}) => { | ||
const auth = useAuth(); | ||
const router = useRouter(); | ||
|
||
const authCheck = ( | ||
user: { isAdmin: boolean } | undefined | null | ||
): boolean => { | ||
if (props?.requireLoggedIn && !user) { | ||
return false; | ||
} | ||
if (props?.requireAdmin && !user?.isAdmin) { | ||
return false; | ||
} | ||
if (props?.authCheck) { | ||
return props.authCheck(user); | ||
} | ||
// Allow access if no user access rule is defined | ||
return true; | ||
}; | ||
|
||
return ( | ||
<div> | ||
{authCheck(auth?.user) ? ( | ||
children | ||
) : ( | ||
<div className="flex items-start justify-center h-2/6"> | ||
<div className="text-center mt-[20vh]"> | ||
<h1 className="text-4xl font-extrabold tracking-tight lg:text-5xl mb-6"> | ||
Uh Oh! You're not supposed to be here! | ||
</h1> | ||
<Button | ||
size="lg" | ||
onClick={() => { | ||
auth?.user ? router.push("/") : router.push("/auth/login"); | ||
}} | ||
> | ||
Return Home | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthPageWrapper; |
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 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,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> | ||
); | ||
} |
Oops, something went wrong.