generated from theodorusclarence/ts-nextjs-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add RandomFact quiz as SB story
- Loading branch information
1 parent
610fedf
commit 554a391
Showing
8 changed files
with
1,300 additions
and
1,146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import FormControl from '@mui/material/FormControl'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import Radio from '@mui/material/Radio'; | ||
import RadioGroup from '@mui/material/RadioGroup'; | ||
import * as React from 'react'; | ||
import { useState } from 'react'; | ||
|
||
import Button from '@/components/buttons/Button'; | ||
|
||
const localStorageKey = 'alreadyPlayed'; | ||
|
||
export enum Option { | ||
A = 'a', | ||
B = 'b', | ||
C = 'c', | ||
D = 'd', | ||
} | ||
|
||
export interface QuizOption { | ||
headline: string; | ||
key: Option; | ||
} | ||
|
||
export interface QuizProps { | ||
options: QuizOption[]; | ||
falseOption: Option; | ||
} | ||
|
||
const Quiz = ({ options, falseOption }: QuizProps) => { | ||
const [selectedAnswer, setSelectedAnswer] = useState(''); | ||
|
||
const [submitted, setSubmitted] = useState(false); | ||
|
||
const isButtonDisabled = selectedAnswer === ''; | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setSelectedAnswer(event.target.value); | ||
}; | ||
|
||
const submitAnswer = () => { | ||
localStorage.setItem(localStorageKey, 'true'); | ||
|
||
setSubmitted(true); | ||
|
||
const answeredRight = falseOption === selectedAnswer; | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(submitted && answeredRight ? 'Correct!' : 'Wrong!'); | ||
}; | ||
|
||
return ( | ||
<div className='p4 rounded dark:bg-slate-900'> | ||
<FormControl> | ||
<RadioGroup | ||
className='mb-4' | ||
aria-labelledby='demo-radio-buttons-group-label' | ||
name='radio-buttons-group' | ||
value={selectedAnswer} | ||
onChange={handleChange} | ||
> | ||
{options.map((option) => ( | ||
<FormControlLabel | ||
className='mb-2 md:mb-0' | ||
key={option.key} | ||
value={option.key} | ||
control={<Radio />} | ||
label={option.headline} | ||
/> | ||
))} | ||
</RadioGroup> | ||
|
||
<div className='flex flex-row justify-end md:justify-start'> | ||
<Button | ||
className='w-fit shadow-md' | ||
variant='primary' | ||
onClick={submitAnswer} | ||
disabled={isButtonDisabled} | ||
> | ||
Submit | ||
</Button> | ||
</div> | ||
</FormControl> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Quiz; |
42 changes: 42 additions & 0 deletions
42
src/components/molecules/RandomFacts/__stories__/Quiz.stories.tsx
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,42 @@ | ||
import { Meta } from '@storybook/react'; | ||
|
||
import Quiz, { | ||
Option, | ||
QuizOption, | ||
QuizProps, | ||
} from '@/components/molecules/RandomFacts/Quiz'; | ||
|
||
const meta: Meta<typeof Quiz> = { | ||
title: 'Components/Random Facts/Quiz', | ||
component: Quiz, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
|
||
const options: QuizOption[] = [ | ||
{ | ||
key: Option.A, | ||
headline: | ||
'I have been officially excommunicated by the Roman Catholic Church ⛪', | ||
}, | ||
{ | ||
key: Option.B, | ||
headline: 'I studied oboe at the music school for more than 12 years 🎶', | ||
}, | ||
{ | ||
key: Option.C, | ||
headline: 'I know flags and capitals of all 195 countries in the world 🇰🇲', | ||
}, | ||
{ | ||
key: Option.D, | ||
headline: | ||
'I was an extra-actor in a movie once: "Correspondence" by Giuseppe Tornatore 🎬', | ||
}, | ||
]; | ||
|
||
export const SampleStory = (args: QuizProps) => <Quiz {...args} />; | ||
SampleStory.args = { | ||
options, | ||
falseOption: Option.C, | ||
}; |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.