Fix EkriTeks and adapt to new properties

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2022-05-20 02:21:38 +04:00
parent 957cd8b41e
commit afbb90ac17
+75 -70
View File
@@ -23,6 +23,8 @@ import {
import MuiAlert from '@mui/material/Alert' import MuiAlert from '@mui/material/Alert'
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff' import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
import {jwennAnTeks} from '../../lib/oki-api'
import AjouteTradiksyon from './ajoute-tradiksyon' import AjouteTradiksyon from './ajoute-tradiksyon'
const PREFIX = 'EkriTeks' const PREFIX = 'EkriTeks'
@@ -108,7 +110,7 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
setError('') setError('')
} }
const handleClick = () => { const handleClick = async () => {
setLoading(true) setLoading(true)
const {awtis, tit, transkripsyon} = teksEkri const {awtis, tit, transkripsyon} = teksEkri
const {fr, en, es, de, it} = tradiksyon const {fr, en, es, de, it} = tradiksyon
@@ -126,81 +128,85 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
} }
if (currentTeksId) { if (currentTeksId) {
axios.get(`${API_URL}/teks/${currentTeksId}?_publicationState=preview&_where[published_at_null]=true`) try {
.then(awtisResponse => { const teks = await jwennAnTeks(currentTeksId, jwt)
if (awtisResponse.data.user.id !== user.id) { const teksResponse = await axios.put(`${API_URL}/paroles/${currentTeksId}`, {
setLoading(false) data: {
return setError({message: 'Opération non autorisée'}) id: teks.id,
} titre: tit,
transcription: transkripsyon,
axios.put(`${API_URL}/teks/${currentTeksId}`, { traductions: {
tit,
transkripsyon,
tradiksyon: {
francais: fr === '' ? null : fr, francais: fr === '' ? null : fr,
english: en === '' ? null : en, anglais: en === '' ? null : en,
espagnol: es === '' ? null : es, espagnol: es === '' ? null : es,
deutsch: de === '' ? null : de, allemand: de === '' ? null : de,
italiano: it === '' ? null : it italien: it === '' ? null : it
}, },
user, user: {
awtis: awtisResponse.data.awtis.map(id => id), id: user.id,
tradiksyonOtomatik username: user.username,
email: user.email
},
artistes: teks.artistes.map(({id}) => id),
traductionAuto: tradiksyonOtomatik
}
}, { }, {
headers headers
}) })
.then(teksResponse => {
const {data} = teksResponse const {data} = teksResponse
setSuccess(`Le texte "${data.tit}" a été modifié avec succès. Il apparaîtra sur le site après validation.`) setSuccess(`Le texte "${data.tit}" a été modifié avec succès. Il apparaîtra sur le site après validation.`)
setLoading(false) setLoading(false)
}) } catch (error) {
.catch(error => { setError(error?.response?.data)
setError(error)
setLoading(false) setLoading(false)
}) }
})
.catch(error => {
setError(error)
setLoading(false)
})
} else { } else {
axios.post(`${API_URL}/awtis`, { try {
const artiste = await axios.post(`${API_URL}/artistes`, {
data: {
alias: awtis, alias: awtis,
user user: {
id: user.id,
username: user.username,
email: user.email
}
}
}, { }, {
headers headers
}) })
.then(awtisResponse => {
axios.post(`${API_URL}/teks`, { const parole = await axios.post(`${API_URL}/paroles`, {
tit, data: {
transkripsyon, titre: tit,
tradiksyon: { transcription: transkripsyon,
traductions: {
francais: fr === '' ? null : fr, francais: fr === '' ? null : fr,
english: en === '' ? null : en, anglais: en === '' ? null : en,
espagnol: es === '' ? null : es, espagnol: es === '' ? null : es,
deutsch: de === '' ? null : de, allemand: de === '' ? null : de,
italiano: it === '' ? null : it italien: it === '' ? null : it
}, },
user, user: {
awtis: [awtisResponse.data.id], id: user.id,
tradiksyonOtomatik username: user.username,
email: user.email
},
artistes: [artiste.data.id],
traductionAuto: tradiksyonOtomatik
}
}, { }, {
headers headers
}) })
.then(teksResponse => {
const {data} = teksResponse const {data} = parole
setSuccess(`Le texte "${data.tit}" a été soumis avec succès. Il apparaîtra sur le site après validation.`)
setSuccess(`Le texte "${data.titre}" a été soumis avec succès. Il apparaîtra sur le site après validation.`)
setLoading(false) setLoading(false)
}) } catch (error) {
.catch(error => { setError(error?.response?.data)
setError(error)
setLoading(false) setLoading(false)
}) }
})
.catch(error => {
setError(error)
setLoading(false)
})
} }
} }
@@ -230,39 +236,38 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
setTradiksyonOtomatik(false) setTradiksyonOtomatik(false)
setCurrentTeksId(selectedTeks.id) setCurrentTeksId(selectedTeks.id)
setTeksEkri({ setTeksEkri({
awtis: new Intl.ListFormat('fr').format(selectedTeks.awtis.map(({alias}) => alias)), awtis: new Intl.ListFormat('fr').format(selectedTeks.artistes.map(({alias}) => alias)),
tit: selectedTeks.tit, tit: selectedTeks.titre,
transkripsyon: selectedTeks.transkripsyon transkripsyon: selectedTeks.transcription
}) })
setTradiksyon({ setTradiksyon({
fr: selectedTeks.tradiksyon.francais || '', fr: selectedTeks?.traductions?.francais || '',
en: selectedTeks.tradiksyon.english || '', en: selectedTeks?.traductions?.anglais || '',
es: selectedTeks.tradiksyon.espagnol || '', es: selectedTeks?.traductions?.espagnol || '',
de: selectedTeks.tradiksyon.deutsch || '', de: selectedTeks?.traductions?.allemand || '',
it: selectedTeks.tradiksyon.italiano || '' it: selectedTeks?.traductions?.italien || ''
}) })
const jennLangId = () => { const jennLangId = () => {
const ids = [] const ids = []
const {francais, english, espagnol, deutsch, italiano} = selectedTeks.tradiksyon
if (francais) { if (selectedTeks?.traductions?.francais) {
ids.push('fr') ids.push('fr')
} }
if (english) { if (selectedTeks?.traductions?.anglais) {
ids.push('en') ids.push('en')
} }
if (espagnol) { if (selectedTeks?.traductions?.espagnol) {
ids.push('es') ids.push('es')
} }
if (deutsch) { if (selectedTeks?.traductions?.allemand) {
ids.push('de') ids.push('de')
} }
if (italiano) { if (selectedTeks?.traductions?.italien) {
ids.push('it') ids.push('it')
} }
@@ -369,7 +374,7 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
</form> </form>
<AjouteTradiksyon <AjouteTradiksyon
showSwitch={Boolean(canAutoTranslate) && Boolean(!currentTeksId)} showSwitch={Boolean(canAutoTranslate) && Boolean(!currentTeksId)}
disableSwitch={tradiksyon.fr === ''} disableSwitch={tradiksyon.fr === '' || tradiksyon.en !== '' || tradiksyon.es !== '' || tradiksyon.de !== '' || tradiksyon.it !== ''}
tradiksyonOtomatik={tradiksyonOtomatik} tradiksyonOtomatik={tradiksyonOtomatik}
setTradiksyonOtomatik={setTradiksyonOtomatik} setTradiksyonOtomatik={setTradiksyonOtomatik}
chwaLang={kiChawLang} chwaLang={kiChawLang}
@@ -410,9 +415,9 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
</Snackbar> </Snackbar>
)} )}
{error && ( {error && (
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}> <Snackbar open={open} autoHideDuration={10_000} onClose={handleClose}>
<Alert severity='error' onClose={handleClose}> <Alert severity='error' onClose={handleClose}>
<strong>Une erreur sest produite</strong> : <i>{error.message}</i> <strong>Une erreur sest produite</strong> : <i>{error?.error?.message}</i>
</Alert> </Alert>
</Snackbar> </Snackbar>
)} )}