Fix pagination

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-02-12 14:18:50 -08:00
parent bbc037ad6e
commit 743ec49972
14 changed files with 71 additions and 68 deletions

View File

@@ -16,14 +16,14 @@ import { Link } from "react-router";
import Fuse from "fuse.js"; import Fuse from "fuse.js";
import type { VendorListPageQuery as VendorListPageQueryType } from "./__generated__/VendorListPageQuery.graphql"; import type { VendorListPageQuery as VendorListPageQueryType } from "./__generated__/VendorListPageQuery.graphql";
import { Helmet } from "react-helmet-async"; import { Helmet } from "react-helmet-async";
const ITEMS_PER_PAGE = 2; const ITEMS_PER_PAGE = 4;
const vendorListPageQuery = graphql` const vendorListPageQuery = graphql`
query VendorListPageQuery($count: Int!, $after: CursorKey, $before: CursorKey) { query VendorListPageQuery($first: Int, $after: CursorKey, $last: Int, $before: CursorKey) {
node(id: "AZSfP_xAcAC5IAAAAAAltA") { node(id: "AZSfP_xAcAC5IAAAAAAltA") {
id id
... on Organization { ... on Organization {
vendors(first: $count, after: $after, before: $before) { vendors(first: $first, after: $after, last: $last, before: $before) {
edges { edges {
node { node {
id id
@@ -70,7 +70,7 @@ function VendorListContent({
onPageChange, onPageChange,
}: { }: {
queryRef: PreloadedQuery<VendorListPageQueryType>; queryRef: PreloadedQuery<VendorListPageQueryType>;
onPageChange: (params: { before?: string; after?: string }) => void; onPageChange: (params: { first?: number; after?: string; last?: number; before?: string }) => void;
}) { }) {
const data = usePreloadedQuery(vendorListPageQuery, queryRef); const data = usePreloadedQuery(vendorListPageQuery, queryRef);
const [searchParams, setSearchParams] = useSearchParams(); const [searchParams, setSearchParams] = useSearchParams();
@@ -95,8 +95,8 @@ function VendorListContent({
startTransition(() => { startTransition(() => {
const params = direction === 'next' const params = direction === 'next'
? { after: pageInfo.endCursor } ? { first: ITEMS_PER_PAGE, after: pageInfo.endCursor }
: { before: pageInfo.startCursor }; : { last: ITEMS_PER_PAGE, before: pageInfo.startCursor };
setSearchParams(prev => { setSearchParams(prev => {
if (direction === 'next') { if (direction === 'next') {
@@ -268,18 +268,20 @@ export default function VendorListPage() {
const before = searchParams.get('before'); const before = searchParams.get('before');
loadQuery({ loadQuery({
count: ITEMS_PER_PAGE, first: before ? undefined : ITEMS_PER_PAGE,
after: after || undefined, after: after || undefined,
last: before ? ITEMS_PER_PAGE : undefined,
before: before || undefined, before: before || undefined,
}); });
}, [loadQuery, searchParams]); }, [loadQuery, searchParams]);
const handlePageChange = ({ before, after }: { before?: string; after?: string }) => { const handlePageChange = ({ first, after, last, before }: { first?: number; after?: string; last?: number; before?: string }) => {
loadQuery( loadQuery(
{ {
count: ITEMS_PER_PAGE, first,
before: before || undefined, after,
after: after || undefined, last,
before,
}, },
{ fetchPolicy: 'network-only' } { fetchPolicy: 'network-only' }
); );

View File

@@ -1,5 +1,5 @@
/** /**
* @generated SignedSource<<bfb82e1523f2352b45b1cf3135a05b19>> * @generated SignedSource<<3b4f62f431760660274bbff4ae888d0a>>
* @lightSyntaxTransform * @lightSyntaxTransform
* @nogrep * @nogrep
*/ */
@@ -12,7 +12,8 @@ import { ConcreteRequest } from 'relay-runtime';
export type VendorListPageQuery$variables = { export type VendorListPageQuery$variables = {
after?: any | null | undefined; after?: any | null | undefined;
before?: any | null | undefined; before?: any | null | undefined;
count: number; first?: number | null | undefined;
last?: number | null | undefined;
}; };
export type VendorListPageQuery$data = { export type VendorListPageQuery$data = {
readonly node: { readonly node: {
@@ -55,23 +56,28 @@ v1 = {
v2 = { v2 = {
"defaultValue": null, "defaultValue": null,
"kind": "LocalArgument", "kind": "LocalArgument",
"name": "count" "name": "first"
}, },
v3 = [ v3 = {
"defaultValue": null,
"kind": "LocalArgument",
"name": "last"
},
v4 = [
{ {
"kind": "Literal", "kind": "Literal",
"name": "id", "name": "id",
"value": "AZSfP_xAcAC5IAAAAAAltA" "value": "AZSfP_xAcAC5IAAAAAAltA"
} }
], ],
v4 = { v5 = {
"alias": null, "alias": null,
"args": null, "args": null,
"kind": "ScalarField", "kind": "ScalarField",
"name": "id", "name": "id",
"storageKey": null "storageKey": null
}, },
v5 = { v6 = {
"kind": "InlineFragment", "kind": "InlineFragment",
"selections": [ "selections": [
{ {
@@ -90,7 +96,12 @@ v5 = {
{ {
"kind": "Variable", "kind": "Variable",
"name": "first", "name": "first",
"variableName": "count" "variableName": "first"
},
{
"kind": "Variable",
"name": "last",
"variableName": "last"
} }
], ],
"concreteType": "VendorConnection", "concreteType": "VendorConnection",
@@ -114,7 +125,7 @@ v5 = {
"name": "node", "name": "node",
"plural": false, "plural": false,
"selections": [ "selections": [
(v4/*: any*/), (v5/*: any*/),
{ {
"alias": null, "alias": null,
"args": null, "args": null,
@@ -200,7 +211,8 @@ return {
"argumentDefinitions": [ "argumentDefinitions": [
(v0/*: any*/), (v0/*: any*/),
(v1/*: any*/), (v1/*: any*/),
(v2/*: any*/) (v2/*: any*/),
(v3/*: any*/)
], ],
"kind": "Fragment", "kind": "Fragment",
"metadata": null, "metadata": null,
@@ -208,14 +220,14 @@ return {
"selections": [ "selections": [
{ {
"alias": null, "alias": null,
"args": (v3/*: any*/), "args": (v4/*: any*/),
"concreteType": null, "concreteType": null,
"kind": "LinkedField", "kind": "LinkedField",
"name": "node", "name": "node",
"plural": false, "plural": false,
"selections": [ "selections": [
(v4/*: any*/), (v5/*: any*/),
(v5/*: any*/) (v6/*: any*/)
], ],
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")" "storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
} }
@@ -228,6 +240,7 @@ return {
"argumentDefinitions": [ "argumentDefinitions": [
(v2/*: any*/), (v2/*: any*/),
(v0/*: any*/), (v0/*: any*/),
(v3/*: any*/),
(v1/*: any*/) (v1/*: any*/)
], ],
"kind": "Operation", "kind": "Operation",
@@ -235,7 +248,7 @@ return {
"selections": [ "selections": [
{ {
"alias": null, "alias": null,
"args": (v3/*: any*/), "args": (v4/*: any*/),
"concreteType": null, "concreteType": null,
"kind": "LinkedField", "kind": "LinkedField",
"name": "node", "name": "node",
@@ -248,24 +261,24 @@ return {
"name": "__typename", "name": "__typename",
"storageKey": null "storageKey": null
}, },
(v4/*: any*/), (v5/*: any*/),
(v5/*: any*/) (v6/*: any*/)
], ],
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")" "storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
} }
] ]
}, },
"params": { "params": {
"cacheID": "813b95f0cae45fec89ee444ce9be7ebb", "cacheID": "d38a5cbe98c811f237ea5722871aabf6",
"id": null, "id": null,
"metadata": {}, "metadata": {},
"name": "VendorListPageQuery", "name": "VendorListPageQuery",
"operationKind": "query", "operationKind": "query",
"text": "query VendorListPageQuery(\n $count: Int!\n $after: CursorKey\n $before: CursorKey\n) {\n node(id: \"AZSfP_xAcAC5IAAAAAAltA\") {\n __typename\n id\n ... on Organization {\n vendors(first: $count, after: $after, before: $before) {\n edges {\n node {\n id\n name\n createdAt\n updatedAt\n }\n cursor\n }\n pageInfo {\n hasNextPage\n hasPreviousPage\n startCursor\n endCursor\n }\n }\n }\n }\n}\n" "text": "query VendorListPageQuery(\n $first: Int\n $after: CursorKey\n $last: Int\n $before: CursorKey\n) {\n node(id: \"AZSfP_xAcAC5IAAAAAAltA\") {\n __typename\n id\n ... on Organization {\n vendors(first: $first, after: $after, last: $last, before: $before) {\n edges {\n node {\n id\n name\n createdAt\n updatedAt\n }\n cursor\n }\n pageInfo {\n hasNextPage\n hasPreviousPage\n startCursor\n endCursor\n }\n }\n }\n }\n}\n"
} }
}; };
})(); })();
(node as any).hash = "46f94dbcc31c870cab30e1b8a2c64095"; (node as any).hash = "e371fe8685ccc70b229f0d5b07d6e271";
export default node; export default node;

View File

@@ -20,7 +20,7 @@ import (
"errors" "errors"
"time" "time"
"go.gearno.de/crypto/uuid" "github.com/getprobo/probo/pkg/gid"
) )
type ( type (
@@ -63,7 +63,7 @@ func CursorKeyFromBytes(b []byte) (CursorKey, error) {
return ck, nil return ck, nil
} }
func NewCursorKey(id uuid.UUID, t time.Time) CursorKey { func NewCursorKey(id gid.GID, t time.Time) CursorKey {
var cursorKey CursorKey var cursorKey CursorKey
copy(cursorKey[:16], id[:]) copy(cursorKey[:16], id[:])
_ = binary.PutVarint(cursorKey[16:], t.UnixMicro()) _ = binary.PutVarint(cursorKey[16:], t.UnixMicro())
@@ -88,9 +88,8 @@ func (ck CursorKey) Timestamp() time.Time {
return time.Unix(seconds, nanoseconds) return time.Unix(seconds, nanoseconds)
} }
func (ck CursorKey) ID() uuid.UUID { func (ck CursorKey) ID() gid.GID {
id, _ := uuid.FromBytes(ck[:]) return gid.GID(ck[:16])
return id
} }
func (ck CursorKey) MarshalText() ([]byte, error) { func (ck CursorKey) MarshalText() ([]byte, error) {

View File

@@ -24,7 +24,6 @@ import (
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -44,7 +43,7 @@ type (
) )
func (c Control) CursorKey() page.CursorKey { func (c Control) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(c.ID), c.CreatedAt) return page.NewCursorKey(c.ID, c.CreatedAt)
} }
func (c *Control) scan(r pgx.Row) error { func (c *Control) scan(r pgx.Row) error {
@@ -146,13 +145,13 @@ VALUES (
` `
args := pgx.NamedArgs{ args := pgx.NamedArgs{
"control_id": c.ID, "control_id": c.ID,
"framework_id": c.FrameworkID, "framework_id": c.FrameworkID,
"name": c.Name, "name": c.Name,
"description": c.Description, "description": c.Description,
"content_ref": c.ContentRef, "content_ref": c.ContentRef,
"created_at": c.CreatedAt, "created_at": c.CreatedAt,
"updated_at": c.UpdatedAt, "updated_at": c.UpdatedAt,
} }
_, err := conn.Exec(ctx, q, args) _, err := conn.Exec(ctx, q, args)
return err return err

View File

@@ -22,7 +22,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -37,7 +36,7 @@ type (
) )
func (cst ControlStateTransition) CursorKey() page.CursorKey { func (cst ControlStateTransition) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(cst.ID), cst.CreatedAt) return page.NewCursorKey(cst.ID, cst.CreatedAt)
} }
func (cst *ControlStateTransition) scan(r pgx.Row) error { func (cst *ControlStateTransition) scan(r pgx.Row) error {

View File

@@ -23,7 +23,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -43,7 +42,7 @@ type (
) )
func (e Evidence) CursorKey() page.CursorKey { func (e Evidence) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(e.ID), e.CreatedAt) return page.NewCursorKey(e.ID, e.CreatedAt)
} }
func (e *Evidence) scan(r pgx.Row) error { func (e *Evidence) scan(r pgx.Row) error {

View File

@@ -22,7 +22,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -37,7 +36,7 @@ type (
) )
func (cst EvidenceStateTransition) CursorKey() page.CursorKey { func (cst EvidenceStateTransition) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(cst.ID), cst.CreatedAt) return page.NewCursorKey(cst.ID, cst.CreatedAt)
} }
func (cst *EvidenceStateTransition) scan(r pgx.Row) error { func (cst *EvidenceStateTransition) scan(r pgx.Row) error {

View File

@@ -23,7 +23,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -42,7 +41,7 @@ type (
) )
func (f Framework) CursorKey() page.CursorKey { func (f Framework) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(f.ID), f.CreatedAt) return page.NewCursorKey(f.ID, f.CreatedAt)
} }
func (f *Framework) scan(r pgx.Row) error { func (f *Framework) scan(r pgx.Row) error {
@@ -177,13 +176,13 @@ VALUES (
` `
args := pgx.NamedArgs{ args := pgx.NamedArgs{
"framework_id": f.ID, "framework_id": f.ID,
"organization_id": f.OrganizationID, "organization_id": f.OrganizationID,
"name": f.Name, "name": f.Name,
"description": f.Description, "description": f.Description,
"content_ref": f.ContentRef, "content_ref": f.ContentRef,
"created_at": f.CreatedAt, "created_at": f.CreatedAt,
"updated_at": f.UpdatedAt, "updated_at": f.UpdatedAt,
} }
_, err := conn.Exec(ctx, q, args) _, err := conn.Exec(ctx, q, args)
return err return err

View File

@@ -23,7 +23,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -43,7 +42,7 @@ type (
) )
func (p People) CursorKey() page.CursorKey { func (p People) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(p.ID), p.CreatedAt) return page.NewCursorKey(p.ID, p.CreatedAt)
} }
func (p *People) scan(r pgx.Row) error { func (p *People) scan(r pgx.Row) error {

View File

@@ -24,7 +24,6 @@ import (
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -44,7 +43,7 @@ type (
) )
func (t Task) CursorKey() page.CursorKey { func (t Task) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(t.ID), t.CreatedAt) return page.NewCursorKey(t.ID, t.CreatedAt)
} }
func (t *Task) scan(r pgx.Row) error { func (t *Task) scan(r pgx.Row) error {

View File

@@ -22,7 +22,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -37,7 +36,7 @@ type (
) )
func (tst TaskStateTransition) CursorKey() page.CursorKey { func (tst TaskStateTransition) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(tst.ID), tst.CreatedAt) return page.NewCursorKey(tst.ID, tst.CreatedAt)
} }
func (tst *TaskStateTransition) scan(r pgx.Row) error { func (tst *TaskStateTransition) scan(r pgx.Row) error {

View File

@@ -23,7 +23,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -40,7 +39,7 @@ type (
) )
func (v Vendor) CursorKey() page.CursorKey { func (v Vendor) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(v.ID), v.CreatedAt) return page.NewCursorKey(v.ID, v.CreatedAt)
} }
func (v *Vendor) scan(r pgx.Row) error { func (v *Vendor) scan(r pgx.Row) error {

View File

@@ -22,7 +22,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -37,7 +36,7 @@ type (
) )
func (s Session) CursorKey() page.CursorKey { func (s Session) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(s.ID), s.CreatedAt) return page.NewCursorKey(s.ID, s.CreatedAt)
} }
func (s *Session) scan(r pgx.Row) error { func (s *Session) scan(r pgx.Row) error {

View File

@@ -21,7 +21,6 @@ import (
"github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page" "github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
) )
@@ -36,7 +35,7 @@ type (
) )
func (u User) CursorKey() page.CursorKey { func (u User) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(u.ID), u.CreatedAt) return page.NewCursorKey(u.ID, u.CreatedAt)
} }
func (u *User) scan(r pgx.Row) error { func (u *User) scan(r pgx.Row) error {