diff --git a/src/__generated__/gql.ts b/src/__generated__/gql.ts index c3cc2e8..2deeb68 100644 --- a/src/__generated__/gql.ts +++ b/src/__generated__/gql.ts @@ -19,6 +19,7 @@ const documents = { "\n mutation CancelQuiz($quizId: String!) {\n cancelQuiz(quizId: $quizId)\n }\n": types.CancelQuizDocument, "\n query CountryQuizzes($country: String!) {\n quizzesByCountry(country: $country) {\n id\n title\n description\n difficulty\n timeLimit\n image\n tags\n questions {\n question\n type\n }\n creator {\n lastName\n }\n }\n }\n": types.CountryQuizzesDocument, "\n mutation CreateQuiz($quiz: QuizAddInput!) {\n createQuiz(quiz: $quiz) {\n id\n }\n }\n": types.CreateQuizDocument, + "\n mutation DeleteAttempt($attemptId: String!) {\n deleteAttempt(attemptId: $attemptId)\n }\n": types.DeleteAttemptDocument, "\n query QuizRating($quizId: String!) {\n quizRating(quizId: $quizId)\n }\n": types.QuizRatingDocument, "\n mutation LoginUser($input: LoginInput!) {\n loginUser(input: $input) {\n access_token\n }\n }\n": types.LoginUserDocument, "\n query LogoutQuery {\n logoutUser\n }\n": types.LogoutQueryDocument, @@ -70,6 +71,10 @@ export function gql(source: "\n query CountryQuizzes($country: String!) {\n * The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ export function gql(source: "\n mutation CreateQuiz($quiz: QuizAddInput!) {\n createQuiz(quiz: $quiz) {\n id\n }\n }\n"): (typeof documents)["\n mutation CreateQuiz($quiz: QuizAddInput!) {\n createQuiz(quiz: $quiz) {\n id\n }\n }\n"]; +/** + * The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. + */ +export function gql(source: "\n mutation DeleteAttempt($attemptId: String!) {\n deleteAttempt(attemptId: $attemptId)\n }\n"): (typeof documents)["\n mutation DeleteAttempt($attemptId: String!) {\n deleteAttempt(attemptId: $attemptId)\n }\n"]; /** * The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ diff --git a/src/__generated__/graphql.ts b/src/__generated__/graphql.ts index aaae060..6916478 100644 --- a/src/__generated__/graphql.ts +++ b/src/__generated__/graphql.ts @@ -415,6 +415,13 @@ export type CreateQuizMutationVariables = Exact<{ export type CreateQuizMutation = { __typename?: 'Mutation', createQuiz: { __typename?: 'QuizData', id: string } }; +export type DeleteAttemptMutationVariables = Exact<{ + attemptId: Scalars['String']['input']; +}>; + + +export type DeleteAttemptMutation = { __typename?: 'Mutation', deleteAttempt: boolean }; + export type QuizRatingQueryVariables = Exact<{ quizId: Scalars['String']['input']; }>; @@ -499,6 +506,7 @@ export const QuizAttemptDocument = {"kind":"Document","definitions":[{"kind":"Op export const CancelQuizDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CancelQuiz"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quizId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"cancelQuiz"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quizId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quizId"}}}]}]}}]} as unknown as DocumentNode; export const CountryQuizzesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"CountryQuizzes"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"country"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quizzesByCountry"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"country"},"value":{"kind":"Variable","name":{"kind":"Name","value":"country"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"difficulty"}},{"kind":"Field","name":{"kind":"Name","value":"timeLimit"}},{"kind":"Field","name":{"kind":"Name","value":"image"}},{"kind":"Field","name":{"kind":"Name","value":"tags"}},{"kind":"Field","name":{"kind":"Name","value":"questions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"question"}},{"kind":"Field","name":{"kind":"Name","value":"type"}}]}},{"kind":"Field","name":{"kind":"Name","value":"creator"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"lastName"}}]}}]}}]}}]} as unknown as DocumentNode; export const CreateQuizDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateQuiz"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quiz"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"QuizAddInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createQuiz"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quiz"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quiz"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode; +export const DeleteAttemptDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"DeleteAttempt"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"attemptId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"deleteAttempt"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"attemptId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"attemptId"}}}]}]}}]} as unknown as DocumentNode; export const QuizRatingDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"QuizRating"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quizId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quizRating"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quizId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quizId"}}}]}]}}]} as unknown as DocumentNode; export const LoginUserDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"LoginUser"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"LoginInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"loginUser"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"access_token"}}]}}]}}]} as unknown as DocumentNode; export const LogoutQueryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"LogoutQuery"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"logoutUser"}}]}}]} as unknown as DocumentNode; diff --git a/src/pages/QuizResult/index.tsx b/src/pages/QuizResult/index.tsx index 7e866b9..4b73ff9 100644 --- a/src/pages/QuizResult/index.tsx +++ b/src/pages/QuizResult/index.tsx @@ -1,9 +1,10 @@ import { useEffect } from "react"; -import { useLazyQuery, useQuery } from "@apollo/client"; +import { useLazyQuery, useMutation, useQuery } from "@apollo/client"; import { Breadcrumbs, Button, List, Spinner, Typography } from "@material-tailwind/react"; import { format } from "date-fns"; import { ChevronRight, Loader2 } from "lucide-react"; import { Link, useNavigate, useParams } from "react-router-dom"; +import { toast } from "react-toastify"; import { AttemptRow } from "@components/AttempRow/AttemptRow.tsx"; import { AttemptRating } from "@components/AttemptRating/AttemptRating.tsx"; import { ScoreChip } from "@components/ScoreChip/ScoreChip.tsx"; @@ -12,6 +13,7 @@ import { useUserStore } from "@state/userStore.ts"; import { COLOURS } from "@utils/constants.ts"; import { useCountryDetails } from "@utils/hooks/useCountryDetails.ts"; import { GET_ATTEMPT_BY_ID } from "@utils/queries/AttemptById.ts"; +import { DELETE_ATTEMPT } from "@utils/queries/DeleteAttempt.ts"; import { GET_QUIZ_ATTEMPTS } from "@utils/queries/QuizAttempts.ts"; const handleOptionColor = (correct: boolean, chosen: boolean, index: number) => { @@ -33,6 +35,13 @@ export function Component() { isAdmin, } = useUserStore(); + const [deleteAttempt] = useMutation(DELETE_ATTEMPT, { + onCompleted: async () => { + toast.success("Attempt deleted successfully!"); + navigate(-1); + }, + }); + const [fetchQuizAttempts, { data: quizAttempts, loading: loadingAllAttempts }] = useLazyQuery(GET_QUIZ_ATTEMPTS); useEffect(() => { @@ -173,6 +182,17 @@ export function Component() { + {isAdmin && ( + + )} ); diff --git a/src/utils/queries/DeleteAttempt.ts b/src/utils/queries/DeleteAttempt.ts new file mode 100644 index 0000000..d50491c --- /dev/null +++ b/src/utils/queries/DeleteAttempt.ts @@ -0,0 +1,7 @@ +import { gql } from "@generated/gql.ts"; + +export const DELETE_ATTEMPT = gql(/* GraphQL */ ` + mutation DeleteAttempt($attemptId: String!) { + deleteAttempt(attemptId: $attemptId) + } +`);