-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a confirmation modal when deleting a year (#689)
* Add a confirmation modal when deleting a year * fixed linting errors * addressed brandon's comments
- Loading branch information
Showing
3 changed files
with
122 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { DeleteIcon } from "@chakra-ui/icons"; | ||
import { | ||
Button, | ||
Flex, | ||
IconButton, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
useDisclosure, | ||
} from "@chakra-ui/react"; | ||
import { useRouter } from "next/router"; | ||
import { Dispatch, SetStateAction } from "react"; | ||
import { mutate } from "swr"; | ||
import { | ||
USE_STUDENT_WITH_PLANS_SWR_KEY, | ||
useStudentWithPlans, | ||
} from "../../hooks"; | ||
import { handleApiClientError, toast } from "../../utils"; | ||
import { GraduateToolTip } from "../GraduateTooltip"; | ||
|
||
interface DeleteYearModalProps { | ||
yearNum: number; | ||
removeYear: Dispatch<SetStateAction<number | undefined | null>>; | ||
} | ||
export const DeleteYearModal: React.FC<DeleteYearModalProps> = ({ | ||
yearNum, | ||
removeYear, | ||
}) => { | ||
const router = useRouter(); | ||
const { onOpen, onClose, isOpen } = useDisclosure(); | ||
const { student } = useStudentWithPlans(); | ||
|
||
if (!student) { | ||
return <></>; | ||
} | ||
|
||
const deleteYear = async () => { | ||
try { | ||
removeYear(yearNum); | ||
// refresh the cache, show success message, and close the modal | ||
mutate(USE_STUDENT_WITH_PLANS_SWR_KEY); | ||
toast.success("Year deleted successfully."); | ||
onClose(); | ||
} catch (error) { | ||
handleApiClientError(error as Error, router); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<GraduateToolTip label={`Delete Year ${yearNum}?`} fontSize="md"> | ||
<IconButton | ||
aria-label="Delete course" | ||
variant="ghost" | ||
color="white" | ||
icon={<DeleteIcon />} | ||
marginLeft="auto" | ||
marginRight="sm" | ||
_hover={{ bg: "white", color: "primary.red.main" }} | ||
_active={{ bg: "primary.blue.light.900" }} | ||
onClick={onOpen} | ||
/> | ||
</GraduateToolTip> | ||
<Modal isOpen={isOpen} onClose={onClose}> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader textAlign="center" color="primary.blue.dark.main"> | ||
Delete Plan | ||
</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
Woah, are you sure you want to delete Year {yearNum}? | ||
</ModalBody> | ||
<ModalFooter justifyContent="center"> | ||
<Flex columnGap="sm"> | ||
<Button | ||
variant="solidWhite" | ||
size="md" | ||
borderRadius="lg" | ||
onClick={onClose} | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
variant="solid" | ||
size="md" | ||
borderRadius="lg" | ||
type="submit" | ||
onClick={deleteYear} | ||
> | ||
Delete | ||
</Button> | ||
</Flex> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters