add: pagination of users

This commit is contained in:
Alexandro Uc Santos
2024-02-05 21:26:57 -06:00
parent 1933603692
commit 2df3bcfcfb
10 changed files with 139 additions and 39 deletions

View File

@@ -4,12 +4,14 @@ import { useRouter } from 'vue-router';
import { renewToken } from '../services/auth';
import {useNotificationsStore} from './notifications';
import {useCompanyStore} from './company';
import { useLoadsStore } from "./loads";
export const useAuthStore = defineStore('auth', () => {
const router = useRouter();
const noty = useNotificationsStore();
const company = useCompanyStore();
const loadStore = useLoadsStore();
const sesion = ref('')
const checking = ref(false);
const authStatus = ref('checking');
@@ -56,6 +58,7 @@ export const useAuthStore = defineStore('auth', () => {
sesion.value = '';
token.value = '';
company.clear();
localStorage.clear();
user.value = null;
console.log(company.company);
localStorage.removeItem('access');

View File

@@ -8,8 +8,12 @@ export const useCompanyStore = defineStore('company', () => {
const company = ref(null)
const users = ref([]);
const drivers = ref([]);
const usersTotal = ref(0);
const usersCurrentPage = ref(1);
const budgets = ref([]);
const locations = ref([]);
const locationsLoads = ref([]);
const locationsTotal = ref(0);
const locationsCurrentPage = ref(1);
const proposals = ref([])
@@ -21,19 +25,18 @@ export const useCompanyStore = defineStore('company', () => {
if(!company.value) {
loading.value = true;
const resp = await getCompany(companyId);
console.log(resp);
company.value = resp;
}
console.log(company.value);
loading.value = false;
}
const getUsersCompany = async() => {
const getUsersCompany = async(limit = 10, skip = 0, reload = false) => {
const companyId = localStorage.getItem('id');
if(users.value.length <= 0) {
const resp = await getUsers(companyId);
console.log(resp);
if(users.value.length <= 0 || reload === true) {
const filter = `company=${companyId}&&$sort%5BcreatedAt%5D=-1&$limit=${limit}&$skip=${skip}`
const resp = await getUsers(filter);
if(resp !== null && resp.total > 0) {
usersTotal.value = resp.total;
users.value = resp.data;
} else {
users.value = [];
@@ -41,14 +44,34 @@ export const useCompanyStore = defineStore('company', () => {
}
}
const getDrivers = async() => {
const companyId = localStorage.getItem('id');
if(drivers.value.length <= 0) {
const filter = `company=${companyId}&$sort%5BcreatedAt%5D=-1&$limit=100&$skip=0&job_role=driver`
const resp = await getUsers(filter);
if(resp !== null && resp.total > 0) {
drivers.value = resp.data;
} else {
drivers.value = [];
}
}
}
const createUserCompany = async(formData, localData) => {
const data = await createUser(formData);
if(data) {
users.value.push({
users.value.unshift({
...data,
...localData
});
usersTotal.value++;
if(data.job_role === 'driver' && drivers.value.length > 0) {
drivers.value.unshift({
...data,
...localData
})
}
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
@@ -59,11 +82,23 @@ export const useCompanyStore = defineStore('company', () => {
const data = await updateUser(id, formData);
if(data) {
const index = users.value.findIndex((user) => user._id === id);
users.value[index] = {
...users.value[index],
...data,
...localData
};
if(index !== -1) {
users.value[index] = {
...users.value[index],
...data,
...localData
};
if(data.job_role === 'driver' && drivers.value.length > 0) { // Actualizamos en la lista drivers
const indexd = drivers.value.findIndex((user) => user._id === id);
if(indexd !== -1) {
drivers.value[indexd] = {
...drivers.value[index],
...data,
...localData
};
}
}
}
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
@@ -74,7 +109,9 @@ export const useCompanyStore = defineStore('company', () => {
const data = await deleteUser(id);
if(data) {
users.value = users.value.filter(user => user._id !== id);
if(data.job_role === 'driver' && drivers.value.length > 0) {
drivers.value = drivers.value.filter(user => user._id !== id);
}
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
@@ -95,11 +132,18 @@ export const useCompanyStore = defineStore('company', () => {
}
}
const clear = () => {
const clear = () => { //Cuando se cierra la sesion
company.value = null;
users.value = [];
usersTotal.value = 0;
usersCurrentPage.value = 1;
drivers.value = [];
budgets.value = [];
proposals.value = [];
locations.value = [];
locationsLoads.value = [];
locationsTotal.value = 0;
locationsCurrentPage.value = 1;
// companyid = null;
loading.value = false;
}
@@ -119,16 +163,13 @@ export const useCompanyStore = defineStore('company', () => {
const endpoint = `/proposals?carrier=${companyId}`;
const {data} = await api.get(endpoint);
proposals.value = data.data;
console.log(data);
} catch (error) {
proposals.value = [];
console.log(error);
}
}
const createPropsal = async(formData) => {
const data = await saveProposal(formData);
console.log(data);
if(data) {
return 'success';
} else {
@@ -139,7 +180,6 @@ export const useCompanyStore = defineStore('company', () => {
const updatePropsalLoad = async(id, formData, localData) => {
const data = await updateProposal(id, formData);
if(data) {
console.log(data);
const index = proposals.value.findIndex((prop) => prop._id === id);
proposals.value[index] = {
...proposals.value[index],
@@ -156,7 +196,6 @@ export const useCompanyStore = defineStore('company', () => {
let filterArr = Object.values(filterQuery);
let cleanfilterArr = filterArr.filter(n=>n);
// console.log(cleanfilterArr);
var filterStr = "";
if(cleanfilterArr.length >0){
filterStr ="?"+cleanfilterArr.join("&");
@@ -179,7 +218,6 @@ export const useCompanyStore = defineStore('company', () => {
const updateBudgetCompany = async(id, formData, localData) => {
try {
const data = await updateBudget(id, formData);
console.log(localData);
if(data) {
const index = budgets.value.findIndex((budget) => budget._id === id);
budgets.value[index] = {
@@ -216,7 +254,6 @@ export const useCompanyStore = defineStore('company', () => {
const deleteBudgetCompany = async(id) => {
try {
const data = await deleteBudget(id);
console.log(data);
if(data) {
budgets.value = budgets.value.filter(budget => budget._id !== id);
return data;
@@ -238,7 +275,6 @@ export const useCompanyStore = defineStore('company', () => {
}
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;
locationsTotal.value = resp.total;
@@ -248,6 +284,18 @@ export const useCompanyStore = defineStore('company', () => {
}
}
const getLocationsLoads = async() => {
if(locationsLoads.value.length <= 0) {
const filterStr = "?company="+ localStorage.getItem('id') + '&$limit=100'
const resp = await getLocations(filterStr);
if(resp !== null && resp.total > 0) {
locationsLoads.value = resp.data;
} else {
locationsLoads.value = [];
}
}
}
const createLocationCompany = async(formData, localData) => {
const data = await createLocation(formData);
if(data) {
@@ -256,6 +304,10 @@ export const useCompanyStore = defineStore('company', () => {
...localData
});
locationsTotal.value++;
locationsLoads.value.unshift({
...data,
...localData
})
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
@@ -266,11 +318,21 @@ export const useCompanyStore = defineStore('company', () => {
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
};
if(index !== -1) {
locations.value[index] = {
...locations.value[index],
...data,
...localData
};
if(locationsLoads.value.length > 0) {
const indexl = locationsLoads.value.findIndex((loc) => loc._id === id);
locationsLoads.value[indexl] = {
...locationsLoads.value[index],
...data,
...localData
};
}
}
return 'success';
} else {
return 'Algo salio mal, intente más tarde';
@@ -281,6 +343,9 @@ export const useCompanyStore = defineStore('company', () => {
const data = await deleteLocation(id);
if(data) {
locations.value = locations.value.filter(loc => loc._id !== id);
if(locationsLoads.value.length > 0) {
locationsLoads.value = locationsLoads.value.filter(loc => loc._id !== id);
}
return 'success';
} else {
@@ -296,6 +361,7 @@ export const useCompanyStore = defineStore('company', () => {
updatePropsalLoad,
getBudgetsCompany,
getUsersCompany,
getDrivers,
createUserCompany,
updateUserCompany,
deleteUserCompany,
@@ -304,12 +370,17 @@ export const useCompanyStore = defineStore('company', () => {
createBudgetCompany,
deleteBudgetCompany,
getLocationsCompany,
getLocationsLoads,
createLocationCompany,
updateLocationCompany,
deleteLocationCompany,
budgets,
users,
drivers,
usersTotal,
usersCurrentPage,
locations,
locationsLoads,
locationsTotal,
locationsCurrentPage,
clear,

View File

@@ -112,8 +112,18 @@ export const useLoadsStore = defineStore('load', () => {
}
}
const clear = () => {
currentLoad.value = null;
loads.value = [];
proposalsOfLoads.value = [];
openModalEdit.value = false;
openAttachmentsModal.value = false;
openProposalsModal.value = false;
}
return {
clear,
openModalEdit,
openProposalsModal,
openAttachmentsModal,