add: crud locations

This commit is contained in:
Alexandro Uc Santos
2024-01-09 18:49:51 -06:00
parent 6ebeb0f4a2
commit 937c3a8fe5
8 changed files with 678 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget, getUsers, updateUser, createUser, deleteUser } from "../services/company";
import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget, getUsers, updateUser, createUser, deleteUser, getLocations, createLocation, updateLocation, deleteLocation } from "../services/company";
import api from "../lib/axios";
export const useCompanyStore = defineStore('company', () => {
@@ -8,6 +8,7 @@ export const useCompanyStore = defineStore('company', () => {
const company = ref(null)
const users = ref([]);
const budgets = ref([]);
const locations = ref([]);
const proposals = ref([])
const loading = ref(false);
@@ -196,6 +197,64 @@ export const useCompanyStore = defineStore('company', () => {
}
}
const getLocationsCompany = async(filterQuery, reload = false) => {
let filterArr = Object.values(filterQuery);
let cleanfilterArr = filterArr.filter(n=>n);
var filterStr = "";
if(cleanfilterArr.length > 0){
filterStr ="?"+cleanfilterArr.join("&");
}
if(locations.value.length <= 0 || reload === true) {
const resp = await getLocations(filterStr);
console.log(resp);
if(resp !== null && resp.total > 0) {
locations.value = resp.data;
} else {
locations.value = [];
}
}
}
const createLocationCompany = async(formData, localData) => {
const data = await createLocation(formData);
if(data) {
locations.value.push({
...data,
...localData
});
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
}
}
const updateLocationCompany = async(id, formData, localData) => {
const data = await updateLocation(id, formData);
if(data) {
const index = locations.value.findIndex((loc) => loc._id === id);
locations.value[index] = {
...locations.value[index],
...data,
...localData
};
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
}
}
const deleteLocationCompany = async(id) => {
const data = await deleteLocation(id);
if(data) {
locations.value = locations.value.filter(loc => loc._id !== id);
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
}
}
return {
getCompanyData,
@@ -209,8 +268,13 @@ export const useCompanyStore = defineStore('company', () => {
updateBudgetCompany,
createBudgetCompany,
deleteBudgetCompany,
getLocationsCompany,
createLocationCompany,
updateLocationCompany,
deleteLocationCompany,
budgets,
users,
locations,
clear,
$reset,
loading,