|
| 1 | +import { isTheTourCompleted } from "@/lib/client-functions"; |
| 2 | +import React, { useEffect, useState } from "react"; |
| 3 | +import styles from "./CertificateButton.module.css"; |
| 4 | +import { LockIcon } from "@chakra-ui/icons"; |
| 5 | +import { |
| 6 | + Box, |
| 7 | + Button, |
| 8 | + Flex, |
| 9 | + Input, |
| 10 | + Tooltip, |
| 11 | + useDisclosure, |
| 12 | + useToast, |
| 13 | +} from "@chakra-ui/react"; |
| 14 | +import { |
| 15 | + Modal, |
| 16 | + ModalOverlay, |
| 17 | + ModalContent, |
| 18 | + ModalHeader, |
| 19 | + ModalFooter, |
| 20 | + ModalBody, |
| 21 | + ModalCloseButton, |
| 22 | +} from "@chakra-ui/react"; |
| 23 | +import { googleSheetAPIRoute } from "@/lib/contentVariables"; |
| 24 | +const submitCertificateReq = async (name: string, email: string) => { |
| 25 | + await fetch(googleSheetAPIRoute, { |
| 26 | + method: "POST", |
| 27 | + body: JSON.stringify({ |
| 28 | + name, |
| 29 | + email, |
| 30 | + certificateReq: true, |
| 31 | + }), |
| 32 | + }); |
| 33 | +}; |
| 34 | + |
| 35 | +export default function CertificateButton() { |
| 36 | + const { isOpen, onOpen, onClose } = useDisclosure(); |
| 37 | + const [isTheTourCompletedState, setIsTheTourCompletedState] = |
| 38 | + useState(isTheTourCompleted()); |
| 39 | + useEffect(() => { |
| 40 | + setIsTheTourCompletedState(isTheTourCompleted()); |
| 41 | + }, [isTheTourCompleted()]); |
| 42 | + const [name, setName] = useState(""); |
| 43 | + const [email, setEmail] = useState(""); |
| 44 | + const toast = useToast(); |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <Tooltip |
| 49 | + label={ |
| 50 | + isTheTourCompletedState |
| 51 | + ? "" |
| 52 | + : "Complete the tour to get your certificate" |
| 53 | + } |
| 54 | + aria-label="A tooltip" |
| 55 | + > |
| 56 | + <Button |
| 57 | + size={"sm"} |
| 58 | + variant={"default"} |
| 59 | + className={styles.certificateButton} |
| 60 | + onClick={isTheTourCompletedState ? onOpen : () => {}} |
| 61 | + > |
| 62 | + {isTheTourCompletedState ? "" : <LockIcon />} Get Your Certificate |
| 63 | + </Button> |
| 64 | + </Tooltip> |
| 65 | + <Modal isOpen={isOpen} onClose={onClose}> |
| 66 | + <ModalOverlay /> |
| 67 | + <ModalContent> |
| 68 | + <ModalHeader>Get Your Certificate</ModalHeader> |
| 69 | + <ModalCloseButton /> |
| 70 | + <ModalBody> |
| 71 | + <Box mb={4}>We will email you your certificate</Box> |
| 72 | + <Flex direction="column" gap={4}> |
| 73 | + <Input |
| 74 | + placeholder="Name" |
| 75 | + onChange={(e) => setName(e.target.value)} |
| 76 | + value={name} |
| 77 | + /> |
| 78 | + <Input |
| 79 | + placeholder="Email" |
| 80 | + onChange={(e) => setEmail(e.target.value)} |
| 81 | + value={email} |
| 82 | + /> |
| 83 | + </Flex> |
| 84 | + </ModalBody> |
| 85 | + |
| 86 | + <ModalFooter> |
| 87 | + <Button variant="ghost" onClick={onClose}> |
| 88 | + Close |
| 89 | + </Button> |
| 90 | + <Button |
| 91 | + variant="default" |
| 92 | + mr={3} |
| 93 | + onClick={() => { |
| 94 | + submitCertificateReq(name, email); |
| 95 | + onClose(); |
| 96 | + toast({ |
| 97 | + title: "Certificate Requested", |
| 98 | + description: "We will email you your certificate", |
| 99 | + status: "success", |
| 100 | + duration: 9000, |
| 101 | + isClosable: true, |
| 102 | + }); |
| 103 | + }} |
| 104 | + > |
| 105 | + Submit |
| 106 | + </Button> |
| 107 | + </ModalFooter> |
| 108 | + </ModalContent> |
| 109 | + </Modal> |
| 110 | + </> |
| 111 | + ); |
| 112 | +} |
0 commit comments