import axios from "axios"; import { AdminState } from "./types"; import { aplanarProducto } from "../../comunes"; const state: AdminState = { lastFetch: undefined, grupo_de_compra_id: undefined, nombre: undefined, devoluciones_habilitadas: undefined, pedidos: [], total_a_recaudar: undefined, total_sin_devoluciones: undefined, total_barrial: undefined, total_devoluciones: undefined, total_de_pedido: undefined, total_a_transferir: undefined, total_transporte: undefined, cantidad_transporte: undefined, saldo: undefined, }; const mutations = { setState(state: AdminState, { grupo_de_compra }) { const aplanarProductos = (pedido) => { pedido.productos = pedido.productos.map(p => aplanarProducto(p)); return pedido; }; state.lastFetch = new Date(); state.grupo_de_compra_id = grupo_de_compra.id; state.nombre = grupo_de_compra.nombre; state.devoluciones_habilitadas = grupo_de_compra.devoluciones_habilitadas; state.pedidos = grupo_de_compra.pedidos.map(p => aplanarProductos(p)); state.total_a_recaudar = grupo_de_compra.total_a_recaudar; state.total_sin_devoluciones = grupo_de_compra.total_sin_devoluciones; state.total_barrial = grupo_de_compra.total_barrial; state.total_devoluciones = grupo_de_compra.total_devoluciones; state.total_de_pedido = grupo_de_compra.total_de_pedido; state.total_a_transferir = grupo_de_compra.total_a_transferir; state.total_transporte = grupo_de_compra.total_transporte; state.cantidad_transporte = grupo_de_compra.cantidad_transporte; state.saldo = grupo_de_compra.saldo; }, toggleCaracteristica(state, { caracteristica_id }) { state[`${caracteristica_id}_habilitadas`] = !state[`${caracteristica_id}_habilitadas`]; } }; const actions = { async getGrupoDeCompra({ commit }) { const { data } = await axios.get('/user/grupo_de_compra'); commit('setState', data); }, async setAprobacionPedido({ commit, dispatch }, { pedido_id, aprobacion }){ await axios.post("/api/admin/subpedidos/" + pedido_id + "/aprobacion", { aprobacion: aprobacion }); await actions.getGrupoDeCompra({ commit }); dispatch("ui/toast", { mensaje: `Pedido ${aprobacion ? '' : 'des' }aprobado con éxito.` }, { root: true }); }, async toggleCaracteristica({ commit }, { caracteristica_id }) { await axios.post(`/api/grupos-de-compra/${state.grupo_de_compra_id}/${caracteristica_id}`); commit('toggleCaracteristica', { caracteristica_id: caracteristica_id }) }, }; const getters = { grupoDeCompraDefinido() { return state.lastFetch !== undefined; }, hayPedidos() { return state.pedidos?.length > 0; }, pedidosAprobados() { return state.pedidos?.filter(p => p.aprobado) ?? []; }, hayAprobados() { return getters.pedidosAprobados().length !== 0; }, getPedido() { return (pedido_id) => state.pedidos?.find(p => p.id === pedido_id); }, getCaracteristica() { return (caracteristica) => state[`${caracteristica}_habilitadas`]; } }; export default { namespaced: true, state, mutations, actions, getters, };