This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/website/pages/functions/chronometerTimer.jsx

147 lines
4.1 KiB
React
Raw Normal View History

2020-08-03 12:04:07 +02:00
import { useState } from 'react'
import Codepen from 'react-codepen-embed'
import redirect from '../../utils/redirect'
import FunctionPage from '../../components/FunctionPage/FunctionPage'
import FunctionTabs from '../../components/FunctionPage/FunctionTabs'
import FunctionArticle from '../../components/FunctionPage/FunctionArticle'
import FunctionComments from '../../components/FunctionPage/FunctionComments/FunctionComments'
import Loader from '../../components/Loader'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPlay, faPause, faSync } from '@fortawesome/free-solid-svg-icons'
import api from '../../utils/api'
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
let interval
function convertSeconds (seconds) {
return {
minutes: Math.floor(seconds / 60),
seconds: seconds % 60
}
2020-04-29 18:51:30 +02:00
}
const Chronometer = () => {
2020-08-03 12:04:07 +02:00
const [timeLength, setTimeLength] = useState(0) // seconds
const [isPlaying, setIsPlaying] = useState(false)
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
const handlePlayPause = () => {
if (isPlaying) {
clearInterval(interval)
} else {
if (interval) clearInterval(interval)
interval = setInterval(() => {
setTimeLength(time => time + 1)
2020-08-03 12:04:07 +02:00
}, 1000)
2020-04-29 18:51:30 +02:00
}
2020-08-03 12:04:07 +02:00
setIsPlaying(!isPlaying)
}
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
const handleReset = () => {
if (interval) clearInterval(interval)
setIsPlaying(false)
setTimeLength(0)
}
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
const getFormattedValue = () => {
const minutesAndSeconds = convertSeconds(timeLength)
const minutes =
minutesAndSeconds.minutes < 100
? ('0' + minutesAndSeconds.minutes).slice(-2)
: minutesAndSeconds.minutes
2020-08-03 12:04:07 +02:00
const seconds = ('0' + minutesAndSeconds.seconds).slice(-2)
return `${minutes}:${seconds}`
}
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
return (
<div className='container-fluid'>
<div className='row justify-content-center'>
<div className='col-sm-24 col-md-12'>
<div className='Chronometer__container'>
<div className='Chronometer__item'>
<div className='Chronomter__row'>
<div className='Chronometer__time-left'>
{getFormattedValue()}
2020-04-29 18:51:30 +02:00
</div>
2020-08-03 12:04:07 +02:00
</div>
</div>
<div className='Chronometer__item Chronometer__buttons'>
<div className='Chronomter__row Chronometer__row-button'>
<button onClick={handlePlayPause} className='Chronometer-btn'>
<FontAwesomeIcon
{...(isPlaying ? { icon: faPause } : { icon: faPlay })}
/>
2020-08-03 12:04:07 +02:00
</button>
<button
onClick={handleReset}
className='Chronometer-btn'
title='Remettre à zéro ?'
>
2020-08-03 12:04:07 +02:00
<FontAwesomeIcon icon={faSync} />
</button>
</div>
2020-04-29 18:51:30 +02:00
</div>
2020-08-03 12:04:07 +02:00
</div>
2020-04-29 18:51:30 +02:00
</div>
2020-08-03 12:04:07 +02:00
</div>
</div>
)
2020-04-29 18:51:30 +02:00
}
const Pomodoro = () => {
2020-08-03 12:04:07 +02:00
return (
<div style={{ marginBottom: '50px' }} className='container-fluid'>
<Codepen
hash='vYEbPoB'
user='Divlo'
height={800}
defaultTab='result'
preview={false}
loader={() => <Loader />}
/>
2020-08-03 12:04:07 +02:00
</div>
)
2020-04-29 18:51:30 +02:00
}
const FunctionTabManager = props => {
2020-08-03 12:04:07 +02:00
return (
<FunctionTabs
setSlideIndex={props.setSlideIndex}
slideIndex={props.slideIndex}
>
2020-08-03 12:04:07 +02:00
<div className='FunctionComponent__slide'>
<Chronometer />
</div>
<div className='FunctionComponent__slide'>
<Pomodoro />
</div>
<div className='FunctionComponent__slide'>
<FunctionArticle article={props.article} />
</div>
<div className='FunctionComponent__slide'>
<FunctionComments functionId={props.id} />
</div>
</FunctionTabs>
)
2020-04-29 18:51:30 +02:00
}
const chronometerTimer = props => (
2020-08-03 12:04:07 +02:00
<FunctionPage
FunctionTabManager={FunctionTabManager}
{...props}
tabNames={[
'⏰ Chronomètre',
'⌛ Pomodoro',
'📝 Article',
'📬 Commentaires'
]}
2020-08-03 12:04:07 +02:00
/>
)
2020-04-29 18:51:30 +02:00
2020-08-03 12:04:07 +02:00
export async function getServerSideProps (context) {
return api
.get('/functions/chronometerTimer')
.then(response => ({ props: response.data }))
2020-08-03 12:04:07 +02:00
.catch(() => redirect(context, '/404'))
2020-04-29 18:51:30 +02:00
}
2020-08-03 12:04:07 +02:00
export default chronometerTimer