From 245941a9b469cbc9e1c53ddc65be3713b87ed4c9 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Mon, 29 Jan 2024 15:45:34 +0200 Subject: [PATCH] Adding pagination --- client/src/clients/api/users.ts | 4 +-- client/src/types/api.ts | 11 ++++++--- client/src/views/UsersView.vue | 44 +++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/client/src/clients/api/users.ts b/client/src/clients/api/users.ts index 5294c121..ab4c4aca 100644 --- a/client/src/clients/api/users.ts +++ b/client/src/clients/api/users.ts @@ -23,9 +23,7 @@ export class UsersApi extends ApiClientBase { } list(query?: any) { - return this.unwrap(this.$http.get>(this.getUrl('', query)), { - transform: (d) => d.results - }) + return this.unwrap(this.$http.get>(this.getUrl('', query))) } getuser(id: number, options: Api.UnwrapOptions = {}) { return this.unwrap(this.$http.get(this.getUrl(`/${id}`)), { diff --git a/client/src/types/api.ts b/client/src/types/api.ts index 2d76f0e1..53fd07f8 100644 --- a/client/src/types/api.ts +++ b/client/src/types/api.ts @@ -41,7 +41,7 @@ export module Api { applying_user: number | any approval_user: number user?: Api.User - isUpdated? : boolean + isUpdated?: boolean } export interface LeaveReason { @@ -142,6 +142,11 @@ export module Api { export type Meeting = MsgRes export type Profile = MsgRes + export type Users = MsgRes<{ + count: number + users: User[] + }> + export type AllMeetings = MsgRes export type Login = MsgRes<{ @@ -259,10 +264,10 @@ export module Api { className: string eventName: string vacation?: any - meeting? :any + meeting?: any event?: any holidays?: any - users?:any + users?: any date: any len?: number } diff --git a/client/src/views/UsersView.vue b/client/src/views/UsersView.vue index ace6fd48..a373fad3 100644 --- a/client/src/views/UsersView.vue +++ b/client/src/views/UsersView.vue @@ -3,7 +3,7 @@ - + @@ -11,11 +11,16 @@ - - + + + + + + + @@ -38,13 +43,22 @@ export default { const offices = ref([]); const $route = useRoute() let isFirstLoad = true; + const page = ref(1); + const count = ref(0); - useAsyncState($api.users.list({ "location_id": $route.query.location_id }), [], { + useAsyncState($api.users.list({ "location_id": $route.query.location_id, "page": page.value }), undefined, { onSuccess(data) { - users.execute(undefined, data) + if (data?.count) { + count.value = data.count / 10 + count.value = Math.ceil((data.count / 10)); + + } + users.execute(undefined, data?.results || []); } - }) + }); + + const users = useAsyncState( async (users: Api.User[]) => { if (isFirstLoad) { @@ -65,9 +79,21 @@ export default { watch( () => $route.query.location_id, async newValue => { - useAsyncState($api.users.list({ "location_id": $route.query.location_id }), [], { + page.value = 1 + useAsyncState($api.users.list({ "location_id": $route.query.location_id, "page": page.value }), undefined, { + onSuccess(data) { + users.execute(undefined, data?.results || []); + } + }) + }, + ); + + watch( + () => page.value, + async newValue => { + useAsyncState($api.users.list({ "location_id": $route.query.location_id, "page": page.value }), undefined, { onSuccess(data) { - users.execute(undefined, data) + users.execute(undefined, data?.results || []); } }) }, @@ -77,6 +103,8 @@ export default { return { users, + page, + count, offices, UserCard, officeFilters,