Skip to content
Snippets Groups Projects
Commit db8cd589 authored by Timothee P's avatar Timothee P :sunflower:
Browse files

Adapt to new endpoints & fix code

parent a282d6e2
No related branches found
No related tags found
No related merge requests found
...@@ -79,7 +79,6 @@ export default { ...@@ -79,7 +79,6 @@ export default {
data() { data() {
return { return {
newAttachementIds: [],
fileToImport: null, fileToImport: null,
form: { form: {
id: { id: {
...@@ -154,7 +153,7 @@ export default { ...@@ -154,7 +153,7 @@ export default {
this.attachmentForm.dataKey this.attachmentForm.dataKey
); );
if (this.form.id.value) 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() { updateStore() {
...@@ -167,9 +166,6 @@ export default { ...@@ -167,9 +166,6 @@ export default {
fileToImport: this.fileToImport, fileToImport: this.fileToImport,
}; };
this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", data); this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", data);
if (data.id) {
this.$store.commit("feature/PUT_ATTACHMENTS", data);
}
}, },
validateImgFile(files, handleFile) { validateImgFile(files, handleFile) {
......
...@@ -13,7 +13,6 @@ const feature = { ...@@ -13,7 +13,6 @@ const feature = {
state: { state: {
attachmentFormset: [], attachmentFormset: [],
attachmentsToDelete: [], attachmentsToDelete: [],
attachmentsToPut: [],
checkedFeatures: [], checkedFeatures: [],
extra_form: [], extra_form: [],
features: [], features: [],
...@@ -84,17 +83,12 @@ const feature = { ...@@ -84,17 +83,12 @@ const feature = {
CLEAR_LINKED_FORM(state) { CLEAR_LINKED_FORM(state) {
state.linkedFormset = []; state.linkedFormset = [];
}, },
PUT_ATTACHMENTS(state, attachement) {
state.attachmentsToPut = state.attachmentsToPut.filter(el => el.id !== attachement.id); ADD_ATTACHMENT_TO_DELETE(state, attachementId) {
state.attachmentsToPut.push(attachement);
},
DELETE_ATTACHMENTS(state, attachementId) {
// state.attachmentFormset = state.attachmentFormset.filter(el => el.id !== attachementId); // state.attachmentFormset = state.attachmentFormset.filter(el => el.id !== attachementId);
state.attachmentsToDelete.push(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) { REMOVE_ATTACHMENTS_ID_TO_DELETE(state, attachementId) {
state.attachmentsToDelete = state.attachmentsToDelete.filter(el => el !== attachementId); state.attachmentsToDelete = state.attachmentsToDelete.filter(el => el !== attachementId);
}, },
...@@ -179,20 +173,20 @@ const feature = { ...@@ -179,20 +173,20 @@ const feature = {
}) })
.catch((error) => { .catch((error) => {
commit("DISCARD_LOADER", null, { root: true }) commit("DISCARD_LOADER", null, { root: true })
if(error.message=="Network Error" ||window.navigator.onLine==false){ if (error.message == "Network Error" || window.navigator.onLine == false) {
let arraysOffline=[]; let arraysOffline = [];
let localStorageArray=localStorage.getItem("geocontrib_offline"); let localStorageArray = localStorage.getItem("geocontrib_offline");
if(localStorageArray){ if (localStorageArray) {
arraysOffline=JSON.parse(localStorageArray); arraysOffline = JSON.parse(localStorageArray);
} }
let updateMsg={ let updateMsg = {
project:rootState.project_slug, project: rootState.project_slug,
type:'put', type: 'put',
featureId:state.form.feature_id, featureId: state.form.feature_id,
geojson:geojson geojson: geojson
}; };
arraysOffline.push(updateMsg); arraysOffline.push(updateMsg);
localStorage.setItem("geocontrib_offline",JSON.stringify(arraysOffline)); localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline));
router.push({ router.push({
name: "offline-signalement", name: "offline-signalement",
params: { params: {
...@@ -201,11 +195,11 @@ const feature = { ...@@ -201,11 +195,11 @@ const feature = {
}); });
} }
else{ else {
console.log(error) console.log(error)
throw error; throw error;
} }
throw error; throw error;
}); });
} else { } else {
...@@ -222,19 +216,19 @@ const feature = { ...@@ -222,19 +216,19 @@ const feature = {
}) })
.catch((error) => { .catch((error) => {
commit("DISCARD_LOADER", null, { root: true }) commit("DISCARD_LOADER", null, { root: true })
if(error.message=="Network Error" ||window.navigator.onLine==false){ if (error.message == "Network Error" || window.navigator.onLine == false) {
let arraysOffline=[]; let arraysOffline = [];
let localStorageArray=localStorage.getItem("geocontrib_offline"); let localStorageArray = localStorage.getItem("geocontrib_offline");
if(localStorageArray){ if (localStorageArray) {
arraysOffline=JSON.parse(localStorageArray); arraysOffline = JSON.parse(localStorageArray);
} }
let updateMsg={ let updateMsg = {
project:rootState.project_slug, project: rootState.project_slug,
type:'post', type: 'post',
geojson:geojson geojson: geojson
}; };
arraysOffline.push(updateMsg); arraysOffline.push(updateMsg);
localStorage.setItem("geocontrib_offline",JSON.stringify(arraysOffline)); localStorage.setItem("geocontrib_offline", JSON.stringify(arraysOffline));
router.push({ router.push({
name: "offline-signalement", name: "offline-signalement",
params: { params: {
...@@ -243,11 +237,11 @@ const feature = { ...@@ -243,11 +237,11 @@ const feature = {
}); });
} }
else{ else {
console.log(error) console.log(error)
throw error; throw error;
} }
}); });
} }
}, },
...@@ -255,18 +249,54 @@ const feature = { ...@@ -255,18 +249,54 @@ const feature = {
async SEND_ATTACHMENTS({ state, rootState, dispatch }, featureId) { async SEND_ATTACHMENTS({ state, rootState, dispatch }, featureId) {
const DJANGO_API_BASE = rootState.configuration.VUE_APP_DJANGO_API_BASE; const DJANGO_API_BASE = rootState.configuration.VUE_APP_DJANGO_API_BASE;
function postAttachement(attachment) { function addFile(attachment, attchmtId) {
if (!attachment.id) { let formdata = new FormData();
let formdata = new FormData(); formdata.append("file", attachment.fileToImport, attachment.fileToImport.name);
formdata.append("file", attachment.fileToImport, attachment.fileToImport.name); return axios
const data = { .put(`${DJANGO_API_BASE}features/${featureId}/attachments/${attchmtId}/upload-file/`, formdata)
title: attachment.title, .then((response) => {
info: attachment.info, console.log(response)
} if (response && response.status === 200) {
formdata.append("data", JSON.stringify(data)); console.log(response.status)
axios }
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) .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) => { .catch((error) => {
console.error(error); console.error(error);
return error return error
...@@ -275,25 +305,6 @@ const feature = { ...@@ -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) { function deleteAttachement(attachmentsId, featureId) {
let payload = { let payload = {
'attachmentsId': attachmentsId, 'attachmentsId': attachmentsId,
...@@ -303,31 +314,14 @@ const feature = { ...@@ -303,31 +314,14 @@ const feature = {
.then((response) => response); .then((response) => response);
} }
const promisesResult = await Promise.all([ const promisesResult = await Promise.all([
...state.attachmentFormset.map((attachment) => postAttachement(attachment)), ...state.attachmentFormset.map((attachment) => putOrPostAttachement(attachment)),
...state.attachmentsToPut.map((attachments) => putAttachement(attachments, featureId)),
...state.attachmentsToDelete.map((attachmentsId) => deleteAttachement(attachmentsId, featureId)) ...state.attachmentsToDelete.map((attachmentsId) => deleteAttachement(attachmentsId, featureId))
] ]
); );
state.attachmentsToDelete = [] state.attachmentsToDelete = []
state.attachmentsToPut = []
return promisesResult 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) { DELETE_ATTACHMENTS({ commit }, payload) {
let url = `${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${payload.featureId}/attachments/${payload.attachmentsId}/` let url = `${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${payload.featureId}/attachments/${payload.attachmentsId}/`
...@@ -335,6 +329,7 @@ const feature = { ...@@ -335,6 +329,7 @@ const feature = {
.delete(url) .delete(url)
.then((response) => { .then((response) => {
if (response && response.status === 204) { if (response && response.status === 204) {
console.log(response)
commit("REMOVE_ATTACHMENTS_ID_TO_DELETE", payload.attachmentsId) commit("REMOVE_ATTACHMENTS_ID_TO_DELETE", payload.attachmentsId)
return response return response
} }
......
...@@ -155,13 +155,13 @@ ...@@ -155,13 +155,13 @@
<a <a
class="ui tiny image" class="ui tiny image"
target="_blank" target="_blank"
:href="DJANGO_BASE_URL + pj.attachment_file" :href="pj.attachment_file"
> >
<img <img
:src=" :src="
pj.extension === '.pdf' pj.extension === '.pdf'
? require('@/assets/img/pdf.png') ? require('@/assets/img/pdf.png')
: DJANGO_BASE_URL + pj.attachment_file : pj.attachment_file
" "
/> />
</a> </a>
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
<a <a
class="header" class="header"
target="_blank" target="_blank"
:href="DJANGO_BASE_URL + pj.attachment_file" :href="pj.attachment_file"
>{{ pj.title }}</a >{{ pj.title }}</a
> >
<div class="description"> <div class="description">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment