From afbb90ac17f2479005fe86d295841b4d50c5c340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Fri, 20 May 2022 02:21:38 +0400 Subject: [PATCH] Fix EkriTeks and adapt to new properties --- components/soumet/ekri-teks.js | 177 +++++++++++++++++---------------- 1 file changed, 91 insertions(+), 86 deletions(-) diff --git a/components/soumet/ekri-teks.js b/components/soumet/ekri-teks.js index ac793d0..1eb360d 100644 --- a/components/soumet/ekri-teks.js +++ b/components/soumet/ekri-teks.js @@ -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}) { )} {error && ( - + - Une erreur s’est produite : {error.message} + Une erreur s’est produite : {error?.error?.message} )}