Newer
Older
Sébastien DA ROCHA
committed
const axios = require("axios");
import Vue from 'vue';
import Vuex from 'vuex';
import router from '../router'
//import modules from './modules';
import feature_type from "./modules/feature_type"
import feature from "./modules/feature"
import map from "./modules/map"
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');
Sébastien DA ROCHA
committed
Vue.use(Vuex);
axios.defaults.withCredentials = true; // * add cookies to axios
function updateAxiosHeader() {
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');
}
// ! À vérifier s'il y a un changement de token pendant l'éxécution de l'appli
updateAxiosHeader();
export default new Vuex.Store({
modules: {
feature_type,
feature,
map
},
state: {
Sébastien DA ROCHA
committed
logged: false,
user: false,
configuration:null,
project_slug: null,
projects: [],
last_comments: [],
staticPages: null,
SSO_SETTED: false,
USER_LEVEL_PROJECTS: null,
user_permissions: null,
},
mutations: {
error(state, data) {
return state.error = data
},
Sébastien DA ROCHA
committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
SET_PROJECTS(state, projects) {
state.projects = projects;
},
ADD_PROJECT(state, project) {
state.projects = [project, ...state.projects];
},
SET_PROJECT_SLUG(state, slug) {
state.project_slug = slug;
},
SET_USER(state, payload) {
state.user = payload;
},
SET_CONFIG(state, payload) {
state.configuration = payload;
},
SET_USERS(state, payload) {
state.users = payload;
},
SET_COOKIE(state, cookie) {
state.cookie = cookie
},
SET_STATIC_PAGES(state, staticPages) {
state.staticPages = staticPages
},
SET_SSO(state, SSO_SETTED) {
state.SSO_SETTED = SSO_SETTED
},
SET_USER_LEVEL_PROJECTS(state, USER_LEVEL_PROJECTS) {
state.USER_LEVEL_PROJECTS = USER_LEVEL_PROJECTS
},
SET_LOGGED(state, value) {
state.logged = value
},
SET_PROJECT_COMMENTS(state, last_comments) {
state.last_comments = last_comments
},
SET_USER_PERMISSIONS(state, userPermissions) {
state.user_permissions = userPermissions
},
},
getters: {
project: state => state.projects.find((project) => project.slug === state.project_slug),
project_types: state => state.projects.filter(projet => projet.is_project_type),
project_user: state => state.projects.filter(projet => projet.creator === state.user.id), // todo: add id to user in api
},
actions: {
async GET_ALL_PROJECTS({ commit }) {
function parseDate(date) {
let dateArr = date.split("/").reverse()
return new Date(dateArr[0], dateArr[1] - 1, dateArr[2])
}
await axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/`)
.then((response) => {
if (response.status === 200 && response.data) {
const orderedProjects = response.data.sort((a, b) => parseDate(b.created_on) - parseDate(a.created_on));
commit("SET_PROJECTS", orderedProjects)
}
})
.catch((error) => {
throw error;
});
},
async GET_STATIC_PAGES({ commit }) {
await axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}flat-pages/`)
.then((response) => (commit("SET_STATIC_PAGES", response.data)))
.catch((error) => {
throw error;
});
},
LOGIN({ commit, dispatch }, payload) {
if (payload.username && payload.password) {
axios
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}login/`, {
username: payload.username,
password: payload.password,
})
.then((response) => {
Sébastien DA ROCHA
committed
if (response && response.status === 200) {
// * use stored previous route to go back after login if page not open on login at first
let routerHistory = ''
if (router.options.routerHistory[0] != undefined){
routerHistory = router.options.routerHistory[0].name !== "login" ? router.options.routerHistory : "/"
} else {
routerHistory = "/"
}
Sébastien DA ROCHA
committed
commit("SET_USER", response.data.user);
router.push(routerHistory[routerHistory.length - 1] || "/")
dispatch("GET_USER_LEVEL_PROJECTS");
}
})
.catch((error) => {
if (error.response.status === 403)
commit('error', error.response.data.detail)
Sébastien DA ROCHA
committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
commit("SET_USER", false);
});
}
},
USER_INFO({ commit }) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user_info/`)
.then((response) => {
if (response && response.status === 200) {
const user = response.data.user;
commit("SET_USER", user);
window.localStorage.setItem("user", JSON.stringify(user)); //? toujours nécessaire ?
}
})
.catch(() => {
router.push({ name: "login" });
});
},
LOGOUT({ commit }) { // ? logout se fait bien dans django ?
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}logout/`)
.then((response) => {
if (response && response.status === 200) {
commit("SET_USER", false); // ? better false or null
commit("SET_USER_LEVEL_PROJECTS", null);
}
})
.catch((error) => {
throw error;
});
},
GET_CONFIG({ commit }) {
axios
.get("./config/config.json")
.then((response) => {
if (response && response.status === 200) {
commit("SET_CONFIG", response.data)
}
})
.catch((error) => {
throw error;
});
},
GET_USER_LEVEL_PROJECTS({ commit }) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user-level-projects/`)
.then((response) => {
if (response && response.status === 200) {
commit("SET_USER_LEVEL_PROJECTS", response.data)
}
})
.catch((error) => {
throw error;
});
},
GET_USER_LEVEL_PERMISSIONS({ commit }) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user-permissions/`)
.then((response) => {
if (response && response.status === 200) {
commit("SET_USER_PERMISSIONS", response.data)
}
})
.catch((error) => {
throw error;
});
},
GET_PROJECT_INFO({ commit, dispatch }, slug) {
commit("SET_PROJECT_SLUG", slug);
dispatch("GET_PROJECT_LAST_MESSAGES", slug);
dispatch("feature_type/GET_PROJECT_FEATURE_TYPES", slug);
dispatch("feature/GET_PROJECT_FEATURES", slug);
dispatch("map/GET_BASEMAPS", slug);
},
GET_PROJECT_LAST_MESSAGES({ commit }, project_slug) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/comments/`)
.then((response) => {
if (response && response.status === 200) {
commit("SET_PROJECT_COMMENTS", response.data.last_comments)
}
})
.catch((error) => {
throw error;
});
},
/* GET_PROJECT_FEATURES({ commit }, project_slug) {
axios
.get(`${DJANGO_API_BASE}projects/${project_slug}/feature`)
.then((response) => commit("feature/SET_FEATURES", response.data.features))
.catch((error) => {
throw error;
});
}, */
/* GET_PROJECT({ commit }, project_slug) {
axios
.get(`${DJANGO_API_BASE}projects/${project_slug}/project`)
.then((response) => commit("SET_PROJECT", response.data))
.catch((error) => {
throw error;
});
},
GET_PROJECT_USER({ commit }, project_slug) {
axios
.get(`${DJANGO_API_BASE}projects/${project_slug}/utilisateurs`)
.then((response) => (commit("SET_PROJECT_MEMBERS", response.data.members)))
.catch((error) => {
throw error;
});
}, */
/* GET_COOKIE({ commit }, name) {
let cookieValue = null;
if (document.cookie && document.cookie !== "") {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === name + "=") {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
commit("SET_COOKIE", cookieValue);
}, */
}
});