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();
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: {
Sébastien DA ROCHA
committed
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,
},
mutations: {
Sébastien DA ROCHA
committed
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
},
},
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), // 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]);
Sébastien DA ROCHA
committed
}
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));
Sébastien DA ROCHA
committed
}
})
.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 : "/";
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
}
})
if (error.response.status === 403) {
commit('error', error.response.data.detail);
}
Sébastien DA ROCHA
committed
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 ?
}
Sébastien DA ROCHA
committed
.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);
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;
});
},
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) {
Sébastien DA ROCHA
committed
}
})
.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) {
Sébastien DA ROCHA
committed
}
})
.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);
Sébastien DA ROCHA
committed
}
})
.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;
});
}, */
Sébastien DA ROCHA
committed
/* 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;
});
Sébastien DA ROCHA
committed
/* 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;
});
},
Sébastien DA ROCHA
committed
/* 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);
}, */
}
});