Skip to content

Commit

Permalink
feat: update quiz and refetch queries
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Mar 4, 2024
1 parent 23e3cd0 commit 4396ff0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/__generated__/gql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/__generated__/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 37 additions & 18 deletions src/components/Quiz/QuizForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Difficulty, QuestionType, QuizByIdQuery, Roles } from "@generated/graph
import { useUserStore } from "@state/userStore.ts";
import { CREATE_QUIZ } from "@utils/queries/CreateQuiz.ts";
import { GET_QUIZ_BY_ID } from "@utils/queries/QuizById.ts";
import { UPDATE_QUIZ } from "@utils/queries/UpdateQuiz.ts";

const getDefaultValues = (quiz?: QuizByIdQuery["quizList"][0]) => ({
title: quiz?.title || "",
Expand Down Expand Up @@ -61,28 +62,45 @@ export function QuizForm() {
}
}, [quizId, quiz]);

const [createQuizMutation, { loading: isLoadingQuizCreation, error: mutationError }] = useMutation(CREATE_QUIZ, {
const [createQuizMutation, { loading: isLoadingQuizCreation, error: mutationCreateError }] = useMutation(
CREATE_QUIZ,
{
onCompleted: async () => {
toast.success(
role === Roles.Admin
? "Quiz created successfully!"
: "Quiz created successfully. An admin will review it promptly!"
);
navigate("/");
reset();
},
}
);

const [updateQuizMutation, { loading: isLoadingQuizUpdate, error: mutationUpdateError }] = useMutation(UPDATE_QUIZ, {
onCompleted: async () => {
toast.success(
role === Roles.Admin
? "Quiz created successfully!"
: "Quiz created successfully. An admin will review it promptly!"
);
navigate("/");
reset();
toast.success("Quiz updated successfully!");
},
refetchQueries: [GET_QUIZ_BY_ID],
});

const onSubmit: SubmitHandler<z.infer<typeof quizFormSchema>> = async (values, event) => {
event?.preventDefault();
try {
await createQuizMutation({
variables: {
quiz: {
...values,
if (quizId) {
await updateQuizMutation({
variables: {
quizId,
quiz: values,
},
});
} else {
await createQuizMutation({
variables: {
quiz: values,
},
},
});
});
}
} catch (e) {
console.log("Something went wrong", e);
}
Expand Down Expand Up @@ -157,9 +175,10 @@ export function QuizForm() {
/>
</div>

{mutationError?.message && (
{(mutationCreateError?.message || mutationUpdateError?.message) && (
<Typography variant="small" color="red">
{mutationError.message}
{mutationCreateError?.message}
{mutationUpdateError?.message}
</Typography>
)}

Expand All @@ -174,8 +193,8 @@ export function QuizForm() {
<Button
type="submit"
fullWidth
disabled={isLoadingQuizCreation || !isDirty || Object.keys(errors).length > 0}
loading={isLoadingQuizCreation}
disabled={isLoadingQuizCreation || isLoadingQuizUpdate || !isDirty || Object.keys(errors).length > 0}
loading={isLoadingQuizCreation || isLoadingQuizUpdate}
className="mt-4"
>
Create Quiz
Expand Down
2 changes: 2 additions & 0 deletions src/state/attemptStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {} from "@redux-devtools/extension";
import { QuizByIdQuery } from "@generated/graphql.ts";
import { useUserStore } from "@state/userStore.ts";
import { apolloClient } from "@utils/apolloSetup.ts";
import { GET_QUIZ_ATTEMPTS } from "@utils/queries/QuizAttempts.ts";
import { SUBMIT_ATTEMPT } from "@utils/queries/SubmitAttempt.ts";

type Questions = QuizByIdQuery["quizList"][0]["questions"];
Expand Down Expand Up @@ -54,6 +55,7 @@ export const useAttemptStore = create<AttemptState>()(
variables: {
attempt: { questions, startTime, endTime, quiz: quizId, user: useUserStore.getState().user.userId },
},
refetchQueries: [GET_QUIZ_ATTEMPTS],
});

if (data?.addAttempt?.id) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/queries/UpdateQuiz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { gql } from "@generated/gql.ts";

export const UPDATE_QUIZ = gql(/* GraphQL */ `
mutation UpdateQuiz($quizId: String!, $quiz: QuizAddInput!) {
updateQuiz(quizId: $quizId, quiz: $quiz) {
id
}
}
`);

0 comments on commit 4396ff0

Please sign in to comment.