Files
pawol.nu/components/soumet/chwa-teks.js
T
Cédric FAMIBELLE-PRONZOLA 51879ba5aa Replace _id by id
2022-05-05 17:59:15 +04:00

117 lines
3.3 KiB
JavaScript

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'
import {jwennTeksEpiUserId} from '../../lib/oki-api'
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}>
<DialogTitle>Choisir le texte à modifier</DialogTitle>
{loading ? (
<Container sx={{textAlign: 'center', marginBottom: 1}}>
<CircularProgress size={20} sx={{color: '#29d'}} />
</Container>
) : (
<List sx={{pt: 0}}>
{teksLis.length > 0 ? teksLis.map(({id, tit, awtis}) => (
<ListItem key={id} button onClick={() => handleListItemClick(id)}>
<ListItemAvatar>
<Avatar sx={{bgcolor: green[100], color: green[600]}}>
<AudiotrackIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary={`${new Intl.ListFormat('fr').format(awtis.map(({alias}) => alias))} - ${tit}`} />
</ListItem>
)) : (
<Typography sx={{textAlign: 'center'}} variant='button' component='div'>Aucun texte</Typography>
)}
</List>
)}
</Dialog>
)
}
Chwa.defaultProps = {
selectedTeks: null
}
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 {user} = session
const [open, setOpen] = useState(false)
const [teksLis, setTeksLis] = useState([])
const handleClickOpen = async () => {
setOpen(true)
setLoading(true)
const jwennTeks = await jwennTeksEpiUserId(user.id)
setTeksLis(jwennTeks)
setLoading(false)
}
const handleClose = value => {
const teks = teksLis.find(({id}) => id === value)
setSelectedTeks(teks)
setOpen(false)
}
return (
<Container sx={{textAlign: 'center', marginBottom: 1}} maxWidth='sm'>
<Button variant='outlined' onClick={handleClickOpen}>
Modifier mes textes
</Button>
<Chwa
selectedTeks={selectedTeks}
teksLis={teksLis}
open={open}
loading={loading}
onClose={handleClose}
/>
</Container>
)
}
ChwaTeks.defaultProps = {
selectedTeks: null
}
ChwaTeks.propTypes = {
selectedTeks: PropTypes.object,
setSelectedTeks: PropTypes.func.isRequired,
}