Files
pawol.nu/components/soumet/chwa-teks.js
T

110 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-03-25 00:05:49 +04:00
import {useState} from 'react'
import {useSession} from 'next-auth/react'
import PropTypes from 'prop-types'
import Button from '@mui/material/Button'
import Avatar from '@mui/material/Avatar'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListItemAvatar from '@mui/material/ListItemAvatar'
import ListItemText from '@mui/material/ListItemText'
import DialogTitle from '@mui/material/DialogTitle'
import Dialog from '@mui/material/Dialog'
import {green} from '@mui/material/colors'
import Container from '@mui/material/Container'
import CircularProgress from '@mui/material/CircularProgress'
import Typography from '@mui/material/Typography'
import AudiotrackIcon from '@mui/icons-material/Audiotrack'
2022-05-20 02:18:16 +04:00
import {jwennUserEpiToken} from '../../lib/oki-api'
2022-03-25 00:05:49 +04:00
function Chwa(props) {
const {onClose, teksLis, selectedTeks, loading, open} = props
const handleClose = () => {
onClose(selectedTeks)
}
const handleListItemClick = value => {
onClose(value)
}
return (
<Dialog open={open} onClose={handleClose}>
2022-05-23 18:34:55 +04:00
<DialogTitle>Choisir la parole à modifier</DialogTitle>
2022-03-25 00:05:49 +04:00
{loading ? (
2022-03-26 13:08:37 +04:00
<Container sx={{textAlign: 'center', marginBottom: 1}}>
<CircularProgress size={20} sx={{color: '#29d'}} />
2022-03-25 00:16:58 +04:00
</Container>
2022-03-25 00:05:49 +04:00
) : (
<List sx={{pt: 0}}>
2022-05-20 02:18:16 +04:00
{teksLis.length > 0 ? teksLis.map(({id, titre, artistes}) => (
2022-05-05 17:58:05 +04:00
<ListItem key={id} button onClick={() => handleListItemClick(id)}>
2022-03-25 00:05:49 +04:00
<ListItemAvatar>
<Avatar sx={{bgcolor: green[100], color: green[600]}}>
<AudiotrackIcon />
</Avatar>
</ListItemAvatar>
2022-05-20 02:18:16 +04:00
<ListItemText primary={`${new Intl.ListFormat('fr').format(artistes.map(({alias}) => alias))} - ${titre}`} />
2022-03-25 00:05:49 +04:00
</ListItem>
)) : (
<Typography sx={{textAlign: 'center'}} variant='button' component='div'>Aucun texte</Typography>
)}
</List>
)}
</Dialog>
)
}
Chwa.propTypes = {
onClose: PropTypes.func.isRequired,
teksLis: PropTypes.array.isRequired,
loading: PropTypes.bool.isRequired,
open: PropTypes.bool.isRequired,
selectedTeks: PropTypes.object,
}
export default function ChwaTeks({selectedTeks, setSelectedTeks}) {
const {data: session} = useSession()
const [loading, setLoading] = useState(false)
const [open, setOpen] = useState(false)
const [teksLis, setTeksLis] = useState([])
const handleClickOpen = async () => {
setOpen(true)
setLoading(true)
2022-05-20 02:18:16 +04:00
const user = await jwennUserEpiToken(session?.jwt)
const {paroles} = user
const parolesList = paroles && paroles.length > 0 ? paroles : []
2022-03-25 00:05:49 +04:00
2022-05-20 02:18:16 +04:00
setTeksLis(parolesList)
2022-03-25 00:05:49 +04:00
setLoading(false)
}
const handleClose = value => {
2022-05-05 17:58:05 +04:00
const teks = teksLis.find(({id}) => id === value)
2022-03-25 00:05:49 +04:00
setSelectedTeks(teks)
setOpen(false)
}
return (
2022-03-27 05:20:30 +04:00
<Container sx={{textAlign: 'center', marginBottom: 1}} maxWidth='sm'>
2022-03-25 00:05:49 +04:00
<Button variant='outlined' onClick={handleClickOpen}>
2022-05-23 18:34:55 +04:00
Modifier mes paroles
2022-03-25 00:05:49 +04:00
</Button>
<Chwa
selectedTeks={selectedTeks}
teksLis={teksLis}
open={open}
loading={loading}
onClose={handleClose}
/>
</Container>
)
}
ChwaTeks.propTypes = {
selectedTeks: PropTypes.object,
setSelectedTeks: PropTypes.func.isRequired,
}