Skip to content

Commit

Permalink
feat: delete attempt as admin
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Mar 8, 2024
1 parent 913a6b7 commit c6f3cc5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
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.

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

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

22 changes: 21 additions & 1 deletion src/pages/QuizResult/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) => {
Expand All @@ -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(() => {
Expand Down Expand Up @@ -173,6 +182,17 @@ export function Component() {
</div>
</section>
</div>
{isAdmin && (
<Button
variant="outlined"
color="red"
className="mt-6"
size="sm"
onClick={() => deleteAttempt({ variables: { attemptId: attemptId || "" } })}
>
Delete attempt
</Button>
)}
</section>
</div>
);
Expand Down
7 changes: 7 additions & 0 deletions src/utils/queries/DeleteAttempt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { gql } from "@generated/gql.ts";

export const DELETE_ATTEMPT = gql(/* GraphQL */ `
mutation DeleteAttempt($attemptId: String!) {
deleteAttempt(attemptId: $attemptId)
}
`);

0 comments on commit c6f3cc5

Please sign in to comment.