-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8094 from ethereum/learnQuizzes
Learn quizzes
- Loading branch information
Showing
30 changed files
with
1,844 additions
and
11 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,153 @@ | ||
// Import libraries | ||
import React, { useMemo } from "react" | ||
import { | ||
Box, | ||
chakra, | ||
Circle, | ||
Flex, | ||
RadioProps, | ||
Text, | ||
useRadio, | ||
useRadioGroup, | ||
} from "@chakra-ui/react" | ||
|
||
// Import types | ||
import { Question } from "../../types" | ||
|
||
// Interfaces | ||
export interface CustomRadioProps extends RadioProps { | ||
index: number | ||
label: string | ||
} | ||
export interface IProps { | ||
questionData: Question | ||
showAnswer: boolean | ||
handleSelection: (answerId: string) => void | ||
selectedAnswer: string | null | ||
} | ||
|
||
// Component | ||
const QuizRadioGroup: React.FC<IProps> = ({ | ||
questionData, | ||
showAnswer, | ||
handleSelection, | ||
selectedAnswer, | ||
}) => { | ||
const { getRadioProps, getRootProps } = useRadioGroup({ | ||
onChange: handleSelection, | ||
}) | ||
const { prompt, answers, correctAnswerId } = questionData | ||
|
||
// Memoized values | ||
const explanation = useMemo<string>(() => { | ||
if (!selectedAnswer) return "" | ||
return answers.filter(({ id }) => id === selectedAnswer)[0].explanation | ||
}, [selectedAnswer]) | ||
|
||
const isSelectedCorrect = useMemo<boolean>( | ||
() => correctAnswerId === selectedAnswer, | ||
[selectedAnswer] | ||
) | ||
|
||
// Custom radio button component | ||
const CustomRadio: React.FC<CustomRadioProps> = ({ | ||
index, | ||
label, | ||
...radioProps | ||
}) => { | ||
const { state, getInputProps, getCheckboxProps, htmlProps } = | ||
useRadio(radioProps) | ||
|
||
// Memoized values | ||
const buttonBg = useMemo<string>(() => { | ||
if (!state.isChecked) return "bodyInverted" | ||
if (!showAnswer) return "primary" | ||
if (!isSelectedCorrect) return "error" | ||
return "success" | ||
}, [state.isChecked, showAnswer, isSelectedCorrect]) | ||
|
||
// Render CustomRadio component | ||
return ( | ||
<chakra.label {...htmlProps} cursor="pointer" data-group w="100%"> | ||
<input {...getInputProps({})} hidden /> | ||
<Flex | ||
{...getCheckboxProps()} | ||
w="100%" | ||
p={2} | ||
alignItems="center" | ||
bg={buttonBg} | ||
color={state.isChecked ? "white" : "text"} | ||
borderRadius="base" | ||
_hover={{ | ||
boxShadow: showAnswer ? "none" : "primary", | ||
outline: showAnswer | ||
? "none" | ||
: "1px solid var(--eth-colors-primary)", | ||
cursor: showAnswer ? "default" : "pointer", | ||
}} | ||
> | ||
<Circle | ||
size="25px" | ||
bg={ | ||
showAnswer | ||
? "white" | ||
: state.isChecked | ||
? "primaryPressed" | ||
: "disabled" | ||
} | ||
_groupHover={{ | ||
bg: showAnswer ? "white" : "primaryPressed", | ||
}} | ||
me={2} | ||
> | ||
<Text | ||
m="0" | ||
fontWeight="700" | ||
fontSize="lg" | ||
color={ | ||
!showAnswer ? "white" : isSelectedCorrect ? "success" : "error" | ||
} | ||
> | ||
{String.fromCharCode(97 + index).toUpperCase()} | ||
</Text> | ||
</Circle> | ||
{label} | ||
</Flex> | ||
</chakra.label> | ||
) | ||
} | ||
|
||
// Render QuizRadioGroup | ||
return ( | ||
<Flex {...getRootProps()} direction="column" w="100%"> | ||
<Text fontWeight="700" fontSize="2xl" mb={6}> | ||
{prompt} | ||
</Text> | ||
<Flex direction="column" gap={4}> | ||
{answers.map(({ id, label }, index) => { | ||
const display = | ||
!showAnswer || id === selectedAnswer ? "inline-flex" : "none" | ||
return ( | ||
<CustomRadio | ||
key={id} | ||
display={display} | ||
index={index} | ||
label={label} | ||
{...getRadioProps({ value: id })} | ||
/> | ||
) | ||
})} | ||
</Flex> | ||
{showAnswer && ( | ||
<Box mt={5}> | ||
<Text fontWeight="bold" mt={0} mb={2}> | ||
Explanation | ||
</Text> | ||
<Text m={0}>{explanation}</Text> | ||
</Box> | ||
)} | ||
</Flex> | ||
) | ||
} | ||
|
||
export default QuizRadioGroup |
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,75 @@ | ||
// Import libraries | ||
import React, { useMemo } from "react" | ||
import { Box, Flex, Text, useMediaQuery } from "@chakra-ui/react" | ||
import { useIntl } from "react-intl" | ||
|
||
// Import utilities | ||
import { numberToPercent } from "../../utils/numberToPercent" | ||
|
||
// Interfaces | ||
export interface IProps { | ||
correctCount: number | ||
isPassingScore: boolean | ||
questionCount: number | ||
ratioCorrect: number | ||
} | ||
|
||
// Component | ||
const QuizSummary: React.FC<IProps> = ({ | ||
correctCount, | ||
isPassingScore, | ||
questionCount, | ||
ratioCorrect, | ||
}) => { | ||
const { locale } = useIntl() | ||
const [largerThanMobile] = useMediaQuery("(min-width: 30em)") | ||
|
||
const valueStyles = { fontWeight: "700", mb: 2 } | ||
const labelStyles = { fontSize: "sm", m: 0, color: "disabled" } | ||
|
||
// Render QuizSummary component | ||
return ( | ||
<Box w="full" fontSize={["xl", "2xl"]}> | ||
<Text fontWeight="700" textAlign="center"> | ||
{isPassingScore ? "You passed the quiz!" : "Your results"} | ||
</Text> | ||
<Flex | ||
p={4} | ||
justify="center" | ||
boxShadow="drop" | ||
bg="background" | ||
mx="auto" | ||
w="fit-content" | ||
sx={{ | ||
"div:not(:last-of-type)": { | ||
borderEnd: "1px", | ||
borderColor: "disabled", | ||
}, | ||
div: { | ||
p: 4, | ||
flexDirection: "column", | ||
alignItems: "center", | ||
}, | ||
}} | ||
overflowX="hidden" | ||
> | ||
<Flex> | ||
<Text {...valueStyles}>{numberToPercent(ratioCorrect, locale)}</Text> | ||
<Text {...labelStyles}>Score</Text> | ||
</Flex> | ||
<Flex> | ||
<Text {...valueStyles}>+{correctCount}</Text> | ||
<Text {...labelStyles}>Correct</Text> | ||
</Flex> | ||
{largerThanMobile && ( | ||
<Flex> | ||
<Text {...valueStyles}>{questionCount}</Text> | ||
<Text {...labelStyles}>Total</Text> | ||
</Flex> | ||
)} | ||
</Flex> | ||
</Box> | ||
) | ||
} | ||
|
||
export default QuizSummary |
Oops, something went wrong.