Fix EkriTeks and adapt to new properties
This commit is contained in:
@@ -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'})
|
||||
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,
|
||||
anglais: en === '' ? null : en,
|
||||
espagnol: es === '' ? null : es,
|
||||
allemand: de === '' ? null : de,
|
||||
italien: it === '' ? null : it
|
||||
},
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email
|
||||
},
|
||||
artistes: teks.artistes.map(({id}) => id),
|
||||
traductionAuto: tradiksyonOtomatik
|
||||
}
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
|
||||
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),
|
||||
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)
|
||||
setLoading(false)
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
})
|
||||
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?.response?.data)
|
||||
setLoading(false)
|
||||
}
|
||||
} else {
|
||||
axios.post(`${API_URL}/awtis`, {
|
||||
alias: awtis,
|
||||
user
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
.then(awtisResponse => {
|
||||
axios.post(`${API_URL}/teks`, {
|
||||
tit,
|
||||
transkripsyon,
|
||||
tradiksyon: {
|
||||
try {
|
||||
const artiste = await axios.post(`${API_URL}/artistes`, {
|
||||
data: {
|
||||
alias: awtis,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email
|
||||
}
|
||||
}
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
|
||||
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
|
||||
}, {
|
||||
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.`)
|
||||
setLoading(false)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email
|
||||
},
|
||||
artistes: [artiste.data.id],
|
||||
traductionAuto: tradiksyonOtomatik
|
||||
}
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
|
||||
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?.response?.data)
|
||||
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 s’est produite</strong> : <i>{error.message}</i>
|
||||
<strong>Une erreur s’est produite</strong> : <i>{error?.error?.message}</i>
|
||||
</Alert>
|
||||
</Snackbar>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user