From 78d249bb080dcbcbf61cefc6979056bee2053b87 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Mon, 12 Sep 2022 22:17:38 -0300 Subject: [PATCH] feat: seek pagination support --- app/controllers/me/pending-invites.js | 7 +++---- app/utils/seek.js | 15 +++++++++++++++ src/controllers/crate_owner_invitation.rs | 12 ++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 app/utils/seek.js diff --git a/app/controllers/me/pending-invites.js b/app/controllers/me/pending-invites.js index c0deefeb23e..84fb01091fe 100644 --- a/app/controllers/me/pending-invites.js +++ b/app/controllers/me/pending-invites.js @@ -3,12 +3,11 @@ import { tracked } from '@glimmer/tracking'; import { reads } from 'macro-decorators'; -import { pagination } from '../../utils/pagination'; +import { pagination } from '../../utils/seek'; export default class PendingInvitesController extends Controller { - queryParams = ['page', 'per_page']; - @tracked page = '1'; - @tracked per_page = 10; + queryParams = ['seek']; + @tracked seek = 'WzEsIDFd'; @reads('model.meta.total') totalItems; @pagination() pagination; diff --git a/app/utils/seek.js b/app/utils/seek.js new file mode 100644 index 00000000000..592c33c501a --- /dev/null +++ b/app/utils/seek.js @@ -0,0 +1,15 @@ +import macro from 'macro-decorators'; + +const VIEWABLE_PAGES = 9; + +export function pagination() { + return macro(function () { + let { page, per_page: perPage, totalItems } = this; + + return { + page, + perPage, + totalItems, + } + }); +} diff --git a/src/controllers/crate_owner_invitation.rs b/src/controllers/crate_owner_invitation.rs index 35ec3642fc0..69eb3ab7c69 100644 --- a/src/controllers/crate_owner_invitation.rs +++ b/src/controllers/crate_owner_invitation.rs @@ -20,7 +20,9 @@ pub fn list(req: &mut dyn RequestExt) -> EndpointResult { let user_id = auth.user_id(); let PrivateListResponse { - invitations, users, .. + invitations, + users, + meta, } = prepare_list(req, auth, ListFilter::InviteeId(user_id))?; // The schema for the private endpoints is converted to the schema used by v1 endpoints. @@ -47,6 +49,7 @@ pub fn list(req: &mut dyn RequestExt) -> EndpointResult { Ok(req.json(&json!({ "crate_owner_invitations": crate_owner_invitations, "users": users, + "meta": meta, }))) } @@ -123,6 +126,10 @@ fn prepare_list( )) // We fetch one element over the page limit to then detect whether there is a next page. .limit(pagination.per_page as i64 + 1); + let total = crate_owner_invitations::table + .count() + .get_result(&*conn) + .unwrap(); // Load and paginate the results. let mut raw_invitations: Vec = match pagination.page { @@ -225,7 +232,7 @@ fn prepare_list( Ok(PrivateListResponse { invitations, users: users.into_iter().map(|(_, user)| user.into()).collect(), - meta: ResponseMeta { next_page }, + meta: ResponseMeta { next_page, total }, }) } @@ -239,6 +246,7 @@ struct PrivateListResponse { #[derive(Serialize)] struct ResponseMeta { next_page: Option, + total: i64, } #[derive(Deserialize)]