Skip to content

Commit 85b3c32

Browse files
committed
feat: add question component
1 parent cfab6ef commit 85b3c32

File tree

7 files changed

+176
-58
lines changed

7 files changed

+176
-58
lines changed

components/mdx/Components.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dracula from 'react-syntax-highlighter/dist/cjs/styles/prism/dracula'
44
import { CopyToClipboard } from '../CopyToClipboard'
55
import SideDrawer from './SideDrawer'
66
import Quiz from './Quiz'
7+
import Question from './Question'
78
import Callout from './Callout'
89

910
const Components = {
@@ -43,6 +44,7 @@ const Components = {
4344
SideDrawer,
4445
Callout,
4546
Quiz,
47+
Question,
4648
}
4749

4850
export default Components

components/mdx/Question.tsx

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { Box, VStack, Text, Button, useToast } from '@chakra-ui/react'
2+
import React, { FC, useState } from 'react'
3+
4+
interface QuestionProps {
5+
question: string
6+
}
7+
8+
interface Question {
9+
question: string
10+
options: [
11+
{
12+
answer: string
13+
correct?: boolean
14+
},
15+
]
16+
}
17+
18+
interface Answers {
19+
[index: string]: number
20+
}
21+
22+
const Question: FC<QuestionProps> = (props: QuestionProps) => {
23+
const question: Question = require(`../../utils/questions/${props.question}.json`)
24+
const [optionSelected, setOptionSelected]: [
25+
number,
26+
React.Dispatch<React.SetStateAction<number>>,
27+
] = useState(-1)
28+
const toast = useToast()
29+
30+
const selectAnswer = (optionIndex: number) => {
31+
setOptionSelected(optionIndex)
32+
}
33+
34+
const getOptionBackground = (optionIndex: number) => {
35+
if (optionSelected == optionIndex) {
36+
return 'yellow.600'
37+
}
38+
return 'gray.600'
39+
}
40+
41+
const quizNotAnswered = () => {
42+
toast({
43+
title: 'No answer selected',
44+
description: 'Choose an answer!',
45+
status: 'warning',
46+
duration: 9000,
47+
isClosable: true,
48+
})
49+
}
50+
51+
const quizFailedToast = () => {
52+
toast({
53+
title: 'Wrong answer',
54+
description: `Try again fren`,
55+
status: 'error',
56+
duration: 9000,
57+
isClosable: true,
58+
})
59+
}
60+
61+
const quizSuccessToast = () => {
62+
toast({
63+
title: 'Correct answer!',
64+
description: "Let's goooo",
65+
status: 'success',
66+
duration: 9000,
67+
isClosable: true,
68+
})
69+
}
70+
71+
const submit = () => {
72+
if (optionSelected === -1) {
73+
return quizNotAnswered()
74+
}
75+
76+
if (question.options[optionSelected].correct) {
77+
return quizSuccessToast()
78+
}
79+
80+
return quizFailedToast()
81+
}
82+
83+
return (
84+
<VStack
85+
spacing={4}
86+
background="gray.900"
87+
padding="6"
88+
borderRadius="md"
89+
mt="7"
90+
>
91+
<Text fontWeight="bold" textAlign="left" w="100%">
92+
{question.question}
93+
</Text>
94+
{question.options.map((o, index) => {
95+
return (
96+
<Box
97+
w="100%"
98+
borderRadius="md"
99+
background={getOptionBackground(index)}
100+
padding="3"
101+
cursor="pointer"
102+
onClick={() => selectAnswer(index)}
103+
key={index}
104+
>
105+
{o.answer}
106+
</Box>
107+
)
108+
})}
109+
<Button
110+
mx="1"
111+
mt="7"
112+
colorScheme="green"
113+
backgroundColor="green.400"
114+
onClick={submit}
115+
alignSelf="flex-start"
116+
>
117+
Submit
118+
</Button>
119+
</VStack>
120+
)
121+
}
122+
123+
export default Question

pages/lessons/projects/1.mdx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ benefits to this decentralised model as we’ll continue to see.
122122

123123
</SideDrawer>
124124

125+
<Question question="question-1" />
126+
125127
## Let’s get this party started!
126128

127129
We’re going to write a smart contract, which is going to be a “Web3
@@ -297,13 +299,15 @@ contract WAGMI {
297299
<Callout emoji='💡' size='md' variant='info'>
298300
While private and internal are not readable or modifiable from other contracts, its values are set on a public blockchain, making its values visible from the outside world.
299301
</Callout>
300-
302+
301303
#### Which visibility should I use ?
302304

303305
A simple answer to this is , start with giving every variable a `private` visibility then as you move further in contract developement modify variables to `internal` visibility as a next step of modification. Try to use `public` visibility as little as possible in order to be gas efficient and making less vulnerabilities in the smart contract.
304306

305307
</SideDrawer>
306308

309+
<Question question="question-2" />
310+
307311
## Creating Functions
308312

309313
Our contract needs a way to store new information on the blockchain! But both
@@ -395,6 +399,8 @@ some more about them in **Variables** and **Functions**.
395399

396400
</SideDrawer>
397401

402+
<Question question="question-3" />
403+
398404
Ok, now we have to figure out a way to store some info on our contract!
399405

400406
We want to _set_ a new message in our contract, so now we define a new function
@@ -705,27 +711,14 @@ you see the magical spell that is being cast here?
705711
![Wizard Wooshing on Successful Lesson](/assets/lessons/1/img_10.png)
706712

707713
Before you go ahead and tell us: **what your future in web3 is,** have a check
708-
on what you didn’t know a little while ago, and what you know now! *
709-
710-
Apart from a user wallet, what else uses a blockchain (Ethereum) address?
711-
712-
How many parameters can we have in an event?
713-
714-
What can we use to find a past event?
714+
on what you didn’t know a little while ago, and what you know now! Take the quiz
715+
and prove your knowledge :)
715716

716-
Ethereum uses something for transaction fees. What’s it called?
717-
718-
Where do the values for event parameters get stored?
719-
720-
What is the use of the **pragma solidity** statement in our smart contract?
721-
722-
Do state variables stay permanently on the blockchain?
723-
724-
Can a view function modify the state of the blockchain?
725-
726-
What can we use a smart contract for?
717+
<br />
727718

728719
<Quiz quiz="lesson-1" />
729720

721+
<br />
722+
730723
**Now, go to the community in Discord to share your new Open Sourcerer
731724
powers**......and find the answers too!!!

utils/questions/question-1.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"question": "Apart from a user wallet, what else uses a blockchain (Ethereum) address?",
3+
"options": [
4+
{
5+
"answer": "Transactions"
6+
},
7+
{
8+
"answer": "Smart contracts",
9+
"correct": true
10+
},
11+
{
12+
"answer": "NFTs"
13+
}
14+
]
15+
}

utils/questions/question-2.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"question": "Do state variables stay permanently on the blockchain?",
3+
"options": [
4+
{
5+
"answer": "Yes",
6+
"correct": true
7+
},
8+
{
9+
"answer": "No"
10+
}
11+
]
12+
}

utils/questions/question-3.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"question": "Can a view function modify the state of the blockchain?",
3+
"options": [
4+
{
5+
"answer": "Yes"
6+
},
7+
{
8+
"answer": "No",
9+
"correct": true
10+
}
11+
]
12+
}

utils/quizzes/lesson-1.json

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
{
22
"title": "Quiz: Lesson 1",
33
"questions": [
4-
{
5-
"question": "Apart from a user wallet, what else uses a blockchain (Ethereum) address?",
6-
"options": [
7-
{
8-
"answer": "Transactions"
9-
},
10-
{
11-
"answer": "Smart contracts",
12-
"correct": true
13-
},
14-
{
15-
"answer": "NFTs"
16-
}
17-
]
18-
},
194
{
205
"question": "In order to shut down a blockchain you would have to:",
216
"options": [
@@ -118,30 +103,6 @@
118103
}
119104
]
120105
},
121-
{
122-
"question": "Do state variables stay permanently on the blockchain?",
123-
"options": [
124-
{
125-
"answer": "Yes",
126-
"correct": true
127-
},
128-
{
129-
"answer": "No"
130-
}
131-
]
132-
},
133-
{
134-
"question": "Can a view function modify the state of the blockchain?",
135-
"options": [
136-
{
137-
"answer": "Yes"
138-
},
139-
{
140-
"answer": "No",
141-
"correct": true
142-
}
143-
]
144-
},
145106
{
146107
"question": "What can we use a smart contract for?",
147108
"options": [

0 commit comments

Comments
 (0)