Newer
Older
Sébastien DA ROCHA
committed
const axios = require("axios");
import Vue from 'vue';
import Vuex from 'vuex';
import router from '../router'
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();
const noPermissions = { "can_view_project": true, "can_create_project": false, "can_update_project": false, "can_view_feature": true, "can_view_archived_feature": true, "can_create_feature": false, "can_update_feature": false, "can_delete_feature": false, "can_publish_feature": false, "can_create_feature_type": false, "can_view_feature_type": true, "is_project_administrator": false }
Sébastien DA ROCHA
committed
export default new Vuex.Store({
modules: {
feature_type,
feature,
map
},
state: {
logged: false,
user: false,
Sébastien DA ROCHA
committed
project_slug: null,
projects: [],
last_comments: [],
staticPages: null,
SSO_SETTED: false,
USER_LEVEL_PROJECTS: null,
user_permissions: null,
Sébastien DA ROCHA
committed
},
mutations: {
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) {
Sébastien DA ROCHA
committed
},
SET_STATIC_PAGES(state, staticPages) {
Sébastien DA ROCHA
committed
},
SET_SSO(state, SSO_SETTED) {
Sébastien DA ROCHA
committed
},
SET_USER_LEVEL_PROJECTS(state, USER_LEVEL_PROJECTS) {
Sébastien DA ROCHA
committed
},
SET_LOGGED(state, value) {
Sébastien DA ROCHA
committed
},
SET_PROJECT_COMMENTS(state, last_comments) {
Sébastien DA ROCHA
committed
},
SET_USER_PERMISSIONS(state, userPermissions) {
Sébastien DA ROCHA
committed
},
DISPLAY_MESSAGE(state, comment) {
state.messages = [{ comment }, ...state.messages];
document.getElementById("messages").scrollIntoView({ block: "start", inline: "nearest" });
},
CLEAR_MESSAGES(state) {
state.messages = [];
}
Sébastien DA ROCHA
committed
},
getters: {
project: state => state.projects.find((project) => project.slug === state.project_slug),
permissions: state => state.user_permissions ? state.user_permissions[state.project_slug] : noPermissions,
Sébastien DA ROCHA
committed
project_types: state => state.projects.filter(projet => projet.is_project_type),
project_user: state => state.projects.filter(projet => projet.creator === state.user.id),
Sébastien DA ROCHA
committed
},
actions: {
Sébastien DA ROCHA
committed
function parseDate(date) {
let dateArr = date.split("/").reverse();
return new Date(dateArr[0], dateArr[1] - 1, dateArr[2]);
Sébastien DA ROCHA
committed
}
Sébastien DA ROCHA
committed
.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));
Sébastien DA ROCHA
committed
}
})
.catch((error) => {
throw error;
});
},
Sébastien DA ROCHA
committed
.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) {
Sébastien DA ROCHA
committed
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}login/`, {
username: payload.username,
password: payload.password,
})
.then((response) => {
if (response.status === 200 && response.data) {
Sébastien DA ROCHA
committed
// * 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 : "/";
Sébastien DA ROCHA
committed
commit("SET_USER", response.data.user);
router.push(routerHistory[routerHistory.length - 1] || "/");
Sébastien DA ROCHA
committed
dispatch("GET_USER_LEVEL_PROJECTS");
Sébastien DA ROCHA
committed
}
})
Sébastien DA ROCHA
committed
commit("SET_USER", false);
Sébastien DA ROCHA
committed
});
}
},
if (!state.user) {
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)); // ? nécessaire ?
}
})
.catch(() => {
router.push({ name: "login" });
});
}
Sébastien DA ROCHA
committed
},
LOGOUT({ commit, dispatch }) {
// const pageNoRedirect = ["liste-signalements", "details-type-signalement", "details-signalement", "project_detail", "mentions", "aide", "index"]
Sébastien DA ROCHA
committed
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}logout/`)
.then((response) => {
if (response && response.status === 200) {
Sébastien DA ROCHA
committed
commit("SET_USER_LEVEL_PROJECTS", null);
dispatch("GET_USER_LEVEL_PERMISSIONS");
// if (!pageNoRedirect.includes(router.history.current.name)) router.push("/");
if (router.history.current.name !== "index") router.push("/");
Sébastien DA ROCHA
committed
}
})
.catch((error) => {
throw error;
});
},
GET_CONFIG({ commit }) {
axios
.get("./config/config.json")
.then((response) => {
if (response && response.status === 200) {
Sébastien DA ROCHA
committed
}
})
.catch((error) => {
throw error;
});
},
Sébastien DA ROCHA
committed
GET_USER_LEVEL_PROJECTS({ commit }) {
Sébastien DA ROCHA
committed
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user-level-projects/`)
.then((response) => {
if (response && response.status === 200) {
Sébastien DA ROCHA
committed
}
})
.catch((error) => {
throw error;
});
},
GET_USER_LEVEL_PERMISSIONS({ commit }) {
Sébastien DA ROCHA
committed
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user-permissions/`)
.then((response) => {
if (response && response.status === 200) {
Sébastien DA ROCHA
committed
}
})
.catch((error) => {
throw error;
});
},
async GET_PROJECT_INFO({ state, commit, dispatch }, slug) {
Sébastien DA ROCHA
committed
commit("SET_PROJECT_SLUG", slug);
let promises = [
dispatch("GET_PROJECT_LAST_MESSAGES", slug).then(response => response),
dispatch("feature_type/GET_PROJECT_FEATURE_TYPES", slug).then(response => response),
dispatch("feature/GET_PROJECT_FEATURES", slug).then(response => response),
]
if (state.user) promises.push(dispatch("map/GET_BASEMAPS", slug).then(response => response))
const promiseResult = await Promise.all(promises)
return promiseResult;
Sébastien DA ROCHA
committed
},
GET_PROJECT_LAST_MESSAGES({ commit }, project_slug) {
Sébastien DA ROCHA
committed
.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);
Sébastien DA ROCHA
committed
}
return response;
Sébastien DA ROCHA
committed
})
.catch((error) => {
throw error;
});
},
}
});