Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #140 #141

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 74 additions & 67 deletions better-frontend/src/pages/QuizPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ function QuizPreview() {
return <SoloGame quiz={quiz} />;
}

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 (
<>
<QuizEditor
Expand All @@ -141,72 +153,65 @@ function QuizPreview() {
Start
</Button>
)}
{(quiz.owner === undefined ||
quiz.owner === authInfo?.username) && (
<>
<Menu>
<MenuButton as={Button} colorScheme="purple">
Actions
</MenuButton>
<MenuList>
{quiz.questions.length > 0 && (
<MenuItem
as={Button}
onClick={async () => {
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"}
</MenuItem>
)}
<MenuItem
as={Button}
textAlign="center"
onClick={() => setQuizForEdit(quiz)}
>
Edit
</MenuItem>
<MenuItem
as={Button}
colorScheme="red"
onClick={async () => {
await deleteQuiz(id);
navigate("/");
}}
>
Delete
</MenuItem>
</MenuList>
</Menu>
</>
)}
<Menu>
<MenuButton as={Button} colorScheme="purple">
Actions
</MenuButton>
<MenuList>
{quiz.questions.length > 0 && (
<MenuItem
as={Button}
onClick={async () => {
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"}
</MenuItem>
)}
<MenuItem
as={Button}
textAlign="center"
onClick={() => withValidUser(() => setQuizForEdit(quiz))}
>
Edit
</MenuItem>
<MenuItem
as={Button}
colorScheme="red"
onClick={() =>
withValidUser(async () => {
await deleteQuiz(id);
navigate("/");
})
}
>
Delete
</MenuItem>
</MenuList>
</Menu>
<IconButton
variant="outline"
aria-label="Discussion"
Expand All @@ -219,7 +224,9 @@ function QuizPreview() {
<Drawer isOpen={isOpen} placement="right" onClose={onClose} size="md">
<DrawerOverlay />
<DrawerContent>
<DrawerHeader fontSize="2xl" fontWeight="bold">Quiz discussion</DrawerHeader>
<DrawerHeader fontSize="2xl" fontWeight="bold">
Quiz discussion
</DrawerHeader>
<DrawerCloseButton />
<DrawerBody display="flex" alignItems="stretch">
<QuizDiscussionBlock id={quiz.id} />
Expand Down
Loading