From db8cd5898aa67589dee2978e2ba4eaf9404acd03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr> Date: Wed, 20 Oct 2021 10:02:47 +0200 Subject: [PATCH] Adapt to new endpoints & fix code --- .../feature/FeatureAttachmentForm.vue | 6 +- src/store/modules/feature.js | 159 +++++++++--------- src/views/feature/Feature_detail.vue | 6 +- 3 files changed, 81 insertions(+), 90 deletions(-) diff --git a/src/components/feature/FeatureAttachmentForm.vue b/src/components/feature/FeatureAttachmentForm.vue index eb7f1aa2..34d7917d 100644 --- a/src/components/feature/FeatureAttachmentForm.vue +++ b/src/components/feature/FeatureAttachmentForm.vue @@ -79,7 +79,6 @@ export default { data() { return { - newAttachementIds: [], fileToImport: null, form: { id: { @@ -154,7 +153,7 @@ export default { this.attachmentForm.dataKey ); if (this.form.id.value) - this.$store.commit("feature/DELETE_ATTACHMENTS", this.form.id.value); + this.$store.commit("feature/ADD_ATTACHMENT_TO_DELETE", this.form.id.value); }, updateStore() { @@ -167,9 +166,6 @@ export default { fileToImport: this.fileToImport, }; this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", data); - if (data.id) { - this.$store.commit("feature/PUT_ATTACHMENTS", data); - } }, validateImgFile(files, handleFile) { diff --git a/src/store/modules/feature.js b/src/store/modules/feature.js index 72b383a7..1e11c016 100644 --- a/src/store/modules/feature.js +++ b/src/store/modules/feature.js @@ -13,7 +13,6 @@ const feature = { state: { attachmentFormset: [], attachmentsToDelete: [], - attachmentsToPut: [], checkedFeatures: [], extra_form: [], features: [], @@ -84,17 +83,12 @@ const feature = { CLEAR_LINKED_FORM(state) { state.linkedFormset = []; }, - PUT_ATTACHMENTS(state, attachement) { - state.attachmentsToPut = state.attachmentsToPut.filter(el => el.id !== attachement.id); - state.attachmentsToPut.push(attachement); - }, - DELETE_ATTACHMENTS(state, attachementId) { + + ADD_ATTACHMENT_TO_DELETE(state, attachementId) { // state.attachmentFormset = state.attachmentFormset.filter(el => el.id !== attachementId); state.attachmentsToDelete.push(attachementId); }, - REMOVE_ATTACHMENTS_ID_TO_PUT(state, attachement) { - state.attachmentsToPut = state.attachmentsToPut.filter(el => el.id !== attachement.id); - }, + REMOVE_ATTACHMENTS_ID_TO_DELETE(state, attachementId) { state.attachmentsToDelete = state.attachmentsToDelete.filter(el => el !== attachementId); }, @@ -179,20 +173,20 @@ const feature = { }) .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); + 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 + 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)); + localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline)); router.push({ name: "offline-signalement", params: { @@ -201,11 +195,11 @@ const feature = { }); } - else{ + else { console.log(error) throw error; } - + throw error; }); } else { @@ -222,19 +216,19 @@ const feature = { }) .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); + 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 + let updateMsg = { + project: rootState.project_slug, + type: 'post', + geojson: geojson }; arraysOffline.push(updateMsg); - localStorage.setItem("geocontrib_offline",JSON.stringify(arraysOffline)); + localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline)); router.push({ name: "offline-signalement", params: { @@ -243,11 +237,11 @@ const feature = { }); } - else{ + else { console.log(error) throw error; } - + }); } }, @@ -255,18 +249,54 @@ const feature = { async SEND_ATTACHMENTS({ state, rootState, dispatch }, featureId) { const DJANGO_API_BASE = rootState.configuration.VUE_APP_DJANGO_API_BASE; - function postAttachement(attachment) { - if (!attachment.id) { - let formdata = new FormData(); - formdata.append("file", attachment.fileToImport, attachment.fileToImport.name); - const data = { - title: attachment.title, - info: attachment.info, - } - formdata.append("data", JSON.stringify(data)); - axios + 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) => { + console.error(error); + return error + }); + } + + function putOrPostAttachement(attachment) { + let formdata = new FormData(); + formdata.append("title", attachment.title); + formdata.append("info", attachment.info); + + if (!attachment.id) { //* used to check if doesn't exist in DB and should be send through post (useless now) + return axios .post(`${DJANGO_API_BASE}features/${featureId}/attachments/`, formdata) - .then((response) => response) + .then((response) => { + console.log(response) + if (response && response.status === 201 && attachment.fileToImport) { + console.log(response.status) + return addFile(attachment, response.data.id); + } + return response + }) + .catch((error) => { + console.error(error); + return error + }); + } else { + 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); + } + }) .catch((error) => { console.error(error); return error @@ -275,25 +305,6 @@ const feature = { } } - function putAttachement(attachment, featureId) { - let formdataToUpdate = new FormData(); - if (attachment.fileToImport) - formdataToUpdate.append("file", attachment.fileToImport, attachment.fileToImport.name); - const data = {} - if (attachment.title) - data['title'] = attachment.title - if (attachment.title) - data['info'] = attachment.info - formdataToUpdate.append("data", JSON.stringify(data)); - let payload = { - 'attachmentsId': attachment.id, - 'featureId': featureId, - 'formdataToUpdate': formdataToUpdate - } - return dispatch("PUT_ATTACHMENTS", payload) - .then((response) => response); - } - function deleteAttachement(attachmentsId, featureId) { let payload = { 'attachmentsId': attachmentsId, @@ -303,31 +314,14 @@ const feature = { .then((response) => response); } const promisesResult = await Promise.all([ - ...state.attachmentFormset.map((attachment) => postAttachement(attachment)), - ...state.attachmentsToPut.map((attachments) => putAttachement(attachments, featureId)), + ...state.attachmentFormset.map((attachment) => putOrPostAttachement(attachment)), ...state.attachmentsToDelete.map((attachmentsId) => deleteAttachement(attachmentsId, featureId)) ] ); state.attachmentsToDelete = [] - state.attachmentsToPut = [] return promisesResult }, - PUT_ATTACHMENTS({ commit }, payload) { - let url = `${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${payload.featureId}/attachments/${payload.attachmentsId}/` - return axios - .put(url, payload.formdataToUpdate) - .then((response) => { - if (response && response.status === 204) { - commit("REMOVE_ATTACHMENTS_ID_TO_PUT", payload.attachmentsId) - return response - } - }) - .catch((error) => { - console.error(error); - return error - }); - }, DELETE_ATTACHMENTS({ commit }, payload) { let url = `${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${payload.featureId}/attachments/${payload.attachmentsId}/` @@ -335,6 +329,7 @@ const feature = { .delete(url) .then((response) => { if (response && response.status === 204) { + console.log(response) commit("REMOVE_ATTACHMENTS_ID_TO_DELETE", payload.attachmentsId) return response } diff --git a/src/views/feature/Feature_detail.vue b/src/views/feature/Feature_detail.vue index fc5562d7..371a938c 100644 --- a/src/views/feature/Feature_detail.vue +++ b/src/views/feature/Feature_detail.vue @@ -155,13 +155,13 @@ <a class="ui tiny image" target="_blank" - :href="DJANGO_BASE_URL + pj.attachment_file" + :href="pj.attachment_file" > <img :src=" pj.extension === '.pdf' ? require('@/assets/img/pdf.png') - : DJANGO_BASE_URL + pj.attachment_file + : pj.attachment_file " /> </a> @@ -169,7 +169,7 @@ <a class="header" target="_blank" - :href="DJANGO_BASE_URL + pj.attachment_file" + :href="pj.attachment_file" >{{ pj.title }}</a > <div class="description"> -- GitLab