Mercurial > gemma
view client/src/store/usermanagement.js @ 1576:b5d7770fe632
fix: error with translations
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 13 Dec 2018 13:41:27 +0100 |
parents | d0c4d17ee7ab |
children | a6eea3002e6e |
line wrap: on
line source
/* This is Free Software under GNU Affero General Public License v >= 3.0 * without warranty, see README.md and license for details. * * SPDX-License-Identifier: AGPL-3.0-or-later * License-Filename: LICENSES/AGPL-3.0.txt * * Copyright (C) 2018 by via donau * – Österreichische Wasserstraßen-Gesellschaft mbH * Software engineering by Intevation GmbH * * Author(s): * Thomas Junk <thomas.junk@intevation.de> * Markus Kottländer <markus@intevation.de> */ import { HTTP } from "../lib/http"; // initial state const init = () => { return { users: null, currentUser: null, userDetailsVisible: false }; }; const newUser = () => { return { user: "", email: "", country: null, role: null, isNew: true, password: "", roleLabel: "" }; }; export default { init, namespaced: true, state: init(), getters: { isUserDetailsVisible: state => { return state.userDetailsVisible; }, currentUser: state => { return state.currentUser; }, users: state => { return state.users; }, getUserByName: state => name => { return state.users.find(user => { return user.user === name; }); } }, mutations: { setUserDetailsInvisible: state => { state.userDetailsVisible = false; }, setUserDetailsVisible: state => { state.userDetailsVisible = true; }, usersLoaded: (state, data) => { const resolveLabel = x => { const labels = { waterway_user: "Waterway User", waterway_admin: "Waterway Administrator", sys_admin: "System Admininistrator" }; return labels[x]; }; let users = data.users.map(u => { u["roleLabel"] = resolveLabel(u["role"]); return u; }); state.users = users; }, setCurrentUser: (state, data) => { state.currentUser = data; state.userDetailsVisible = true; }, clearCurrentUser: state => { state.currentUser = newUser(); } }, actions: { deleteUser({ commit }, data) { const { name } = data; return new Promise((resolve, reject) => { HTTP.delete("/users/" + name, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("clearCurrentUser"); commit("setUserDetailsInvisible"); resolve(response); }) .catch(error => { reject(error); }); }); }, saveCurrentUser({ commit }, data) { const { path, user } = data; if (user.isNew) { return new Promise((resolve, reject) => { HTTP.post("/users", user, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("setUserDetailsInvisible"); commit("clearCurrentUser"); resolve(response); }) .catch(error => { reject(error); }); }); } else { return new Promise((resolve, reject) => { HTTP.put("/users/" + path, user, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("setUserDetailsInvisible"); commit("clearCurrentUser"); resolve(response); }) .catch(error => { reject(error); }); }); } }, loadUsers({ commit }) { return new Promise((resolve, reject) => { HTTP.get("/users", { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { commit("usersLoaded", response.data); resolve(response); }) .catch(error => { reject(error); }); }); } } };