Skip to content

Commit

Permalink
feat: add RandomFacts.tsx basic structure and behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc-resourcify committed Jul 25, 2023
1 parent 32385eb commit b07fa9a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app/(public)/about/free-time/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from 'react';
import Books from '@/components/organisms/about-free-time/Books';
import Music from '@/components/organisms/about-free-time/Music';
import Podcasts from '@/components/organisms/about-free-time/Podcasts';
import RandomFacts from '@/components/organisms/about-free-time/RandomFacts';
import TvSeries from '@/components/organisms/about-free-time/TvSeries';
import VideoGames from '@/components/organisms/about-free-time/VideoGames';

Expand Down Expand Up @@ -67,6 +68,8 @@ const AboutFreeTimePage = async () => {
<TvSeries tvSeries={tvSeries} />

<Music />

<RandomFacts />
</div>
</section>
</main>
Expand Down
79 changes: 79 additions & 0 deletions src/components/organisms/about-free-time/RandomFacts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use client';

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';

const RandomFacts = () => {
const [selectedAnswer, setSelectedAnswer] = useState('');

const [submitted, setSubmitted] = useState(false);

const [alreadyPlayed, setAlreadyPlayed] = useState(() => {
const alreadyPlayed = localStorage.getItem(localStorageKey);
return alreadyPlayed ? JSON.parse(alreadyPlayed) : false;
});

const isButtonDisabled = selectedAnswer === '';

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSelectedAnswer(event.target.value);
};

const submitAnswer = () => {
localStorage.setItem(localStorageKey, 'true');

setSubmitted(true);
setAlreadyPlayed(true);
};

return (
<div className='mb-6'>
<div className='mb-6 mt-2 flex'>
<h2>Random facts about me</h2>
</div>

{!submitted && !alreadyPlayed && (
<div className='rounded dark:bg-slate-900'>
<FormControl>
<RadioGroup
aria-labelledby='demo-radio-buttons-group-label'
name='radio-buttons-group'
value={selectedAnswer}
onChange={handleChange}
>
<FormControlLabel value='a' control={<Radio />} label='A' />
<FormControlLabel value='b' control={<Radio />} label='B' />
<FormControlLabel value='c' control={<Radio />} label='C' />
<FormControlLabel value='d' control={<Radio />} label='D' />
</RadioGroup>

<Button
className='shadow-md'
variant='primary'
onClick={submitAnswer}
disabled={isButtonDisabled}
>
Submit
</Button>
</FormControl>
</div>
)}
{submitted && alreadyPlayed && (
<div className='rounded bg-gray-200 p-4'>
<p className='text-gray-800'>Thank you for your submission!</p>
</div>
)}
{!submitted && alreadyPlayed && <div>Already played :)</div>}
</div>
);
};

export default RandomFacts;

0 comments on commit b07fa9a

Please sign in to comment.