add: crud locations
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user