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 VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
import {jwennAnTeks} from '../../lib/oki-api'
import AjouteTradiksyon from './ajoute-tradiksyon'
const PREFIX = 'EkriTeks'
@@ -108,7 +110,7 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
setError('')
}
const handleClick = () => {
const handleClick = async () => {
setLoading(true)
const {awtis, tit, transkripsyon} = teksEkri
const {fr, en, es, de, it} = tradiksyon
@@ -126,81 +128,85 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
}
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ée'})
}
axios.put(`${API_URL}/teks/${currentTeksId}`, {
tit,
transkripsyon,
tradiksyon: {
try {
const teks = await jwennAnTeks(currentTeksId, jwt)
const teksResponse = await axios.put(`${API_URL}/paroles/${currentTeksId}`, {
data: {
id: teks.id,
titre: tit,
transcription: transkripsyon,
traductions: {
francais: fr === '' ? null : fr,
english: en === '' ? null : en,
anglais: en === '' ? null : en,
espagnol: es === '' ? null : es,
deutsch: de === '' ? null : de,
italiano: it === '' ? null : it
allemand: de === '' ? null : de,
italien: it === '' ? null : it
},
user,
awtis: awtisResponse.data.awtis.map(id => id),
tradiksyonOtomatik
user: {
id: user.id,
username: user.username,
email: user.email
},
artistes: teks.artistes.map(({id}) => id),
traductionAuto: tradiksyonOtomatik
}
}, {
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)
} catch (error) {
setError(error?.response?.data)
setLoading(false)
})
})
.catch(error => {
setError(error)
setLoading(false)
})
}
} else {
axios.post(`${API_URL}/awtis`, {
try {
const artiste = await axios.post(`${API_URL}/artistes`, {
data: {
alias: awtis,
user
user: {
id: user.id,
username: user.username,
email: user.email
}
}
}, {
headers
})
.then(awtisResponse => {
axios.post(`${API_URL}/teks`, {
tit,
transkripsyon,
tradiksyon: {
const parole = await axios.post(`${API_URL}/paroles`, {
data: {
titre: tit,
transcription: transkripsyon,
traductions: {
francais: fr === '' ? null : fr,
english: en === '' ? null : en,
anglais: en === '' ? null : en,
espagnol: es === '' ? null : es,
deutsch: de === '' ? null : de,
italiano: it === '' ? null : it
allemand: de === '' ? null : de,
italien: it === '' ? null : it
},
user,
awtis: [awtisResponse.data.id],
tradiksyonOtomatik
user: {
id: user.id,
username: user.username,
email: user.email
},
artistes: [artiste.data.id],
traductionAuto: tradiksyonOtomatik
}
}, {
headers
})
.then(teksResponse => {
const {data} = teksResponse
setSuccess(`Le texte "${data.tit}" a été soumis avec succès. Il apparaîtra sur le site après validation.`)
const {data} = parole
setSuccess(`Le texte "${data.titre}" a été soumis avec succès. Il apparaîtra sur le site après validation.`)
setLoading(false)
})
.catch(error => {
setError(error)
} catch (error) {
setError(error?.response?.data)
setLoading(false)
})
})
.catch(error => {
setError(error)
setLoading(false)
})
}
}
}
@@ -230,39 +236,38 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
setTradiksyonOtomatik(false)
setCurrentTeksId(selectedTeks.id)
setTeksEkri({
awtis: new Intl.ListFormat('fr').format(selectedTeks.awtis.map(({alias}) => alias)),
tit: selectedTeks.tit,
transkripsyon: selectedTeks.transkripsyon
awtis: new Intl.ListFormat('fr').format(selectedTeks.artistes.map(({alias}) => alias)),
tit: selectedTeks.titre,
transkripsyon: selectedTeks.transcription
})
setTradiksyon({
fr: selectedTeks.tradiksyon.francais || '',
en: selectedTeks.tradiksyon.english || '',
es: selectedTeks.tradiksyon.espagnol || '',
de: selectedTeks.tradiksyon.deutsch || '',
it: selectedTeks.tradiksyon.italiano || ''
fr: selectedTeks?.traductions?.francais || '',
en: selectedTeks?.traductions?.anglais || '',
es: selectedTeks?.traductions?.espagnol || '',
de: selectedTeks?.traductions?.allemand || '',
it: selectedTeks?.traductions?.italien || ''
})
const jennLangId = () => {
const ids = []
const {francais, english, espagnol, deutsch, italiano} = selectedTeks.tradiksyon
if (francais) {
if (selectedTeks?.traductions?.francais) {
ids.push('fr')
}
if (english) {
if (selectedTeks?.traductions?.anglais) {
ids.push('en')
}
if (espagnol) {
if (selectedTeks?.traductions?.espagnol) {
ids.push('es')
}
if (deutsch) {
if (selectedTeks?.traductions?.allemand) {
ids.push('de')
}
if (italiano) {
if (selectedTeks?.traductions?.italien) {
ids.push('it')
}
@@ -369,7 +374,7 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
</form>
<AjouteTradiksyon
showSwitch={Boolean(canAutoTranslate) && Boolean(!currentTeksId)}
disableSwitch={tradiksyon.fr === ''}
disableSwitch={tradiksyon.fr === '' || tradiksyon.en !== '' || tradiksyon.es !== '' || tradiksyon.de !== '' || tradiksyon.it !== ''}
tradiksyonOtomatik={tradiksyonOtomatik}
setTradiksyonOtomatik={setTradiksyonOtomatik}
chwaLang={kiChawLang}
@@ -410,9 +415,9 @@ function EkriTeks({canAutoTranslate, selectedTeks, setSelectedTeks}) {
</Snackbar>
)}
{error && (
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
<Snackbar open={open} autoHideDuration={10_000} 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>
</Snackbar>
)}