import axios from '@/axios-client.js'; 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'); 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 }; export default new Vuex.Store({ modules: { feature_type, feature, map }, state: { logged: false, user: false, configuration: null, project_slug: null, projects: [], last_comments: [], staticPages: null, SSO_SETTED: false, USER_LEVEL_PROJECTS: null, user_permissions: null, levelsPermissions: [], messages: [], events: null, loader: { isLoading: false, message: "En cours de chargement" }, cancellableSearchRequest: [], reloadIntervalId: null }, 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) { 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; }, SET_LEVELS_PERMISSIONS(state, levelsPermissions) { state.levelsPermissions = levelsPermissions; }, SET_EVENTS(state, events) { state.events = events; }, DISPLAY_MESSAGE(state, comment) { state.messages = [{ comment }, ...state.messages]; if (document.getElementById("content")) document.getElementById("content").scrollIntoView({ block: "start", inline: "nearest" }); setTimeout(() => { state.messages = []; }, 3000); }, CLEAR_MESSAGES(state) { state.messages = []; }, DISPLAY_LOADER(state, message) { state.loader = { isLoading: true, message } }, DISCARD_LOADER(state) { state.loader = { isLoading: false, message: "En cours de chargement" }; }, SET_CANCELLABLE_SEARCH_REQUEST(state, payload) { state.cancellableSearchRequest.push(payload); }, RESET_CANCELLABLE_SEARCH_REQUEST(state) { state.cancellableSearchRequest = []; }, SET_RELOAD_INTERVAL_ID(state, payload) { state.reloadIntervalId = payload; }, CLEAR_RELOAD_INTERVAL_ID(state) { clearInterval(state.reloadIntervalId); state.reloadIntervalId = null; } }, getters: { project: state => state.projects.find((project) => project.slug === state.project_slug), permissions: state => state.user_permissions ? state.user_permissions[state.project_slug] : noPermissions, project_types: state => state.projects.filter(projet => projet.is_project_type), project_user: state => state.projects.filter(projet => projet.creator === state.user.id), }, actions: { GET_ALL_PROJECTS({ commit }) { function parseDate(date) { let dateArr = date.split("/").reverse(); return new Date(dateArr[0], dateArr[1] - 1, dateArr[2]); } return 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; }); }, GET_STATIC_PAGES({ commit }) { return 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) { return axios .post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}login/`, { username: payload.username, password: payload.password, }) .then((response) => { if (response.status === 200 && response.data) { // * 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 = "/"; } commit("SET_USER", response.data.user); router.push(routerHistory[routerHistory.length - 1] || "/"); dispatch("GET_USER_LEVEL_PROJECTS"); dispatch("GET_USER_LEVEL_PERMISSIONS") return response.status } }) .catch(() => { commit("SET_USER", false); return "error"; }); } }, USER_INFO({ state, commit }) { 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" }); }); } }, USER_EVENTS({ commit }) { return new Promise((resolve, reject) => { axios .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}events`) .then((response) => { if (response && response.status === 200) { const events = response.data; commit("SET_EVENTS", events); resolve(response.data); } }) .catch(() => { reject("Error"); }); }); }, LOGOUT({ commit, dispatch }) { // const pageNoRedirect = ["liste-signalements", "details-type-signalement", "details-signalement", "project_detail", "mentions", "aide", "index"] axios .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}logout/`) .then((response) => { if (response && response.status === 200) { commit("SET_USER", false); 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("/"); } }) .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 }) { return 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 }) { return 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_LEVELS_PERMISSIONS({ commit }) { return axios .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}levels-permissions/`) .then((response) => { if (response && response.status === 200) { commit("SET_LEVELS_PERMISSIONS", response.data); } }) .catch((error) => { throw error; }); }, async GET_PROJECT_INFO({ state, commit, dispatch }, slug) { 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; }, GET_PROJECT_LAST_MESSAGES({ commit }, project_slug) { return 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); } return response; }) .catch((error) => { throw error; }); }, } });