Skip to content

Commit

Permalink
feat: seek pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Sep 13, 2022
1 parent 6a44750 commit 78d249b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 3 additions & 4 deletions app/controllers/me/pending-invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions app/utils/seek.js
Original file line number Diff line number Diff line change
@@ -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,
}
});
}
12 changes: 10 additions & 2 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
})))
}

Expand Down Expand Up @@ -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<CrateOwnerInvitation> = match pagination.page {
Expand Down Expand Up @@ -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 },
})
}

Expand All @@ -239,6 +246,7 @@ struct PrivateListResponse {
#[derive(Serialize)]
struct ResponseMeta {
next_page: Option<String>,
total: i64,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 78d249b

Please sign in to comment.