Skip to content

Commit

Permalink
Implement save changes button on admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
jq1836 committed Sep 26, 2024
1 parent b3a59ca commit 7b2a47c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 4 additions & 3 deletions frontend/components/questions/question-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ const QuestionForm: React.FC<QuestionFormProps> = ({ ...props }) => {
<CardFooter>
{props.isAdmin && (
<Button
onClick={() =>
question && props.handleSubmit && props.handleSubmit(question)
}
onClick={(e) => {
e.preventDefault();
question && props.handleSubmit && props.handleSubmit(question);
}}
>
Save Changes
</Button>
Expand Down
39 changes: 35 additions & 4 deletions frontend/components/questions/question-view-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import useSWR from "swr";
import QuestionForm from "@/components/questions/question-form";
import { useAuth } from "@/app/auth/auth-context";
import { useEffect, useState } from "react";
import { updateQuestion } from "@/lib/update-question";
import { useToast } from "@/components/hooks/use-toast";

const fetcher = async (url: string): Promise<Question> => {
const token = localStorage.getItem("jwtToken");
Expand Down Expand Up @@ -34,8 +36,9 @@ export default function QuestionViewEdit({
questionId: string;
}) {
const auth = useAuth();
const { toast } = useToast();

const { data } = useSWR(
const { data, mutate } = useSWR(
`http://localhost:8000/questions/${questionId}`,
fetcher
);
Expand All @@ -46,9 +49,37 @@ export default function QuestionViewEdit({
setQuestion(data);
}, [data]);

const handleEdit = (question: Question) => {
// Todo: Implement
question;
const handleEdit = async (question: Question) => {
const response = await updateQuestion(question);
if (!response.ok) {
toast({
title: "Unknown Error",
description: "An unexpected error has occurred",
});
return;
}
switch (response.status) {
case 200:
toast({
title: "Success",
description: "Question updated successfully!",
});
break;
case 404:
toast({
title: "Question not found",
description: "Question with specified ID not found",
});
return;
case 409:
toast({
title: "Duplicated title",
description: "The title you entered is already in use",
});
return;
}

mutate();
};

return (
Expand Down

0 comments on commit 7b2a47c

Please sign in to comment.