|
| 1 | +import React, { useContext } from "react" |
| 2 | +import { Box, Flex, ListItem, Stack, Text } from "@chakra-ui/react" |
| 3 | +import { useTranslation } from "gatsby-plugin-react-i18next" |
| 4 | + |
| 5 | +import Button from "../Button" |
| 6 | +import Translation from "../Translation" |
| 7 | +import Tag from "../Tag" |
| 8 | +import { GreenTickIcon } from "../icons/quiz" |
| 9 | + |
| 10 | +import { QuizzesHubContext } from "./context" |
| 11 | + |
| 12 | +import { trackCustomEvent } from "../../utils/matomo" |
| 13 | + |
| 14 | +import { QuizzesListItem } from "../../types" |
| 15 | + |
| 16 | +import allQuizzesData from "../../data/quizzes" |
| 17 | + |
| 18 | +const QuizItem: React.FC<QuizzesListItem> = (props) => { |
| 19 | + const { id, level, quizHandler, modalHandler } = props |
| 20 | + const { |
| 21 | + userStats: { completed }, |
| 22 | + } = useContext(QuizzesHubContext) |
| 23 | + const numberOfQuestions = allQuizzesData[id].questions.length |
| 24 | + const isCompleted = JSON.parse(completed)[id][0] |
| 25 | + |
| 26 | + const { t } = useTranslation() |
| 27 | + |
| 28 | + const handleStart = () => { |
| 29 | + quizHandler(id) |
| 30 | + modalHandler(true) |
| 31 | + |
| 32 | + trackCustomEvent({ |
| 33 | + eventCategory: "quiz_hub_events", |
| 34 | + eventAction: "quizzes click", |
| 35 | + eventName: `${id}`, |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <ListItem |
| 41 | + color={isCompleted ? "bodyMedium" : "text"} |
| 42 | + fontWeight="bold" |
| 43 | + px={{ base: 0, lg: 4 }} |
| 44 | + py={4} |
| 45 | + borderBottom="1px solid" |
| 46 | + borderColor="disabled" |
| 47 | + mb={0} |
| 48 | + sx={{ counterIncrement: "list-counter" }} |
| 49 | + > |
| 50 | + <Flex |
| 51 | + justifyContent="space-between" |
| 52 | + alignItems={{ base: "flex-start", lg: "center" }} |
| 53 | + direction={{ base: "column", lg: "row" }} |
| 54 | + > |
| 55 | + <Stack mb={{ base: 5, lg: 0 }}> |
| 56 | + <Flex gap={2} alignItems="center"> |
| 57 | + <Text |
| 58 | + color={isCompleted ? "bodyMedium" : "text"} |
| 59 | + fontWeight="bold" |
| 60 | + mb={0} |
| 61 | + _before={{ |
| 62 | + content: 'counter(list-counter) ". "', |
| 63 | + }} |
| 64 | + > |
| 65 | + <Translation id={allQuizzesData[id].title} /> |
| 66 | + </Text> |
| 67 | + |
| 68 | + {/* Show green tick if quizz was completed only */} |
| 69 | + <Box display={isCompleted ? "flex" : "none"}> |
| 70 | + <GreenTickIcon /> |
| 71 | + </Box> |
| 72 | + </Flex> |
| 73 | + |
| 74 | + {/* Labels */} |
| 75 | + <Flex gap={3}> |
| 76 | + {/* number of questions - label */} |
| 77 | + <Tag |
| 78 | + label={t(`${numberOfQuestions} ${t("questions")}`)} |
| 79 | + ml={{ lg: -2 }} |
| 80 | + /> |
| 81 | + |
| 82 | + {/* difficulty - label */} |
| 83 | + <Tag label={level.toUpperCase()} /> |
| 84 | + </Flex> |
| 85 | + </Stack> |
| 86 | + |
| 87 | + {/* Start Button */} |
| 88 | + <Box w={{ base: "full", lg: "auto" }}> |
| 89 | + <Button |
| 90 | + variant="outline-color" |
| 91 | + w={{ base: "full", lg: "auto" }} |
| 92 | + onClick={handleStart} |
| 93 | + > |
| 94 | + <Translation id="start" /> |
| 95 | + </Button> |
| 96 | + </Box> |
| 97 | + </Flex> |
| 98 | + </ListItem> |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +export default QuizItem |
0 commit comments