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

340 lines
9.0 KiB
JavaScript
Raw Normal View History

2021-06-02 02:24:19 +02:00
import {useEffect, useState} from 'react'
import PropTypes from 'prop-types'
import Link from 'next/link'
import {
Grid,
Toolbar,
Typography,
AppBar,
CssBaseline,
Drawer,
Hidden,
2021-05-26 02:49:06 +02:00
IconButton,
2021-05-30 11:56:27 +02:00
Box,
2021-06-02 02:24:19 +02:00
useMediaQuery,
Snackbar
} from '@material-ui/core'
import KeyboardBackspaceIcon from '@material-ui/icons/KeyboardBackspace'
2021-05-28 17:03:27 +02:00
import ExplicitIcon from '@material-ui/icons/Explicit'
import MenuIcon from '@material-ui/icons/Menu'
import {makeStyles, useTheme} from '@material-ui/core/styles'
import DrawerBar from './drawer-bar'
import DenyeTeks from './denye-teks'
2020-12-15 08:50:14 +01:00
import VweKouteAchte from './vwe-koute-achte'
2021-01-12 21:58:08 +01:00
import OkiMizik from './oki-mizik'
2021-06-02 02:24:19 +02:00
import Pataje from './pataje'
import MuiAlert from '@material-ui/lab/Alert'
function Alert(props) {
return <MuiAlert elevation={6} variant='filled' {...props} />
}
const drawerWidth = 240
const useStyles = makeStyles(theme => ({
root: {
display: 'flex'
},
drawer: {
marginTop: '10em',
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0
}
},
appBar: {
borderTop: '2px solid #303030',
marginTop: '4.71rem',
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth
}
},
menuButton: {
[theme.breakpoints.up('sm')]: {
display: 'none'
}
},
toolbar: theme.mixins.toolbar,
drawerPaper: {
borderTop: '2px solid #303030',
marginTop: '4.71rem',
width: drawerWidth
},
content: {
flexGrow: 1,
padding: theme.spacing(3)
},
list: {
marginBottom: '6em'
},
form: {
marginLeft: theme.spacing(1)
},
text: {
marginBottom: '0.5em'
},
gridText: {
2021-05-27 03:28:40 +02:00
border: '2px solid grey',
borderRadius: '5px',
marginTop: '2em',
2021-05-27 03:28:40 +02:00
padding: '1em'
},
grid: {
marginTop: '1em'
2020-12-15 08:50:14 +01:00
},
koute: {
position: 'absolute',
2020-12-17 23:44:01 +01:00
right: '1px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
},
vwe: {
position: 'absolute',
2020-12-17 23:44:01 +01:00
right: '60px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
2021-06-02 02:24:19 +02:00
},
pataje: {
position: 'absolute',
top: '140px',
left: '10px',
[theme.breakpoints.up('sm')]: {
left: '275px'
}
}
}))
const formatJsonString = stringToFormat => {
return stringToFormat.split('\n').map((string, index) => <div key={index}>{`${string}`}<br /></div>) // eslint-disable-line react/no-array-index-key
}
2021-05-27 03:28:40 +02:00
const langToArray = anTeks => {
const langArray = []
if (anTeks && anTeks.tradiksyon) {
const {francais, english, espagnol} = anTeks.tradiksyon
if (francais) {
langArray.push({title: 'Traduction', emoji: '&#127467;&#127479;', lang: francais})
}
if (english) {
langArray.push({title: 'Translation', emoji: '&#127468;&#127463;', lang: english})
}
if (espagnol) {
langArray.push({title: 'Traducción', emoji: '&#127466;&#127480;', lang: espagnol})
}
}
return langArray
}
2021-05-30 16:59:56 +02:00
const alignTeks = (langArray, isMobile) => {
if (langArray.length > 0 && !isMobile) {
return 'justify'
}
if (langArray.length > 0 && isMobile) {
return 'justify'
}
if (langArray.length === 0 && isMobile) {
return 'justify'
}
if (langArray.length === 0 && !isMobile) {
return 'center'
}
}
2020-12-18 22:08:34 +01:00
export default function TeksDrawer({teks, anTeks}) {
2021-05-30 11:56:27 +02:00
const isMobile = useMediaQuery('(max-width:800px)')
2021-05-27 03:28:40 +02:00
const langArray = langToArray(anTeks)
const classes = useStyles()
const theme = useTheme()
2020-12-18 22:21:53 +01:00
const [esMobilOuve, meteEsMobilOuve] = useState(false)
2021-06-02 02:24:19 +02:00
const [open, setOpen] = useState(false)
const [error, setError] = useState('')
const [success, setSuccess] = useState('')
const handleClose = (event, reason) => {
if (reason === 'clickaway') {
return
}
setOpen(false)
setSuccess('')
setError('')
}
const handleDrawerToggle = () => {
2020-12-18 22:21:53 +01:00
meteEsMobilOuve(!esMobilOuve)
}
const container = typeof window === 'undefined' ? undefined : () => window.document.body
2021-06-02 02:24:19 +02:00
useEffect(() => {
if (error || success) {
setOpen(true)
}
}, [error, success, setOpen])
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position='fixed' className={classes.appBar}>
<Toolbar>
<IconButton
color='inherit'
aria-label='open drawer'
edge='start'
className={classes.menuButton}
onClick={handleDrawerToggle}
>
<MenuIcon />
</IconButton>
2020-12-18 22:08:34 +01:00
{anTeks ? (
<>
<Link href='/teks'>
2020-12-17 23:45:22 +01:00
<IconButton aria-label='return' size='medium'>
<KeyboardBackspaceIcon style={{fontSize: '1.5em'}} />
</IconButton>
</Link>
2020-12-18 22:08:34 +01:00
{anTeks.lyen && anTeks.lyen.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.vwe}>
2020-12-18 22:08:34 +01:00
<VweKouteAchte niVideyo anTeks={anTeks} />
2020-12-15 08:50:14 +01:00
</div>
)}
2020-12-18 22:08:34 +01:00
{anTeks.kouteyAchtey && anTeks.kouteyAchtey.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.koute}>
2020-12-18 22:08:34 +01:00
<VweKouteAchte niOdyo anTeks={anTeks} />
2020-12-15 08:50:14 +01:00
</div>
)}
</>
) : (
<Typography noWrap variant='h6'>
Dènyé Tèks
</Typography>
)}
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label='mailbox folders'>
<Hidden smUp implementation='css'>
<Drawer
container={container}
variant='temporary'
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
2020-12-18 22:21:53 +01:00
open={esMobilOuve}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
onClose={handleDrawerToggle}
>
2020-12-18 22:21:53 +01:00
<DrawerBar meteEsMobilOuve={meteEsMobilOuve} teks={teks} anTeks={anTeks} />
</Drawer>
</Hidden>
<Hidden xsDown implementation='css'>
<Drawer
open
classes={{
paper: classes.drawerPaper
}}
variant='permanent'
>
2020-12-18 22:08:34 +01:00
<DrawerBar teks={teks} anTeks={anTeks} />
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
2020-12-18 22:08:34 +01:00
{anTeks ? (
2020-12-17 23:47:15 +01:00
<>
2021-05-26 02:49:06 +02:00
<Box align='center'>
<Typography noWrap style={{marginTop: '0.8em'}} variant='h4'>
{anTeks.tit}
2021-05-28 17:03:27 +02:00
{anTeks.eksplisit && (
<ExplicitIcon style={{marginLeft: 10}} color='secondary' />
)}
2021-06-02 02:24:19 +02:00
<div className={classes.pataje}>
<Pataje teks={anTeks} setError={setError} setSuccess={setSuccess} />
</div>
2021-05-26 02:49:06 +02:00
</Typography>
<Typography variant='caption'>
{anTeks.user && (
2021-06-02 19:21:54 +02:00
<i>texte soumis par {anTeks.user.username}</i>
2021-05-26 02:49:06 +02:00
)}
</Typography>
</Box>
2021-01-12 21:58:08 +01:00
{anTeks.okiMizikID && (
<OkiMizik id={anTeks.okiMizikID} />
)}
2021-05-27 03:28:40 +02:00
<Grid container justify='center' spacing={1}>
<Grid item xs={12} md={langArray.length > 0 ? 6 : null}>
<div className={classes.gridText}>
2020-12-17 23:47:15 +01:00
<Typography align='center' className={classes.text} variant='h4'>
2021-05-27 03:28:40 +02:00
Transcription
2020-12-17 23:47:15 +01:00
</Typography>
2021-05-30 16:59:56 +02:00
<Typography paragraph align={alignTeks(langArray, isMobile)} component='span'>
2021-05-27 03:28:40 +02:00
{formatJsonString(anTeks.transkripsyon)}
2020-12-17 23:47:15 +01:00
</Typography>
2021-05-27 03:28:40 +02:00
</div>
</Grid>
{langArray.map(({title, emoji, lang}) => (
<Grid key={title} item xs={12} md={6}>
<div className={classes.gridText}>
<Typography align='center' className={classes.text} variant='h4'>
<span dangerouslySetInnerHTML={{__html: emoji}} /> {title}
</Typography>
<Typography paragraph align='justify' component='span'>
{formatJsonString(lang)}
</Typography>
</div>
2020-12-17 23:47:15 +01:00
</Grid>
2021-05-27 03:28:40 +02:00
))}
2020-12-17 23:47:15 +01:00
</Grid>
2021-06-02 02:24:19 +02:00
{success && (
<Snackbar open={open} autoHideDuration={3000} onClose={handleClose}>
<Alert severity='success' onClose={handleClose}>
<strong>{success}</strong>
</Alert>
</Snackbar>
)}
{error && (
<Snackbar open={open} autoHideDuration={3000} onClose={handleClose}>
<Alert severity='error' onClose={handleClose}>
<strong>Une erreur sest produite</strong> : <i>{error.message}</i>
</Alert>
</Snackbar>
)}
2020-12-17 23:47:15 +01:00
</>
) : (
2020-12-18 22:08:34 +01:00
<DenyeTeks {...teks} />
)}
</main>
</div>
)
}
TeksDrawer.propTypes = {
2020-12-18 22:08:34 +01:00
teks: PropTypes.array.isRequired,
anTeks: PropTypes.object
}
TeksDrawer.defaultProps = {
2020-12-18 22:08:34 +01:00
anTeks: null
}