140 lines
4.5 KiB
TypeScript
140 lines
4.5 KiB
TypeScript
import axios from "axios";
|
|
import { Estilos, LoginState, OpcionLogin, Textos, UrlRol, UserRol } from "./types";
|
|
|
|
const state: LoginState = {
|
|
regiones: [],
|
|
grupos_de_compra: [],
|
|
region_elegida: undefined,
|
|
grupo_de_compra_elegido: undefined,
|
|
rol: undefined,
|
|
};
|
|
|
|
const mutations = {
|
|
setRegiones(state, { regiones }): void {
|
|
state.regiones = regiones;
|
|
},
|
|
setRegionYBarrios(state, { region, grupos_de_compra }): void {
|
|
state.region_elegida = region;
|
|
state.grupos_de_compra = grupos_de_compra;
|
|
},
|
|
selectGrupoDeCompra(state, { grupo_de_compra }): void {
|
|
state.grupo_de_compra_elegido = grupo_de_compra;
|
|
},
|
|
setRol(state, { rol }) {
|
|
state.rol = rol;
|
|
},
|
|
};
|
|
|
|
const actions = {
|
|
async getRegiones({ commit }): Promise<void> {
|
|
const { data } = await axios.get("/api/regiones");
|
|
commit('setRegiones', { regiones: data });
|
|
},
|
|
async selectRegion({ commit }, { region }): Promise<void> {
|
|
const { data } = await axios.get(`/api/regiones/${region}`);
|
|
commit('setRegionYBarrios', { region: region, grupos_de_compra: data });
|
|
},
|
|
async getRol({ commit }): Promise<void> {
|
|
const { data }: UserRol = await axios.get("/user/rol");
|
|
commit('setRol', { rol: data.rol });
|
|
}
|
|
};
|
|
|
|
const getters = {
|
|
urlRol(): UrlRol {
|
|
let split = window.location.pathname
|
|
.replace('login', '')
|
|
.split('/')
|
|
.filter(x => x.length);
|
|
return split[0] as UrlRol ?? 'pedido';
|
|
},
|
|
textos(): Textos {
|
|
let rol = getters.urlRol();
|
|
switch (rol) {
|
|
case 'admin':
|
|
return {
|
|
titulo: "Administración de Pedidos MPS",
|
|
subtitlo: "administración de pedidos",
|
|
password: "Contraseña de administración del barrio",
|
|
ayuda: "Si no la sabés, consultá a la comisión informática",
|
|
label: "Seleccioná tu región"
|
|
};
|
|
case 'comisiones':
|
|
return {
|
|
titulo: "Comisiones MPS",
|
|
subtitlo: "página de comisiones",
|
|
password: "Contraseña",
|
|
ayuda: "Si no la sabés, consultá a la comisión informática",
|
|
label: "Usuario"
|
|
};
|
|
case 'pedido':
|
|
return {
|
|
titulo: "Pedidos MPS",
|
|
subtitlo: "aplicación de pedidos",
|
|
password: "Contraseña",
|
|
ayuda: "Si no la sabés, consultá a la comisión informática",
|
|
label: "Seleccioná tu región"
|
|
};
|
|
default:
|
|
throw new Error("Url inválida");
|
|
}
|
|
},
|
|
estilos(): Estilos {
|
|
let rol = getters.urlRol();
|
|
switch (rol) {
|
|
case 'admin':
|
|
return {
|
|
fondo: "has-background-danger",
|
|
texto: "has-text-white",
|
|
botones: "is-warning",
|
|
};
|
|
case 'comisiones':
|
|
return {
|
|
fondo: "has-background-warning",
|
|
texto: "",
|
|
botones: "is-dark"
|
|
};
|
|
case 'pedido':
|
|
return {
|
|
fondo: "",
|
|
texto: "",
|
|
botones: "is-danger"
|
|
};
|
|
default:
|
|
throw new Error("Url inválida");
|
|
}
|
|
},
|
|
opcionesLogin(): OpcionLogin[] {
|
|
let rol = getters.urlRol();
|
|
switch (rol) {
|
|
case 'admin':
|
|
return [
|
|
{ nombre: "Pedidos", href: "/" },
|
|
{ nombre: "Comisiones", href: "/comisiones" }
|
|
];
|
|
case 'comisiones':
|
|
return [
|
|
{ nombre: "Pedidos", href: "/" },
|
|
{ nombre: "Administración", href: "/admin" }
|
|
];
|
|
case 'pedido':
|
|
return [
|
|
{ nombre: "Administración", href: "/admin" },
|
|
{ nombre: "Comisiones", href: "/comisiones" }
|
|
];
|
|
default:
|
|
throw new Error("Url inválida");
|
|
}
|
|
},
|
|
nombre() {
|
|
return `${state.grupo_de_compra_elegido}${ getters.urlRol() === 'admin' ? '_admin' : ''}`;
|
|
}
|
|
};
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
mutations,
|
|
actions,
|
|
getters,
|
|
};
|