Newer
Older
Sébastien DA ROCHA
committed
const axios = require("axios");
import router from '../../router'
Sébastien DA ROCHA
committed
axios.defaults.headers.common['X-CSRFToken'] = (name => {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
Sébastien DA ROCHA
committed
const feature = {
namespaced: true,
state: {
attachmentFormset: [],
attachmentsToDelete: [],
attachmentsToPut: [],
checkedFeatures: [],
extra_form: [],
Sébastien DA ROCHA
committed
features: [],
form: null,
statusChoices: [
{
name: "Brouillon",
value: "draft",
},
{
name: "Publié",
value: "published",
},
{
name: "Archivé",
value: "archived",
},
{
name: "En attente de publication",
value: "pending",
},
],
Sébastien DA ROCHA
committed
},
mutations: {
SET_FEATURES(state, features) {
state.features = features;
},
UPDATE_FORM(state, payload) {
state.form = payload;
},
UPDATE_EXTRA_FORM(state, extra_form) {
const index = state.extra_form.findIndex(el => el.label === extra_form.label);
if (index !== -1) {
state.extra_form[index] = extra_form;
}
},
SET_EXTRA_FORM(state, extra_form) {
state.extra_form = extra_form;
},
ADD_ATTACHMENT_FORM(state, attachmentFormset) {
state.attachmentFormset = [...state.attachmentFormset, attachmentFormset];
Sébastien DA ROCHA
committed
},
UPDATE_ATTACHMENT_FORM(state, payload) {
const index = state.attachmentFormset.findIndex((el) => el.dataKey === payload.dataKey);
if (index !== -1) state.attachmentFormset[index] = payload
},
REMOVE_ATTACHMENT_FORM(state, payload) {
state.attachmentFormset = state.attachmentFormset.filter(form => form.dataKey !== payload);
},
CLEAR_ATTACHMENT_FORM(state) {
state.attachmentFormset = [];
},

Timothee P
committed
ADD_LINKED_FORM(state, linkedFormset) {
state.linkedFormset = [...state.linkedFormset, linkedFormset];
Sébastien DA ROCHA
committed
},
UPDATE_LINKED_FORM(state, payload) {
const index = state.linkedFormset.findIndex((el) => el.dataKey === payload.dataKey);
if (index !== -1) state.linkedFormset[index] = payload
},
REMOVE_LINKED_FORM(state, payload) {
state.linkedFormset = state.linkedFormset.filter(form => form.dataKey !== payload);
},
SET_LINKED_FEATURES(state, payload) {
state.linked_features = payload;
},

Timothee P
committed
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);
},
UPDATE_CHECKED_FEATURES(state, checkedFeatures) {
state.checkedFeatures = checkedFeatures;
}
Sébastien DA ROCHA
committed
},
getters: {
},
actions: {
GET_PROJECT_FEATURES({ commit, rootState }, project_slug) {
.get(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/feature/`)
Sébastien DA ROCHA
committed
.then((response) => {
if (response.status === 200 && response.data) {
const features = response.data.features;
commit("SET_FEATURES", features);
//dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated
}
return response;
Sébastien DA ROCHA
committed
})
.catch((error) => {
throw error;
});
},
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";
commit("DISCARD_LOADER", null, { root: true })
router.push({
name: "details-signalement",
params: {
slug_type_signal: rootState.feature_type.current_feature_type_slug,
slug_signal: featureId,
message,
},
});
}
async function handleOtherForms(featureId) {
await dispatch("SEND_ATTACHMENTS", featureId)
await dispatch("PUT_LINKED_FEATURES", featureId)
redirect(featureId);
}
let extraFormObject = {}; //* prepare an object to be flatten in properties of geojson
extraFormObject[field.name] = field.value;
Sébastien DA ROCHA
committed
}
const geojson = {
"id": state.form.feature_id,
"type": "Feature",
"geometry": state.form.geometry,
"properties": {
"title": state.form.title,
"description": state.form.description.value,
"status": state.form.status.value,
"project": rootState.project_slug,
"feature_type": rootState.feature_type.current_feature_type_slug,
...extraFormObject
.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)
Sébastien DA ROCHA
committed
.then((response) => {
if (state.attachmentFormset.length > 0 || state.linkedFormset.length > 0) {
handleOtherForms(response.data.id)
} else {
redirect(response.data.id)
}
Sébastien DA ROCHA
committed
})
.catch((error) => {
commit("DISCARD_LOADER", null, { root: true })
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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.log(error)
throw error;
}
DESPRES Damien
committed
Sébastien DA ROCHA
committed
throw error;
});
.post(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson)
Sébastien DA ROCHA
committed
.then((response) => {
if (state.attachmentFormset.length > 0 || state.linkedFormset.length > 0) {
handleOtherForms(response.data.id)
} else {
redirect(response.data.id)
}
Sébastien DA ROCHA
committed
})
.catch((error) => {
commit("DISCARD_LOADER", null, { root: true })
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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;
}
Sébastien DA ROCHA
committed
});
}
},
async SEND_ATTACHMENTS({ state, rootState, dispatch }, featureId) {
const DJANGO_API_BASE = rootState.configuration.VUE_APP_DJANGO_API_BASE;
function postAttachement(attachment) {
if (!attachment.id) {
formdata.append("file", attachment.fileToImport, attachment.fileToImport.name);
title: attachment.title,
info: attachment.info,
}
formdata.append("data", JSON.stringify(data));
axios
.post(`${DJANGO_API_BASE}features/${featureId}/attachments/`, formdata)
.then((response) => response)
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))
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
);
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) {
.put(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/feature-links/`, state.linkedFormset)
.then((response) => {
if (response.status === 200 && response.data) {
console.log(response, response.data)
return "La relation a bien été ajouté"
}
})
.catch((error) => {
throw error;
});
},
const url = `${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${feature_id}/?` +
`feature_type__slug=${rootState.feature_type.current_feature_type_slug}` +
`&project__slug=${rootState.project_slug}`;
return axios
.delete(url)
.then((response) => response)
.catch(() => {
return false;
});
Sébastien DA ROCHA
committed
},
}