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

@@ -208,7 +208,7 @@
<option disabled value="">-- Seleccionar rol --</option> <option disabled value="">-- Seleccionar rol --</option>
<option value="owner">Dueño</option> <option value="owner">Dueño</option>
<option value="manager">Gerente</option> <option value="manager">Gerente</option>
<option value="driver">Conductor</option> <option v-if="authStore.user?.permissions.includes('role_carrier')" value="driver">Conductor</option>
</select> </select>
</div> </div>
<div class="mb-4 mt-3"> <div class="mb-4 mt-3">

View File

@@ -23,7 +23,7 @@
const loading = ref(false); const loading = ref(false);
onMounted(() => { onMounted(() => {
drivers.value = companyStore.users?.filter((u) => u.job_role == 'driver'); drivers.value = companyStore.drivers;
if(props?.vehicle?.driver) { if(props?.vehicle?.driver) {
const index = drivers.value.findIndex((d) => d._id === props.vehicle.driver?._id); const index = drivers.value.findIndex((d) => d._id === props.vehicle.driver?._id);
driverSelected.value = drivers.value[index]; driverSelected.value = drivers.value[index];

View File

@@ -70,7 +70,7 @@
zoom.value = 4; zoom.value = 4;
heightMap.value = 420; heightMap.value = 420;
} }
if(companyStore.locations.length <= 0) { if(companyStore.locationsLoads.length <= 0) {
getLocations(); getLocations();
} }
formLoad.owner = auth.user?.first_name + ' ' + auth.user?.last_name; formLoad.owner = auth.user?.first_name + ' ' + auth.user?.last_name;
@@ -144,8 +144,8 @@
const getLocations = async() => { const getLocations = async() => {
loadingLocations.value = true; loadingLocations.value = true;
filterQueryVehicles.value.company = "company="+ localStorage.getItem('id'); // filterQueryVehicles.value.company = "company="+ localStorage.getItem('id');
await companyStore.getLocationsCompany(filterQueryVehicles.value, false) await companyStore.getLocationsLoads()
loadingLocations.value = false; loadingLocations.value = false;
} }
@@ -440,7 +440,7 @@
v-model="locationLoadSelected" v-model="locationLoadSelected"
> >
<option disabled value="">-- Seleccionar locación --</option> <option disabled value="">-- Seleccionar locación --</option>
<option v-for="loc in companyStore.locations" :value="loc">{{ loc.branch_name }}</option> <option v-for="loc in companyStore.locationsLoads" :value="loc">{{ loc.branch_name }}</option>
</select> </select>
</div> </div>
<Custominput <Custominput

View File

@@ -23,9 +23,9 @@ export const updateCompany = async(companyId, formData) => {
} }
} }
export const getUsers = async(companyId) => { export const getUsers = async(filter) => {
try { try {
const endpoint = `/users?company=${companyId}`; const endpoint = `/users?${filter}`;
const {data} = await api.get(endpoint); const {data} = await api.get(endpoint);
return data; return data;
} catch (error) { } catch (error) {
@@ -115,7 +115,7 @@ export const deleteBudget = async(id) => {
export const getLocations = async(filter) => { export const getLocations = async(filter) => {
try { try {
const endpoint = `/branches/${filter}&$limit=10&$sort%5BcreatedAt%5D=-1`; const endpoint = `/branches/${filter}&$limit=3&$sort%5BcreatedAt%5D=-1`;
console.log(endpoint); console.log(endpoint);
const {data} = await api.get(endpoint); const {data} = await api.get(endpoint);
return data; return data;

View File

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

View File

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

View File

@@ -19,7 +19,7 @@ import Pagination from '../components/Pagination.vue';
getInitData(); getInitData();
}) })
const limit = 10; const limit = 3;
const getInitData = async() => { const getInitData = async() => {
loading.value = true; loading.value = true;

View File

@@ -4,6 +4,7 @@
import CardUser from '../components/CardUser.vue'; import CardUser from '../components/CardUser.vue';
import { useCompanyStore } from '../stores/company'; import { useCompanyStore } from '../stores/company';
import CreateUserModal from '../components/CreateUserModal.vue'; import CreateUserModal from '../components/CreateUserModal.vue';
import Pagination from '../components/Pagination.vue';
const companyStore = useCompanyStore(); const companyStore = useCompanyStore();
@@ -13,11 +14,20 @@
const currentUser = ref(null); const currentUser = ref(null);
const openModal = ref(false); const openModal = ref(false);
const limit = 3;
const getInitData = async() => { const getInitData = async() => {
console.log('callll') console.log('callll')
loading.value = true; loading.value = true;
await companyStore.getUsersCompany(); await companyStore.getUsersCompany(limit);
loading.value = false;
}
const getUsersByPage = async(data) => {
console.log(data)
loading.value = true;
companyStore.usersCurrentPage = data.page
await companyStore.getUsersCompany(limit, data.skip, true);
loading.value = false; loading.value = false;
} }
@@ -61,6 +71,12 @@
:readonly="false" :readonly="false"
@set-user="handleSetCurrentUser(user)" @set-user="handleSetCurrentUser(user)"
/> />
<Pagination
:limit="limit"
:total="companyStore.usersTotal"
:current-page="companyStore.usersCurrentPage"
@get-elements="getUsersByPage"
/>
</div> </div>
</div> </div>
</template> </template>

View File

@@ -28,7 +28,7 @@
loading.value = true; loading.value = true;
filterQuery.value.company = "company="+ localStorage.getItem('id'); filterQuery.value.company = "company="+ localStorage.getItem('id');
await vehicleStore.fetchVehicles(filterQuery.value, false); await vehicleStore.fetchVehicles(filterQuery.value, false);
await companyStore.getUsersCompany(); await companyStore.getDrivers();
loading.value = false; loading.value = false;
} }