Skip to content

Commit 5c8ba7d

Browse files
feat(devx) - Import Quiz Component from the Wiki (#2643)
* feat(devx): added quiz component * fix(devx): removed SIPs link * fix(devx): typos * fix(devx): fixed button colours and text wrap
1 parent 065513f commit 5c8ba7d

File tree

5 files changed

+198
-2
lines changed

5 files changed

+198
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
description: How to add a Quiz to the IOTA Docs.
3+
---
4+
5+
import Quiz from '@site/src/components/Quiz';
6+
import questions from './example-questions.json';
7+
8+
# How To Add A Quiz
9+
10+
You can simply add a Quiz to every page you like in the Wiki!
11+
12+
## Example Quiz
13+
14+
<Quiz questions={questions} />
15+
16+
## Add Your Quiz
17+
18+
Just add the following lines on top of the page.
19+
20+
```javascript
21+
import Quiz from '@site/src/components/Quiz';
22+
import questions from './example-questions.json';
23+
```
24+
25+
Make sure that the Quiz path fits your folder structure. You may need to remove or add some `../`.
26+
27+
Add your questions in a separate `.json` file with the following format:
28+
29+
```json
30+
[
31+
{
32+
"questionText": "First Question?",
33+
"answerOptions": [
34+
{ "answerText": "1. Answer", "isCorrect": false },
35+
{ "answerText": "2. Answer", "isCorrect": false },
36+
{ "answerText": "3. Answer", "isCorrect": true },
37+
{ "answerText": "4. Answer", "isCorrect": false }
38+
]
39+
},
40+
...
41+
]
42+
```
43+
44+
Now you need to include the Quiz components where you want to show it
45+
46+
```
47+
48+
## Your Quiz
49+
50+
<Quiz questions={questions} />
51+
52+
```
53+
54+
That's all. Congratulations, you have created your own Quiz!
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"questionText": "What is the capital of France?",
4+
"answerOptions": [
5+
{ "answerText": "New York", "isCorrect": false },
6+
{ "answerText": "London", "isCorrect": false },
7+
{ "answerText": "Paris", "isCorrect": true },
8+
{ "answerText": "Dublin", "isCorrect": false }
9+
]
10+
},
11+
{
12+
"questionText": "Who is CEO of Tesla?",
13+
"answerOptions": [
14+
{ "answerText": "Jeff Bezos", "isCorrect": false },
15+
{ "answerText": "Elon Musk", "isCorrect": true },
16+
{ "answerText": "Bill Gates", "isCorrect": false },
17+
{ "answerText": "Tony Stark", "isCorrect": false }
18+
]
19+
},
20+
{
21+
"questionText": "The iPhone was created by which company?",
22+
"answerOptions": [
23+
{ "answerText": "Apple", "isCorrect": true },
24+
{ "answerText": "Intel", "isCorrect": false },
25+
{ "answerText": "Amazon", "isCorrect": false },
26+
{ "answerText": "Microsoft", "isCorrect": false }
27+
]
28+
},
29+
{
30+
"questionText": "How many Harry Potter books are there?",
31+
"answerOptions": [
32+
{ "answerText": "1", "isCorrect": false },
33+
{ "answerText": "4", "isCorrect": false },
34+
{ "answerText": "6", "isCorrect": false },
35+
{ "answerText": "7", "isCorrect": true }
36+
]
37+
}
38+
]

docs/content/sidebars/references.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ const references = [
355355
'references/contribute/contribution-process',
356356
'references/contribute/code-of-conduct',
357357
'references/contribute/style-guide',
358+
'references/contribute/add-a-quiz',
358359
'references/contribute/import-code-docs'
359360
],
360361
},
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React, { useState, useEffect } from 'react';
2+
3+
export default function Quiz(_questions) {
4+
const { questions } = _questions;
5+
6+
useEffect(() => {
7+
setTimeout(() => {
8+
reset();
9+
}, 1000);
10+
}, [questions]);
11+
12+
const [currentQuestion, setCurrentQuestion] = useState(0);
13+
const [showScore, setShowScore] = useState(false);
14+
const [score, setScore] = useState(0);
15+
const [clicked, setClicked] = useState(-1);
16+
17+
const handleAnswerOptionClick = (isCorrect, index) => {
18+
setClicked(index);
19+
if (isCorrect) {
20+
setScore(score + 1);
21+
}
22+
23+
setTimeout(() => {
24+
const nextQuestion = currentQuestion + 1;
25+
if (nextQuestion < questions.length) {
26+
setCurrentQuestion(nextQuestion);
27+
} else {
28+
setShowScore(true);
29+
}
30+
setClicked(-1);
31+
}, 1000);
32+
};
33+
34+
const reset = () => {
35+
setShowScore(false);
36+
setScore(0);
37+
setCurrentQuestion(0);
38+
};
39+
40+
return (
41+
<div className='app'>
42+
{showScore ? (
43+
<div className='score-section'>
44+
<p>
45+
You scored {score} out of {questions.length}
46+
</p>
47+
<a
48+
className='button button--outline button--primary'
49+
onClick={() => reset()}
50+
>
51+
{'Replay'}
52+
</a>
53+
</div>
54+
) : (
55+
<>
56+
<div className='card'>
57+
<div className='card__header'>
58+
<h3>
59+
Question {currentQuestion + 1}/{questions.length}
60+
</h3>
61+
</div>
62+
<div className='card__body'>
63+
{questions[currentQuestion]?.questionText}
64+
</div>
65+
<div className='card__footer'>
66+
{questions[currentQuestion]?.answerOptions.map(
67+
(answerOption, index) => (
68+
<button
69+
className={`button button--block button--primary margin-bottom--xs ${
70+
clicked >= 0 && answerOption.isCorrect
71+
? 'button--success'
72+
: ''
73+
} ${
74+
clicked === index && !answerOption.isCorrect
75+
? 'button--danger'
76+
: ''
77+
}`}
78+
key={'answer-' + index}
79+
onClick={() =>
80+
handleAnswerOptionClick(answerOption.isCorrect, index)
81+
}
82+
>
83+
{answerOption.answerText}
84+
</button>
85+
),
86+
)}
87+
</div>
88+
</div>
89+
</>
90+
)}
91+
</div>
92+
);
93+
}

docs/site/src/css/custom.css

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
.pagination-nav__label {
176176
color: white;
177177
}
178+
178179
.pagination-nav__link--next {
179180
background-color: #0101ff;
180181
display: flex;
@@ -410,8 +411,18 @@ h4 {
410411
}
411412

412413
/** Globals **/
413-
.button-cta {
414+
.button{
415+
text-wrap: pretty;
414416
color: var(--iota-white);
417+
background: var(--iota-hero-dark);
418+
}
419+
.button--success{
420+
background: var(--iota-success-dark);
421+
}
422+
.button--danger{
423+
background: var(--iota-issue-dark);
424+
}
425+
.button-cta {
415426
letter-spacing: -0.3px;
416427
cursor: pointer;
417428
border-style: none;
@@ -428,7 +439,6 @@ h4 {
428439
border-radius: 2px 0px 0px 0px;
429440
justify-content: space-between;
430441
opacity: 0px;
431-
background: #0101ff;
432442
align-items: center;
433443
justify-content: center;
434444
gap: 1rem;

0 commit comments

Comments
 (0)