diff --git a/src/store/modules/feature.js b/src/store/modules/feature.js index 30735fef4da3ee7b0715a189b148848cbe7284aa..750434cfcbb2c5f22882bed7508977ea64521b76 100644 --- a/src/store/modules/feature.js +++ b/src/store/modules/feature.js @@ -180,7 +180,6 @@ const feature = { SEND_FEATURE({ state, rootState, commit, dispatch }, routeName) { commit("DISPLAY_LOADER", "Le signalement est en cours de création", { root: true }) const message = routeName === "editer-signalement" ? "Le signalement a été mis à jour" : "Le signalement a été crée"; - function redirect(featureId) { dispatch( 'GET_PROJECT_FEATURE', @@ -228,110 +227,65 @@ const feature = { } } + let url = `${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/` if (routeName === "editer-signalement") { - return axios - .put(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${state.form.feature_id}/?` + - `feature_type__slug=${rootState.feature_type.current_feature_type_slug}` + - `&project__slug=${rootState.project_slug}` - , geojson) - .then((response) => { - if (response.status === 200 && response.data) { - if (state.attachmentFormset.length > 0 || state.linkedFormset.length > 0) { - handleOtherForms(response.data.id) - } else { - redirect(response.data.id) - } - } - }) - .catch((error) => { - commit("DISCARD_LOADER", null, { root: true }) - if (error.message === "Network Error" || window.navigator.onLine === false) { - let arraysOffline = []; - let localStorageArray = localStorage.getItem("geocontrib_offline"); - if (localStorageArray) { - arraysOffline = JSON.parse(localStorageArray); - } - let updateMsg = { - project: rootState.project_slug, - type: 'put', - featureId: state.form.feature_id, - geojson: geojson - }; - arraysOffline.push(updateMsg); - localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline)); - router.push({ - name: "offline-signalement", - params: { - slug_type_signal: rootState.feature_type.current_feature_type_slug - }, - }); + url += `${state.form.feature_id}/?` + + `feature_type__slug=${rootState.feature_type.current_feature_type_slug}` + + `&project__slug=${rootState.project_slug}` + } + return axios({ + url, + method: routeName === "editer-signalement" ? "PUT" : "POST", + data: geojson + }).then((response) => { + if ((response.status === 200 || response.status === 201) && response.data) { + if (state.attachmentFormset.length > 0 || state.linkedFormset.length > 0 || state.attachmentFormset.length > 0 || state.attachmentsToDelete.length > 0) { + handleOtherForms(response.data.id) + } else { + redirect(response.data.id) } - else { - console.log(error) - throw error; + } + }) + .catch((error) => { + commit("DISCARD_LOADER", null, { root: true }) + if (error.message === "Network Error" || window.navigator.onLine === false) { + let arraysOffline = []; + let localStorageArray = localStorage.getItem("geocontrib_offline"); + if (localStorageArray) { + arraysOffline = JSON.parse(localStorageArray); } - + let updateMsg = { + project: rootState.project_slug, + type: 'put', + featureId: state.form.feature_id, + geojson: geojson + }; + arraysOffline.push(updateMsg); + localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline)); + router.push({ + name: "offline-signalement", + params: { + slug_type_signal: rootState.feature_type.current_feature_type_slug + }, + }); + } + else { + console.error(error) throw error; - }); - } else { - return axios - .post(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson) - .then((response) => { - if (response.status === 201 && response.data) { - if (state.attachmentFormset.length > 0 || state.linkedFormset.length > 0) { - handleOtherForms(response.data.id) - } else { - redirect(response.data.id) - } - } - }) - .catch((error) => { - commit("DISCARD_LOADER", null, { root: true }) - if (error.message === "Network Error" || window.navigator.onLine === false) { - let arraysOffline = []; - let localStorageArray = localStorage.getItem("geocontrib_offline"); - if (localStorageArray) { - arraysOffline = JSON.parse(localStorageArray); - } - let updateMsg = { - project: rootState.project_slug, - type: 'post', - geojson: geojson - }; - arraysOffline.push(updateMsg); - localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline)); - router.push({ - name: "offline-signalement", - params: { - slug_type_signal: rootState.feature_type.current_feature_type_slug - }, - }); - - } - else { - console.log(error) - throw error; - } - - }); - } - // this.$store.dispatch("GET_ALL_PROJECTS"), //* & refresh project list + } + throw error; + }); }, async SEND_ATTACHMENTS({ state, rootState, dispatch }, featureId) { const DJANGO_API_BASE = rootState.configuration.VUE_APP_DJANGO_API_BASE; - function addFile(attachment, attchmtId) { let formdata = new FormData(); formdata.append("file", attachment.fileToImport, attachment.fileToImport.name); return axios .put(`${DJANGO_API_BASE}features/${featureId}/attachments/${attchmtId}/upload-file/`, formdata) .then((response) => { - console.log(response) - if (response && response.status === 200) { - console.log(response.status) - } return response; }) .catch((error) => { @@ -349,9 +303,7 @@ const feature = { return axios .post(`${DJANGO_API_BASE}features/${featureId}/attachments/`, formdata) .then((response) => { - console.log(response) if (response && response.status === 201 && attachment.fileToImport) { - console.log(response.status) return addFile(attachment, response.data.id); } return response @@ -364,9 +316,7 @@ const feature = { return axios .put(`${DJANGO_API_BASE}features/${featureId}/attachments/${attachment.id}/`, formdata) .then((response) => { - console.log(response) if (response && response.status === 200 && attachment.fileToImport) { - console.log(response.status) return addFile(attachment, response.data.id); } }) @@ -386,6 +336,7 @@ const feature = { return dispatch("DELETE_ATTACHMENTS", payload) .then((response) => response); } + const promisesResult = await Promise.all([ ...state.attachmentFormset.map((attachment) => putOrPostAttachement(attachment)), ...state.attachmentsToDelete.map((attachmentsId) => deleteAttachement(attachmentsId, featureId))