Files
pawol.nu/components/teks/teks-drawer.js
T

262 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-05-22 22:18:43 +04:00
import {useEffect, useState, useContext} from 'react'
import Link from 'next/link'
import {useParams} from 'next/navigation'
2022-05-22 22:18:43 +04:00
import PropTypes from 'prop-types'
import {styled} from '@mui/material/styles'
import AppBar from '@mui/material/AppBar'
import Box from '@mui/material/Box'
import CssBaseline from '@mui/material/CssBaseline'
import Drawer from '@mui/material/Drawer'
import IconButton from '@mui/material/IconButton'
import MenuIcon from '@mui/icons-material/Menu'
import Toolbar from '@mui/material/Toolbar'
import Typography from '@mui/material/Typography'
import LinearProgress from '@mui/material/LinearProgress'
import CircularProgress from '@mui/material/CircularProgress'
2022-01-19 07:06:26 +04:00
import KeyboardBackspaceIcon from '@mui/icons-material/KeyboardBackspace'
2022-05-22 22:18:43 +04:00
import {jwennTeks} from '../../lib/oki-api'
2022-05-22 22:18:43 +04:00
import ParolesListContext from '../../contexts/paroles-list'
import MontreTeks from './montre-teks'
import DenyeTeks from './denye-teks'
import Teks from './teks'
2022-05-22 22:18:43 +04:00
import VweKouteAchte from './vwe-koute-achte'
2022-05-23 03:19:46 +04:00
import DrawerBar from './drawer-bar'
2022-05-22 22:18:43 +04:00
const drawerWidth = 240
2021-06-02 02:24:19 +02:00
2022-05-22 22:18:43 +04:00
const PREFIX = 'drawer-bar'
2022-01-19 06:35:04 +04:00
const classes = {
2022-05-22 22:18:43 +04:00
vwe: `${PREFIX}-vwe`,
koute: `${PREFIX}-koute`
2022-01-19 06:35:04 +04:00
}
2022-05-22 22:18:43 +04:00
const Stream = styled('div')((
{
theme
}
) => ({
2022-01-19 06:35:04 +04:00
[`& .${classes.koute}`]: {
2020-12-15 08:50:14 +01:00
position: 'absolute',
2021-06-05 21:23:31 +02:00
right: '40px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
},
2022-01-19 06:35:04 +04:00
[`& .${classes.vwe}`]: {
2020-12-15 08:50:14 +01:00
position: 'absolute',
2021-06-05 21:23:31 +02:00
right: '90px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
}
}))
2022-05-22 22:18:43 +04:00
export default function TeksDrawer({parole, paroleId, slug, denyeTeks}) {
2021-06-02 02:24:19 +02:00
const [open, setOpen] = useState(false)
2022-05-22 22:18:43 +04:00
const {paroles, setParoles} = useContext(ParolesListContext)
const [mobileOpen, setMobileOpen] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [isParolesHidden, setIsParolesHidden] = useState(true)
const [error, setError] = useState('')
2021-06-02 02:24:19 +02:00
const [success, setSuccess] = useState('')
2022-05-22 22:18:43 +04:00
const handleOpenDrawer = async () => {
setMobileOpen(true)
if (paroles.length === 0) {
setIsLoading(true)
try {
const teks = await jwennTeks()
setParoles(teks)
} catch (error) {
console.log('⚠️ error', error.message)
}
setIsLoading(false)
2021-06-02 02:24:19 +02:00
}
2022-05-22 22:18:43 +04:00
}
2021-06-02 02:24:19 +02:00
2022-05-22 22:18:43 +04:00
const handleCloseDrawer = () => {
setMobileOpen(false)
2021-06-02 02:24:19 +02:00
}
2022-05-22 22:18:43 +04:00
const handleClick = async () => {
setIsLoading(true)
try {
const teks = await jwennTeks()
setParoles(teks)
} catch (error) {
console.log('⚠️ error', error.message)
}
setIsLoading(false)
setIsParolesHidden(false)
}
2022-05-22 22:18:43 +04:00
useEffect(() => {
if (paroles.length > 0) {
setIsParolesHidden(false)
}
}, [paroles.length])
2021-06-02 02:24:19 +02:00
useEffect(() => {
if (error || success) {
setOpen(true)
}
}, [error, success, setOpen])
2022-05-22 22:18:43 +04:00
const handleClose = (event, reason) => {
if (reason === 'clickaway') {
return
}
setOpen(false)
setSuccess('')
setError('')
}
return (
2022-05-22 22:18:43 +04:00
<Box sx={{display: 'flex'}}>
<CssBaseline />
2022-05-22 22:18:43 +04:00
<AppBar
position='fixed'
sx={{
width: {sm: `calc(100% - ${drawerWidth}px)`},
ml: {sm: `${drawerWidth}px`},
borderTop: '2px solid #303030',
marginTop: '3rem',
zIndex: 1
}}
>
<Toolbar>
<IconButton
color='inherit'
aria-label='open drawer'
edge='start'
2022-05-22 22:18:43 +04:00
sx={{mr: 2, display: {sm: 'none'}}}
onClick={handleOpenDrawer}
>
<MenuIcon />
</IconButton>
2022-05-20 02:15:56 +04:00
{parole ? (
2022-05-22 22:18:43 +04:00
<Stream>
2022-03-13 01:38:45 +04:00
<Link passHref href='/paroles'>
2020-12-17 23:45:22 +01:00
<IconButton aria-label='return' size='medium'>
<KeyboardBackspaceIcon style={{fontSize: '1.5em'}} />
</IconButton>
</Link>
2022-05-20 02:15:56 +04:00
{parole.streamVideo && parole.streamVideo.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.vwe}>
2022-05-20 02:15:56 +04:00
<VweKouteAchte niVideyo parole={parole} />
2020-12-15 08:50:14 +01:00
</div>
)}
2022-05-22 14:31:41 +04:00
{parole.streamAudio && parole.streamAudio.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.koute}>
2022-05-20 02:15:56 +04:00
<VweKouteAchte niOdyo parole={parole} />
2020-12-15 08:50:14 +01:00
</div>
)}
2022-05-22 22:18:43 +04:00
</Stream>
) : (
<Typography noWrap variant='h6'>
2022-05-23 18:34:55 +04:00
Dernières paroles
</Typography>
)}
</Toolbar>
</AppBar>
2022-05-22 22:18:43 +04:00
<Box
component='nav'
sx={{width: {sm: drawerWidth}, flexShrink: {sm: 0}, padding: 0}}
aria-label='mailbox folders'
>
<Drawer
variant='temporary'
open={mobileOpen}
ModalProps={{
keepMounted: true
}}
sx={{
display: {xs: 'block', sm: 'none'},
'& .MuiDrawer-paper': {boxSizing: 'border-box', width: drawerWidth}
}}
onClose={handleCloseDrawer}
>
{isLoading ? (
<Box sx={{
position: 'absolute',
right: 100,
top: 200,
}}
2022-05-22 22:18:43 +04:00
>
<CircularProgress />
</Box>
) : (
2022-05-23 03:19:46 +04:00
<DrawerBar meteEsMobilOuve={setMobileOpen} paroles={paroles} slug={slug} />
2022-05-22 22:18:43 +04:00
)}
</Drawer>
<Drawer
open
variant='permanent'
sx={{
display: {xs: 'none', sm: 'block'},
'& .MuiDrawer-paper': {boxSizing: 'border-box', width: drawerWidth, marginTop: '3em', borderTop: '2px solid #303030'},
}}
>
{isLoading || isParolesHidden ? (
<>
<MontreTeks handleClick={handleClick} />
{isLoading && (
<Box sx={{width: '100%'}}>
<LinearProgress />
</Box>
)}
</>
) : (
2022-05-23 03:19:46 +04:00
<DrawerBar meteEsMobilOuve={setMobileOpen} paroles={paroles} slug={slug} />
2022-05-22 22:18:43 +04:00
)}
</Drawer>
</Box>
<Box
component='main'
sx={{flexGrow: 1, p: 2, width: {sm: `calc(100% - ${drawerWidth}px)`}}}
>
<Toolbar />
2022-05-20 02:15:56 +04:00
{parole ? (
<Teks
2022-05-20 02:15:56 +04:00
parole={parole}
paroleId={paroleId}
2022-05-22 22:18:43 +04:00
commentaires={parole?.commentaires.data}
open={open}
success={success}
error={error}
setSuccess={setSuccess}
setError={setError}
handleClose={handleClose}
/>
) : (
2022-05-22 22:18:43 +04:00
<DenyeTeks denyeTeks={denyeTeks} />
)}
2022-05-22 22:18:43 +04:00
</Box>
</Box>
)
}
TeksDrawer.defaultProps = {
2022-05-20 02:15:56 +04:00
parole: null,
paroleId: null,
2022-05-22 22:18:43 +04:00
slug: null,
denyeTeks: null
}
2022-05-22 22:18:43 +04:00
TeksDrawer.propTypes = {
parole: PropTypes.object,
paroleId: PropTypes.number,
slug: PropTypes.string,
denyeTeks: PropTypes.array
}