Newer
Older
//import router from '../../router'
const feature = {
namespaced: true,
state: {

Timothee P
committed
attachmentFormset: [],

Timothee P
committed
features: [],

Timothee P
committed
form: null,

Timothee P
committed
extra_form: []
},
mutations: {

Timothee P
committed
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;

Timothee P
committed
ADD_ATTACHMENT_FORM(state, dataKey) {
state.attachmentFormset = [...state.attachmentFormset, { dataKey }];

Timothee P
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);
ADD_LINKED_FORM(state, dataKey) {
state.linkedFormset = [...state.linkedFormset, { dataKey }];
},
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);
},
},
getters: {
},
actions: {

Timothee P
committed
GET_PROJECT_FEATURES({ commit, /* dispatch */ }, project_slug) {
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/feature/`)
.then((response) => {
const features = response.data.features;
commit("SET_FEATURES", features);

Timothee P
committed
//dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated
})
.catch((error) => {
throw error;
});
},
/* UPDATE_FEATURES({dispatch, commit}, features) {
commit("SET_FEATURES", features);
dispatch ("map/ADD_FEATURES");
}, */
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
POST_FEATURE({ state, rootState, rootGetters }) {
/* const data = {
form: state.form,
attachmentFormset: state.attachmentFormset,
linkedFormset: state.linkedFormset,
extra_form: state.extra_form,
} */
const data = {
"feature_id": state.form.feature_id,
"title": state.form.title,
"description": state.form.description.value,
"status": state.form.status,
"project": rootState.project_slug,
"feature_type": rootGetters.feature_type,
"geom": state.form.geometry,
"feature_data": state.extra_form
}
if (state.form.feature_id) {
axios
.put(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${rootState.project_slug}/feature/${state.form.feature_id}/`, data)
.then((response) => {
console.log(response, response.data)
//const routerHistory = router.options.routerHistory
//router.push(routerHistory[routerHistory.length - 1] || "/")
})
.catch((error) => {
throw error;
});
} else {
axios
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${rootState.project_slug}/feature/`, data)
.then((response) => {
console.log(response, response.data)
//const routerHistory = router.options.routerHistory
//router.push(routerHistory[routerHistory.length - 1] || "/")
})
.catch((error) => {
throw error;
});
}
},
//console.log("Deleting feature:", feature_slug, state)
/* axios
.post(`${DJANGO_API_BASE}feature_type/`, data)
.then((response) => {
const routerHistory = router.options.routerHistory
commit("SET_USER", response.data.user);
router.push(routerHistory[routerHistory.length - 1] || "/")
dispatch("GET_USER_LEVEL_PROJECTS");
})
.catch(() => {
commit("SET_USER", false)
}); */
// },
// POST_COMMENT({ state }, data) {
//console.log("post comment", data, state)
/* axios
.post(`${DJANGO_API_BASE}feature_type/`, data)
.then((response) => {
const routerHistory = router.options.routerHistory
commit("SET_USER", response.data.user);
router.push(routerHistory[routerHistory.length - 1] || "/")
dispatch("GET_USER_LEVEL_PROJECTS");
})
.catch(() => {
commit("SET_USER", false)
}); */
// },
// EXPORT_FEATURES({ /* state */ }) {
//console.log("Export features", state.features)
// }
},
}
export default feature