diff --git a/src/components/feature/FeatureAttachmentForm.vue b/src/components/feature/FeatureAttachmentForm.vue index c75905f2db595f7cee272a5d245ede78a522024f..83343c7935dab9d69dee36ccbde5b2315927da43 100644 --- a/src/components/feature/FeatureAttachmentForm.vue +++ b/src/components/feature/FeatureAttachmentForm.vue @@ -23,7 +23,7 @@ :name="form.title.html_name" :id="form.title.id_for_label" v-model="form.title.value" - @change="updateStore" + /> <ul :id="form.title.id_for_error" class="errorlist"> <li v-for="error in form.title.errors" :key="error"> @@ -63,7 +63,6 @@ name="form.info.html_name" rows="5" v-model="form.info.value" - @change="updateStore" ></textarea> <!-- {{ form.info.errors }} --> </div> @@ -80,8 +79,14 @@ export default { data() { return { + newAttachementIds: [], fileToImport: null, form: { + id: { + value: "", + errors: null, + label: "ID", + }, title: { errors: [], id_for_error: `errorlist-title-${this.attachmentForm.dataKey}`, @@ -113,8 +118,21 @@ export default { attachmentForm(newValue) { this.initForm(newValue); }, + "form.title.value": function (newValue, oldValue) { + if (oldValue != ''){ + if (newValue != oldValue){ + this.updateStore(); + } + } + }, + "form.info.value": function (newValue, oldValue) { + if (oldValue != ''){ + if (newValue != oldValue){ + this.updateStore(); + } + } + }, }, - methods: { initForm(attachmentForm) { for (let el in attachmentForm) { @@ -133,16 +151,29 @@ export default { "feature/REMOVE_ATTACHMENT_FORM", this.attachmentForm.dataKey ); + if (this.form.id.value) + this.$store.commit( + "feature/DELETE_ATTACHMENTS", + this.form.id.value + ); }, updateStore() { - this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", { + const data = { + id: this.form.id.value, dataKey: this.attachmentForm.dataKey, title: this.form.title.value, attachment_file: this.form.attachment_file.value, info: this.form.info.value, fileToImport: this.fileToImport, - }); + } + this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", data); + if (data.id){ + this.$store.commit( + "feature/PUT_ATTACHMENTS", + data + ); + } }, onFileChange(e) { diff --git a/src/store/modules/feature.js b/src/store/modules/feature.js index 02c889157156c82b25dfa1d9916228691c872d55..131f0b5699878f9bb6d46b997a6017f874215ea5 100644 --- a/src/store/modules/feature.js +++ b/src/store/modules/feature.js @@ -12,6 +12,8 @@ const feature = { namespaced: true, state: { attachmentFormset: [], + attachmentsToDelete: [], + attachmentsToPut: [], linkedFormset: [], features: [], form: null, @@ -63,6 +65,20 @@ 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) { + // 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); + }, }, getters: { }, @@ -154,29 +170,97 @@ const feature = { } }, - SEND_ATTACHMENTS({ state, rootState }, featureId) { - for (let attacht of state.attachmentFormset) { - if (attacht.fileToImport) { //* if no new file imported abort, until beeing able to do PUT + 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", attacht.fileToImport, attacht.fileToImport.name); + formdata.append("file", attachment.fileToImport, attachment.fileToImport.name); const data = { - title: attacht.title, - info: attacht.info, + title: attachment.title, + info: attachment.info, } formdata.append("data", JSON.stringify(data)); axios - .post(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/attachments/`, formdata) - .then((response) => { - if (response.status === 200 && response.data) { - console.log(response, response.data) - return "La pièce jointe a bien été ajouté" - } - }) + .post(`${DJANGO_API_BASE}features/${featureId}/attachments/`, formdata) + .then((response) => response) .catch((error) => { - throw error; + console.error(error); + return error }); + } } + + 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, + 'featureId': featureId + } + return dispatch("DELETE_ATTACHMENTS", payload) + .then((response) => response); + } + const promisesResult = await Promise.all([ + ...state.attachmentFormset.map((attachment) => postAttachement(attachment)), + ...state.attachmentsToPut.map((attachments) => putAttachement(attachments, featureId)), + ...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}/` + return axios + .delete(url) + .then((response) => { + if (response && response.status === 204) { + commit("REMOVE_ATTACHMENTS_ID_TO_DELETE", payload.attachmentsId) + return response + } + }) + .catch((error) => { + console.error(error); + return error + }); }, PUT_LINKED_FEATURES({ state, rootState }, featureId) { diff --git a/src/views/feature/Feature_edit.vue b/src/views/feature/Feature_edit.vue index cefa6f35cbb71d4d7ba9b5fb7bb21b0cb5785199..17cc7da86bbf5974a2cba46a8908c00ad262ea43 100644 --- a/src/views/feature/Feature_edit.vue +++ b/src/views/feature/Feature_edit.vue @@ -340,6 +340,8 @@ export default { ...mapState("map", ["basemaps"]), ...mapState("feature", [ "attachmentFormset", + "attachmentsToDelete", + "attachmentsToPut", "linkedFormset", "features", "extra_form",