|
| 1 | +import { Button } from "@freecodecamp/ui"; |
| 2 | +import { getAttemptsByExamId, getExams } from "../utils/fetch"; |
| 3 | +import { useNavigate } from "@tanstack/react-router"; |
| 4 | +import { ExamLandingRoute } from "../pages/exam-landing"; |
| 5 | +import { useQuery } from "@tanstack/react-query"; |
| 6 | +import { |
| 7 | + Box, |
| 8 | + Card, |
| 9 | + CardBody, |
| 10 | + CardFooter, |
| 11 | + Heading, |
| 12 | + Text, |
| 13 | + Badge, |
| 14 | + Alert, |
| 15 | + AlertIcon, |
| 16 | + AlertDescription, |
| 17 | + Flex, |
| 18 | + Spinner, |
| 19 | +} from "@chakra-ui/react"; |
| 20 | +import { WarningIcon } from "@chakra-ui/icons"; |
| 21 | + |
| 22 | +type Exams = Awaited<ReturnType<typeof getExams>>["data"]; |
| 23 | + |
| 24 | +interface ExamCardProps { |
| 25 | + exam: NonNullable<Exams>[number]; |
| 26 | +} |
| 27 | + |
| 28 | +interface ExamStatus { |
| 29 | + canTake: boolean; |
| 30 | + status: |
| 31 | + | "Available" |
| 32 | + | "InProgress" |
| 33 | + | "PendingModeration" |
| 34 | + | "RetakeLater" |
| 35 | + | "Expired"; |
| 36 | + message?: string; |
| 37 | + alertStatus?: "success" | "info" | "warning" | "error"; |
| 38 | +} |
| 39 | + |
| 40 | +export function ExamCard({ exam }: ExamCardProps) { |
| 41 | + const navigate = useNavigate(); |
| 42 | + |
| 43 | + const attemptsQuery = useQuery({ |
| 44 | + queryKey: ["exam-attempts", exam.id], |
| 45 | + queryFn: async () => { |
| 46 | + const { data, error } = await getAttemptsByExamId(exam.id); |
| 47 | + |
| 48 | + if (error) { |
| 49 | + // @ts-expect-error TODO: fix error return type upstream |
| 50 | + throw new Error(error.message); |
| 51 | + } |
| 52 | + |
| 53 | + return data; |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + function getExamStatus(): ExamStatus { |
| 58 | + if (attemptsQuery.isPending) { |
| 59 | + return { canTake: false, status: "Available" }; |
| 60 | + } |
| 61 | + |
| 62 | + if (!attemptsQuery.data || attemptsQuery.data.length === 0) { |
| 63 | + return { canTake: exam.canTake, status: "Available" }; |
| 64 | + } |
| 65 | + |
| 66 | + const latestAttempt = getLatestAttempt(attemptsQuery.data); |
| 67 | + |
| 68 | + switch (latestAttempt.status) { |
| 69 | + case "InProgress": |
| 70 | + return { |
| 71 | + canTake: exam.canTake, |
| 72 | + status: latestAttempt.status, |
| 73 | + message: "You have an in-progress attempt for this exam!", |
| 74 | + alertStatus: "warning", |
| 75 | + }; |
| 76 | + case "PendingModeration": |
| 77 | + return { |
| 78 | + canTake: exam.canTake, |
| 79 | + status: latestAttempt.status, |
| 80 | + message: "You have already completed this exam.", |
| 81 | + alertStatus: "info", |
| 82 | + }; |
| 83 | + case "Expired": |
| 84 | + return { |
| 85 | + canTake: exam.canTake, |
| 86 | + status: latestAttempt.status, |
| 87 | + }; |
| 88 | + default: |
| 89 | + const startTime = new Date(latestAttempt.startTime); |
| 90 | + const retakeAvailableAt = new Date( |
| 91 | + startTime.getTime() + exam.config.retakeTimeInS * 1000 |
| 92 | + ); |
| 93 | + const now = new Date(); |
| 94 | + |
| 95 | + if (now < retakeAvailableAt) { |
| 96 | + return { |
| 97 | + canTake: exam.canTake, |
| 98 | + status: "RetakeLater", |
| 99 | + message: `You can retake this exam on ${retakeAvailableAt.toLocaleString()}.`, |
| 100 | + alertStatus: "info", |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + return { canTake: exam.canTake, status: "Available" }; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + function getLatestAttempt( |
| 109 | + attempts: NonNullable<(typeof attemptsQuery)["data"]> |
| 110 | + ) { |
| 111 | + return attempts.reduce((latest, current) => { |
| 112 | + return new Date(current.startTime) > new Date(latest.startTime) |
| 113 | + ? current |
| 114 | + : latest; |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const examStatus = getExamStatus(); |
| 119 | + |
| 120 | + return ( |
| 121 | + <li style={{ listStyle: "none", marginBottom: "1rem" }}> |
| 122 | + <Card |
| 123 | + borderWidth={examStatus.status === "InProgress" ? "3px" : "1px"} |
| 124 | + borderColor={ |
| 125 | + examStatus.status === "InProgress" ? "orange.400" : "gray.200" |
| 126 | + } |
| 127 | + boxShadow={examStatus.status === "InProgress" ? "lg" : "sm"} |
| 128 | + _hover={{ boxShadow: "md" }} |
| 129 | + transition="all 0.2s" |
| 130 | + > |
| 131 | + <CardBody> |
| 132 | + <Flex justifyContent="space-between" alignItems="flex-start" mb={2}> |
| 133 | + <Box flex={1}> |
| 134 | + <Heading size="md" mb={2}> |
| 135 | + {exam.config.name} |
| 136 | + </Heading> |
| 137 | + <Flex alignItems="center" gap={2}> |
| 138 | + <Text color="gray.600" fontSize="sm" marginBottom={0}> |
| 139 | + Duration: |
| 140 | + </Text> |
| 141 | + <Badge colorScheme="blue" fontSize="sm"> |
| 142 | + {examTimeInHumanReadableFormat(exam.config.totalTimeInS)} |
| 143 | + </Badge> |
| 144 | + </Flex> |
| 145 | + </Box> |
| 146 | + {examStatus.status === "InProgress" && ( |
| 147 | + <WarningIcon color="orange.400" boxSize={6} /> |
| 148 | + )} |
| 149 | + </Flex> |
| 150 | + |
| 151 | + {attemptsQuery.isPending ? ( |
| 152 | + <Flex alignItems="center" gap={2} mt={3}> |
| 153 | + <Spinner size="sm" /> |
| 154 | + <Text fontSize="sm" color="gray.500"> |
| 155 | + Getting attempt status... |
| 156 | + </Text> |
| 157 | + </Flex> |
| 158 | + ) : ( |
| 159 | + attemptsQuery.isError && ( |
| 160 | + <Alert status="error" mt={3} borderRadius="md"> |
| 161 | + <AlertIcon /> |
| 162 | + <AlertDescription fontSize="sm"> |
| 163 | + {attemptsQuery.error.message} |
| 164 | + </AlertDescription> |
| 165 | + </Alert> |
| 166 | + ) |
| 167 | + )} |
| 168 | + |
| 169 | + {examStatus.message && ( |
| 170 | + <Alert status={examStatus.alertStatus} mt={3} borderRadius="md"> |
| 171 | + <AlertIcon /> |
| 172 | + <AlertDescription fontSize="sm"> |
| 173 | + {examStatus.message} |
| 174 | + </AlertDescription> |
| 175 | + </Alert> |
| 176 | + )} |
| 177 | + </CardBody> |
| 178 | + |
| 179 | + <CardFooter pt={0}> |
| 180 | + <Button |
| 181 | + disabled={!exam.canTake || attemptsQuery.isPending} |
| 182 | + onClick={() => { |
| 183 | + navigate({ |
| 184 | + to: ExamLandingRoute.to, |
| 185 | + params: { examId: exam.id }, |
| 186 | + search: { note: exam.config.note }, |
| 187 | + }); |
| 188 | + }} |
| 189 | + style={{ width: "100%" }} |
| 190 | + > |
| 191 | + {examStatus.status === "InProgress" |
| 192 | + ? "Continue Exam" |
| 193 | + : "Start Exam"} |
| 194 | + </Button> |
| 195 | + </CardFooter> |
| 196 | + </Card> |
| 197 | + </li> |
| 198 | + ); |
| 199 | +} |
| 200 | + |
| 201 | +// Converts seconds to Xh Ym format |
| 202 | +function examTimeInHumanReadableFormat(seconds: number) { |
| 203 | + const minutes = Math.floor(seconds / 60); |
| 204 | + const hours = Math.floor(minutes / 60); |
| 205 | + if (hours > 0) { |
| 206 | + return `${hours}h ${minutes % 60}m`; |
| 207 | + } |
| 208 | + |
| 209 | + return `${minutes}m`; |
| 210 | +} |
0 commit comments