Skip to content

Add pagination to the "Pending Owner Invites" page #5185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/controllers/me/pending-invites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';

import { reads } from 'macro-decorators';

import { pagination } from '../../utils/pagination';

export default class PendingInvitesController extends Controller {
queryParams = ['page', 'per_page'];
@tracked page = '1';
@tracked per_page = 10;

@reads('model.meta.total') totalItems;
@pagination() pagination;
}
8 changes: 6 additions & 2 deletions app/routes/me/pending-invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import AuthenticatedRoute from '../-authenticated-route';
export default class PendingInvitesRoute extends AuthenticatedRoute {
@service store;

model() {
return this.store.findAll('crate-owner-invite');
queryParams = {
page: { refreshModel: true },
};

model(params) {
return this.store.query('crate-owner-invite', params);
}
}
1 change: 1 addition & 0 deletions app/styles/me/pending-invites.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.list {
background: white;
margin-bottom: 2rem;
}

.row {
Expand Down
2 changes: 2 additions & 0 deletions app/templates/me/pending-invites.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
<p local-class="row" data-test-empty-state>You don't seem to have any pending invitations.</p>
{{/each}}
</div>

<Pagination @pagination={{this.pagination}} />
102 changes: 102 additions & 0 deletions mirage/fixtures/crate-owner-invitations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
export default [
{
inviteeId: 1,
inviterId: 2,
crateId: 'mock-dev-deps',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'postgres',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'nom',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rusted_cypher',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'nanomsg',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'postgres',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'nom',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'nanomsg',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'external_mixin',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rustless',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'serde',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rustful',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'quickcheck',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rusted_cypher',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'external_mixin',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rustless',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'serde',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'rustful',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'quickcheck',
},
{
inviteeId: 1,
inviterId: 2,
crateId: 'mock-dev-deps',
},
];
12 changes: 10 additions & 2 deletions mirage/route-handlers/me.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Response } from 'miragejs';

import { getSession } from '../utils/session';
import { pageParams } from './-utils';

export function register(server) {
server.get('/api/v1/me', function (schema) {
Expand Down Expand Up @@ -98,13 +99,20 @@ export function register(server) {
return { ok: true };
});

server.get('/api/v1/me/crate_owner_invitations', function (schema) {
server.get('/api/v1/me/crate_owner_invitations', function (schema, request) {
let { user } = getSession(schema);
if (!user) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

return schema.crateOwnerInvitations.where({ inviteeId: user.id });
const { start, end } = pageParams(request);
const invites = schema.crateOwnerInvitations.where({ inviteeId: user.id });

let response = this.serialize(invites.slice(start, end));

response.meta = { total: invites.length };

return response;
});

server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
Expand Down
4 changes: 3 additions & 1 deletion mirage/serializers/crate-owner-invitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default BaseSerializer.extend({
delete hash.id;
delete hash.token;

hash.crate_id = Number(hash.crate_id);
// TODO: Check this further, `crate_id` are strings in the current fixtures
// when attempting to parse into number we will get `NaN`s here.
// hash.crate_id = Number(hash.crate_id);
Comment on lines -27 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing some trouble when retrieving crates by its ID, given that crates IDs are actually strings with the name of such crate.


let crate = this.schema.crates.find(hash.crate_id);
hash.crate_name = crate.name;
Expand Down
11 changes: 10 additions & 1 deletion src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,16 @@ fn prepare_list(
)
.load(&*conn)?
}
Page::Numeric(_) => unreachable!("page-based pagination is disabled"),
Page::Numeric(_) => query
.pages_pagination(pagination)
.filter(
crate_owner_invitations::crate_id.gt(seek_key.0).or(
crate_owner_invitations::crate_id
.eq(seek_key.0)
.and(crate_owner_invitations::invited_user_id.gt(seek_key.1)),
),
)
.load(&*conn)?,
};
let next_page = if raw_invitations.len() > pagination.per_page as usize {
// We fetch `per_page + 1` to check if there are records for the next page. Since the last
Expand Down
3 changes: 2 additions & 1 deletion tests/mirage/me/crate-owner-invitations/list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module('Mirage | GET /api/v1/me/crate_owner_invitations', function (hooks) {

let response = await fetch('/api/v1/me/crate_owner_invitations');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), { crate_owner_invitations: [] });
assert.deepEqual(await response.json(), { crate_owner_invitations: [], meta: { total: 0 } });
});

test('returns the list of invitations for the authenticated user', async function (assert) {
Expand Down Expand Up @@ -47,6 +47,7 @@ module('Mirage | GET /api/v1/me/crate_owner_invitations', function (hooks) {
let response = await fetch('/api/v1/me/crate_owner_invitations');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
meta: { total: 0 },
crate_owner_invitations: [
{
crate_id: Number(nanomsg.id),
Expand Down