Adapt EkriTeks component to ChwaTeks
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import {useCallback, useEffect, forwardRef, useState} from 'react'
|
import {useCallback, useEffect, forwardRef, useState} from 'react'
|
||||||
import {styled} from '@mui/material/styles'
|
import {styled} from '@mui/material/styles'
|
||||||
|
import PropTypes from 'prop-types'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import {useSession} from 'next-auth/react'
|
import {useSession} from 'next-auth/react'
|
||||||
import {
|
import {
|
||||||
@@ -20,7 +21,7 @@ import {
|
|||||||
Typography
|
Typography
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import MuiAlert from '@mui/material/Alert'
|
import MuiAlert from '@mui/material/Alert'
|
||||||
import CloseIcon from '@mui/icons-material/Close'
|
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
|
||||||
|
|
||||||
import AjouteTradiksyon from './ajoute-tradiksyon'
|
import AjouteTradiksyon from './ajoute-tradiksyon'
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ const Alert = forwardRef(function Alert(props, ref) {
|
|||||||
|
|
||||||
const RemoveTooltip = Tooltip
|
const RemoveTooltip = Tooltip
|
||||||
|
|
||||||
function EkriTeks() {
|
function EkriTeks({selectedTeks, setSelectedTeks}) {
|
||||||
const {data: session} = useSession()
|
const {data: session} = useSession()
|
||||||
const {jwt, user} = session
|
const {jwt, user} = session
|
||||||
const [teksEkri, setTeksEkri] = useState({awtis: '', tit: '', transkripsyon: ''})
|
const [teksEkri, setTeksEkri] = useState({awtis: '', tit: '', transkripsyon: ''})
|
||||||
@@ -81,6 +82,7 @@ function EkriTeks() {
|
|||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
const [success, setSuccess] = useState('')
|
const [success, setSuccess] = useState('')
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [currentTeksId, setCurrentTeksId] = useState(null)
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
const handleUpdate = useCallback(update => {
|
const handleUpdate = useCallback(update => {
|
||||||
@@ -93,8 +95,7 @@ function EkriTeks() {
|
|||||||
|
|
||||||
const handleRemoveTradiksyon = useCallback(idTradiksyon => {
|
const handleRemoveTradiksyon = useCallback(idTradiksyon => {
|
||||||
setKiChwalang(kiChawLang.filter(t => t !== idTradiksyon))
|
setKiChwalang(kiChawLang.filter(t => t !== idTradiksyon))
|
||||||
setTradiksyon({[tradiksyon.idTradiksyon]: ''})
|
}, [kiChawLang, setKiChwalang])
|
||||||
}, [kiChawLang, setKiChwalang, setTradiksyon, tradiksyon])
|
|
||||||
|
|
||||||
const handleClose = (event, reason) => {
|
const handleClose = (event, reason) => {
|
||||||
if (reason === 'clickaway') {
|
if (reason === 'clickaway') {
|
||||||
@@ -123,6 +124,44 @@ function EkriTeks() {
|
|||||||
Authorization: `Bearer ${jwt}`
|
Authorization: `Bearer ${jwt}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentTeksId) {
|
||||||
|
axios.get(`${API_URL}/teks/${currentTeksId}?_publicationState=preview&_where[published_at_null]=true`)
|
||||||
|
.then(awtisResponse => {
|
||||||
|
if (awtisResponse.data.user._id !== user._id) {
|
||||||
|
setLoading(false)
|
||||||
|
return setError({message: 'Opération non autorisé'})
|
||||||
|
}
|
||||||
|
|
||||||
|
axios.put(`${API_URL}/teks/${currentTeksId}`, {
|
||||||
|
tit,
|
||||||
|
transkripsyon,
|
||||||
|
tradiksyon: {
|
||||||
|
francais: fr === '' ? null : fr,
|
||||||
|
english: en === '' ? null : en,
|
||||||
|
espagnol: es === '' ? null : es,
|
||||||
|
deutsch: de === '' ? null : de,
|
||||||
|
italiano: it === '' ? null : it
|
||||||
|
},
|
||||||
|
user,
|
||||||
|
awtis: awtisResponse.data.awtis.map(id => id)
|
||||||
|
}, {
|
||||||
|
headers
|
||||||
|
})
|
||||||
|
.then(teksResponse => {
|
||||||
|
const {data} = teksResponse
|
||||||
|
setSuccess(`Le texte "${data.tit}" a été modifié avec succès. Il apparaîtra sur le site après validation.`)
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
setError(error)
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
setError(error)
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
axios.post(`${API_URL}/awtis`, {
|
axios.post(`${API_URL}/awtis`, {
|
||||||
alias: awtis,
|
alias: awtis,
|
||||||
user
|
user
|
||||||
@@ -160,11 +199,13 @@ function EkriTeks() {
|
|||||||
setLoading(false)
|
setLoading(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleReset = () => {
|
const handleReset = () => {
|
||||||
setTeksEkri({awtis: '', tit: '', transkripsyon: ''})
|
setTeksEkri({awtis: '', tit: '', transkripsyon: ''})
|
||||||
setTradiksyon({fr: '', en: '', es: '', de: '', it: ''})
|
setTradiksyon({fr: '', en: '', es: '', de: '', it: ''})
|
||||||
setKiChwalang([])
|
setKiChwalang([])
|
||||||
|
setCurrentTeksId(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -180,6 +221,59 @@ function EkriTeks() {
|
|||||||
}
|
}
|
||||||
}, [error])
|
}, [error])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedTeks) {
|
||||||
|
setCurrentTeksId(selectedTeks._id)
|
||||||
|
setTeksEkri({
|
||||||
|
awtis: new Intl.ListFormat('fr').format(selectedTeks.awtis.map(({alias}) => alias)),
|
||||||
|
tit: selectedTeks.tit,
|
||||||
|
transkripsyon: selectedTeks.transkripsyon
|
||||||
|
})
|
||||||
|
setTradiksyon({
|
||||||
|
fr: selectedTeks.tradiksyon.francais || '',
|
||||||
|
en: selectedTeks.tradiksyon.english || '',
|
||||||
|
es: selectedTeks.tradiksyon.espagnol || '',
|
||||||
|
de: selectedTeks.tradiksyon.deutsch || '',
|
||||||
|
it: selectedTeks.tradiksyon.italiano || ''
|
||||||
|
})
|
||||||
|
|
||||||
|
const jennLangId = () => {
|
||||||
|
const ids = []
|
||||||
|
const {francais, english, espagnol, deutsch, italiano} = selectedTeks.tradiksyon
|
||||||
|
|
||||||
|
if (francais) {
|
||||||
|
ids.push('fr')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (english) {
|
||||||
|
ids.push('en')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (espagnol) {
|
||||||
|
ids.push('es')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deutsch) {
|
||||||
|
ids.push('de')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (italiano) {
|
||||||
|
ids.push('it')
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
const langIds = jennLangId()
|
||||||
|
|
||||||
|
setKiChwalang(langIds)
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
setSelectedTeks(null)
|
||||||
|
}
|
||||||
|
}, [teksEkri, selectedTeks, setSelectedTeks, kiChawLang])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledContainer maxWidth='sm'>
|
<StyledContainer maxWidth='sm'>
|
||||||
<Box sx={{textAlign: 'center', marginBottom: 2}}>
|
<Box sx={{textAlign: 'center', marginBottom: 2}}>
|
||||||
@@ -196,6 +290,7 @@ function EkriTeks() {
|
|||||||
name='awtis'
|
name='awtis'
|
||||||
label='Artiste'
|
label='Artiste'
|
||||||
value={teksEkri.awtis}
|
value={teksEkri.awtis}
|
||||||
|
disabled={Boolean(currentTeksId)}
|
||||||
variant='outlined'
|
variant='outlined'
|
||||||
helperText='Nom de l’artiste (obligatoire)'
|
helperText='Nom de l’artiste (obligatoire)'
|
||||||
onChange={event => handleUpdate({awtis: event.target.value})}
|
onChange={event => handleUpdate({awtis: event.target.value})}
|
||||||
@@ -207,7 +302,7 @@ function EkriTeks() {
|
|||||||
id='tit'
|
id='tit'
|
||||||
name='tit'
|
name='tit'
|
||||||
label='Titre'
|
label='Titre'
|
||||||
value={teksEkri.tit}
|
value={selectedTeks?.tit || teksEkri.tit}
|
||||||
variant='outlined'
|
variant='outlined'
|
||||||
helperText='Titre de la musique (obligatoire)'
|
helperText='Titre de la musique (obligatoire)'
|
||||||
onChange={event => handleUpdate({tit: event.target.value})}
|
onChange={event => handleUpdate({tit: event.target.value})}
|
||||||
@@ -243,21 +338,20 @@ function EkriTeks() {
|
|||||||
endAdornment={
|
endAdornment={
|
||||||
<InputAdornment position='end'>
|
<InputAdornment position='end'>
|
||||||
<RemoveTooltip
|
<RemoveTooltip
|
||||||
title='Effacer la traduction'
|
title='Cacher la fenêtre'
|
||||||
placement='left'
|
placement='left'
|
||||||
classes={{
|
classes={{
|
||||||
tooltip: classes.tooltip
|
tooltip: classes.tooltip
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<IconButton
|
<IconButton
|
||||||
color='secondary'
|
|
||||||
style={{bottom: 75}}
|
style={{bottom: 75}}
|
||||||
aria-label='remove-tradiksyon'
|
aria-label='remove-tradiksyon'
|
||||||
edge='end'
|
edge='end'
|
||||||
size='large'
|
size='large'
|
||||||
onClick={() => handleRemoveTradiksyon(k)}
|
onClick={() => handleRemoveTradiksyon(k)}
|
||||||
>
|
>
|
||||||
<CloseIcon />
|
<VisibilityOffIcon />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</RemoveTooltip>
|
</RemoveTooltip>
|
||||||
</InputAdornment>
|
</InputAdornment>
|
||||||
@@ -273,7 +367,7 @@ function EkriTeks() {
|
|||||||
<Button
|
<Button
|
||||||
fullWidth
|
fullWidth
|
||||||
disabled={loading || teksEkri.awtis === '' || teksEkri.tit === '' || teksEkri.transkripsyon === ''}
|
disabled={loading || teksEkri.awtis === '' || teksEkri.tit === '' || teksEkri.transkripsyon === ''}
|
||||||
style={{marginBlock: 20}}
|
style={{marginTop: 20}}
|
||||||
variant='contained'
|
variant='contained'
|
||||||
color='primary'
|
color='primary'
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
@@ -282,6 +376,18 @@ function EkriTeks() {
|
|||||||
Valider
|
Valider
|
||||||
</Typography>
|
</Typography>
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
fullWidth
|
||||||
|
disabled={loading || (teksEkri.awtis === '' && teksEkri.tit === '' && teksEkri.transkripsyon === '')}
|
||||||
|
style={{marginTop: 20}}
|
||||||
|
variant='contained'
|
||||||
|
color='secondary'
|
||||||
|
onClick={handleReset}
|
||||||
|
>
|
||||||
|
<Typography style={{fontWeight: 'bold'}}>
|
||||||
|
Tout effacer
|
||||||
|
</Typography>
|
||||||
|
</Button>
|
||||||
{loading && <LinearProgress size={24} style={{width: '100%', marginBlock: '1em'}} />}
|
{loading && <LinearProgress size={24} style={{width: '100%', marginBlock: '1em'}} />}
|
||||||
</div>
|
</div>
|
||||||
{success && (
|
{success && (
|
||||||
@@ -302,4 +408,13 @@ function EkriTeks() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EkriTeks.defaultProps = {
|
||||||
|
selectedTeks: null
|
||||||
|
}
|
||||||
|
|
||||||
|
EkriTeks.propTypes = {
|
||||||
|
selectedTeks: PropTypes.object,
|
||||||
|
setSelectedTeks: PropTypes.func.isRequired,
|
||||||
|
}
|
||||||
|
|
||||||
export default EkriTeks
|
export default EkriTeks
|
||||||
|
|||||||
Reference in New Issue
Block a user