Skip to content
Snippets Groups Projects
Commit d31425e0 authored by Sébastien DA ROCHA's avatar Sébastien DA ROCHA :bicyclist:
Browse files

Merge branch 'redmine-issues/11814' into 'develop'

REDMINE_ISSUE-11814

See merge request !208
parents fe052f2a 65ba7174
No related branches found
No related tags found
2 merge requests!2132.3.2-rc1,!208REDMINE_ISSUE-11814
import axios from '@/axios-client.js';
import router from '../../router'
// axios.defaults.headers.common['X-CSRFToken'] = (name => {
// var re = new RegExp(name + "=([^;]+)");
// var value = re.exec(document.cookie);
// return (value !== null) ? unescape(value[1]) : null;
// })('csrftoken');
const feature = {
namespaced: true,
......@@ -98,7 +92,6 @@ const feature = {
},
ADD_ATTACHMENT_TO_DELETE(state, attachementId) {
// state.attachmentFormset = state.attachmentFormset.filter(el => el.id !== attachementId);
state.attachmentsToDelete.push(attachementId);
},
......@@ -144,7 +137,6 @@ const feature = {
commit("SET_FEATURES", features);
const features_count = response.data.count;
commit("SET_FEATURES_COUNT", features_count);
//dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated
}
return response;
})
......@@ -180,7 +172,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 +219,66 @@ 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) => {
......@@ -345,37 +292,25 @@ const feature = {
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) => {
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
});
let url = `${DJANGO_API_BASE}features/${featureId}/attachments/`
if (attachment.id) {
url += `${attachment.id}/`
}
return axios({
url,
method: attachment.id ? "PUT" : "POST",
data: formdata
}).then((response) => {
if (response && (response.status === 200 || response.status === 201) && attachment.fileToImport) {
return addFile(attachment, response.data.id);
}
return response
})
.catch((error) => {
console.error(error);
return error
});
}
function deleteAttachement(attachmentsId, featureId) {
......@@ -386,6 +321,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))
......
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