Skip to content
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

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

Closed
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions app/components/seek-pagination.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#if @pagination.nextPage}}
<nav local-class='seek-pagination' aria-label="Pagination navigation">
<LinkTo @query={{hash [email protected]}} local-class="next" rel="next" title="Goes to next page" data-test-pagination-next>
Next Page
</LinkTo>
</nav>
{{/if}}
5 changes: 5 additions & 0 deletions app/components/seek-pagination.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.seek-pagination {
text-align: center;
font-size: 90%;
margin-bottom: 20px;
}
10 changes: 10 additions & 0 deletions app/controllers/me/pending-invites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Controller from '@ember/controller';

import { reads } from 'macro-decorators';

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

export default class PendingInvitesController extends Controller {
@reads('model.meta.next_page') nextPage;
@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 = {
seek: { 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>

<SeekPagination @pagination={{this.pagination}} />
12 changes: 12 additions & 0 deletions app/utils/seek-pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import macro from 'macro-decorators';

export function pagination() {
return macro(function () {
let { nextPage, totalItems } = this;

return {
nextPage,
totalItems,
};
});
}
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
20 changes: 10 additions & 10 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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adds meta to the response similar to how its done for crates endpoint, this way we can retrieve the seek hash for the next page.

Copy link
Member

Choose a reason for hiding this comment

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

sorry that I just noticed this now, but #3763 introduced a new endpoint for the invitations (GET /api/private/crate_owner_invitations) that already supports seek-based pagination. I guess it would be best if we first switched to this new endpoint in the frontend in a dedicated MR and then implemented pagination on top of it. let me know if you want to tackle this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Lets do that!
So we close this PR?

Copy link
Member

Choose a reason for hiding this comment

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

whatever you prefer. feel free to keep it open and then rebase it once the other stuff is done :)

})))
}

Expand Down Expand Up @@ -147,15 +150,12 @@ fn prepare_list(
raw_invitations.pop();

if let Some(last) = raw_invitations.last() {
let mut params = IndexMap::new();
params.insert(
"seek".into(),
crate::controllers::helpers::pagination::encode_seek((
last.crate_id,
last.invited_user_id,
))?,
);
Some(req.query_with_params(params))
let seek_key = crate::controllers::helpers::pagination::encode_seek((
last.crate_id,
last.invited_user_id,
))?;

Some(seek_key)
EstebanBorai marked this conversation as resolved.
Show resolved Hide resolved
} else {
None
}
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