From 4694f0627024ccd7bd852ca3ec274b477e037c09 Mon Sep 17 00:00:00 2001 From: Kanstantinas Piatrashka Date: Sun, 17 Dec 2023 19:31:16 +0200 Subject: [PATCH 1/2] fix #140 --- better-frontend/src/pages/QuizPreview.tsx | 82 ++++++++++++----------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/better-frontend/src/pages/QuizPreview.tsx b/better-frontend/src/pages/QuizPreview.tsx index 03c7c6d7..f9f942a9 100644 --- a/better-frontend/src/pages/QuizPreview.tsx +++ b/better-frontend/src/pages/QuizPreview.tsx @@ -20,7 +20,7 @@ import { DrawerContent, DrawerCloseButton, } from "@chakra-ui/react"; -import { ChatIcon } from "@chakra-ui/icons"; +import { ChatIcon, HamburgerIcon } from "@chakra-ui/icons"; import { QuestionResponseDto, QuizManipulationRequestDto, @@ -141,51 +141,51 @@ function QuizPreview() { Start )} + {quiz.questions.length > 0 && ( + + )} {(quiz.owner === undefined || quiz.owner === authInfo?.username) && ( <> - + } + > Actions - {quiz.questions.length > 0 && ( - { - if (generateButtonIsLoading) { - return; - } - - setGenerateButtonIsLoading(true); - - try { - const newCollection = - await createFromQuizFlashcardCollection(id); - navigate( - `/flashcard-collections/${newCollection.id}` - ); - } catch (error) { - toast({ - title: "Flashcard generation failed", - description: `Whoops! ${ - (error as Error).message - }`, - status: "error", - duration: 5000, - isClosable: true, - }); - } - - setGenerateButtonIsLoading(false); - }} - isDisabled={generateButtonIsLoading} - > - {generateButtonIsLoading - ? "Generating..." - : "Generate flashcards"} - - )} - Quiz discussion + + Quiz discussion + From f475859105c575eaf4e1ba5cfee8da78dc9d0156 Mon Sep 17 00:00:00 2001 From: Kanstantinas Piatrashka Date: Mon, 18 Dec 2023 13:11:59 +0200 Subject: [PATCH 2/2] implement suggested permission behavior --- better-frontend/src/pages/QuizPreview.tsx | 137 +++++++++++----------- 1 file changed, 71 insertions(+), 66 deletions(-) diff --git a/better-frontend/src/pages/QuizPreview.tsx b/better-frontend/src/pages/QuizPreview.tsx index f9f942a9..fa6c5c82 100644 --- a/better-frontend/src/pages/QuizPreview.tsx +++ b/better-frontend/src/pages/QuizPreview.tsx @@ -20,7 +20,7 @@ import { DrawerContent, DrawerCloseButton, } from "@chakra-ui/react"; -import { ChatIcon, HamburgerIcon } from "@chakra-ui/icons"; +import { ChatIcon } from "@chakra-ui/icons"; import { QuestionResponseDto, QuizManipulationRequestDto, @@ -123,6 +123,18 @@ function QuizPreview() { return ; } + function withValidUser(f: () => void) { + if (quiz.owner !== undefined && quiz.owner !== authInfo?.username) { + toast({ + title: "User is not authorized", + description: "You must be the owner of this quiz", + status: "error", + duration: 5000, + isClosable: true, + }); + } else f(); + } + return ( <> )} - {quiz.questions.length > 0 && ( - - )} - {(quiz.owner === undefined || - quiz.owner === authInfo?.username) && ( - <> - - } + + + Actions + + + {quiz.questions.length > 0 && ( + { + if (generateButtonIsLoading) { + return; + } + + setGenerateButtonIsLoading(true); + + try { + const newCollection = + await createFromQuizFlashcardCollection(id); + navigate(`/flashcard-collections/${newCollection.id}`); + } catch (error) { + toast({ + title: "Flashcard generation failed", + description: `Whoops! ${(error as Error).message}`, + status: "error", + duration: 5000, + isClosable: true, + }); + } + + setGenerateButtonIsLoading(false); + }} + isDisabled={generateButtonIsLoading} > - Actions - - - setQuizForEdit(quiz)} - > - Edit - - { - await deleteQuiz(id); - navigate("/"); - }} - > - Delete - - - - - )} + {generateButtonIsLoading + ? "Generating..." + : "Generate flashcards"} + + )} + withValidUser(() => setQuizForEdit(quiz))} + > + Edit + + + withValidUser(async () => { + await deleteQuiz(id); + navigate("/"); + }) + } + > + Delete + + +