Add pagination for vendor list
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -1,13 +1,12 @@
|
|||||||
import { Suspense, useEffect, useState } from "react";
|
import { Suspense, useEffect, useState, useTransition } from "react";
|
||||||
import {
|
import {
|
||||||
graphql,
|
graphql,
|
||||||
PreloadedQuery,
|
PreloadedQuery,
|
||||||
usePreloadedQuery,
|
usePreloadedQuery,
|
||||||
useQueryLoader,
|
useQueryLoader,
|
||||||
useMutation,
|
useMutation,
|
||||||
loadQuery,
|
|
||||||
useLazyLoadQuery,
|
|
||||||
} from "react-relay";
|
} from "react-relay";
|
||||||
|
import { useSearchParams } from "react-router";
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { CircleUser, Globe, Shield, Store } from "lucide-react";
|
import { CircleUser, Globe, Shield, Store } from "lucide-react";
|
||||||
@@ -17,12 +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";
|
||||||
|
|
||||||
|
const ITEMS_PER_PAGE = 2;
|
||||||
|
|
||||||
const vendorListPageQuery = graphql`
|
const vendorListPageQuery = graphql`
|
||||||
query VendorListPageQuery {
|
query VendorListPageQuery($count: Int!, $after: CursorKey, $before: CursorKey) {
|
||||||
node(id: "AZSfP_xAcAC5IAAAAAAltA") {
|
node(id: "AZSfP_xAcAC5IAAAAAAltA") {
|
||||||
id
|
id
|
||||||
... on Organization {
|
... on Organization {
|
||||||
vendors {
|
vendors(first: $count, after: $after, before: $before) {
|
||||||
edges {
|
edges {
|
||||||
node {
|
node {
|
||||||
id
|
id
|
||||||
@@ -30,6 +31,13 @@ const vendorListPageQuery = graphql`
|
|||||||
createdAt
|
createdAt
|
||||||
updatedAt
|
updatedAt
|
||||||
}
|
}
|
||||||
|
cursor
|
||||||
|
}
|
||||||
|
pageInfo {
|
||||||
|
hasNextPage
|
||||||
|
hasPreviousPage
|
||||||
|
startCursor
|
||||||
|
endCursor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,20 +67,51 @@ const vendorsList = [
|
|||||||
|
|
||||||
function VendorListContent({
|
function VendorListContent({
|
||||||
queryRef,
|
queryRef,
|
||||||
|
onPageChange,
|
||||||
}: {
|
}: {
|
||||||
queryRef: PreloadedQuery<VendorListPageQueryType>;
|
queryRef: PreloadedQuery<VendorListPageQueryType>;
|
||||||
|
onPageChange: (params: { before?: string; after?: string }) => void;
|
||||||
}) {
|
}) {
|
||||||
const data = usePreloadedQuery(vendorListPageQuery, queryRef);
|
const data = usePreloadedQuery(vendorListPageQuery, queryRef);
|
||||||
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
const [searchTerm, setSearchTerm] = useState("");
|
const [searchTerm, setSearchTerm] = useState("");
|
||||||
const [filteredVendors, setFilteredVendors] = useState<Array<typeof vendorsList[0]>>([]);
|
const [filteredVendors, setFilteredVendors] = useState<Array<any>>([]);
|
||||||
const [createVendor] = useMutation(createVendorMutation);
|
const [createVendor] = useMutation(createVendorMutation);
|
||||||
const [_, loadQuery] = useQueryLoader<VendorListPageQueryType>(vendorListPageQuery);
|
|
||||||
const fuse = new Fuse<typeof vendorsList[0]>(vendorsList, {
|
const vendors = data.node?.vendors?.edges?.map(edge => edge?.node) ?? [];
|
||||||
|
const pageInfo = data.node?.vendors?.pageInfo;
|
||||||
|
|
||||||
|
const fuse = new Fuse(vendors, {
|
||||||
keys: ['name'],
|
keys: ['name'],
|
||||||
threshold: 0.3,
|
threshold: 0.3,
|
||||||
});
|
});
|
||||||
|
|
||||||
const vendors = data.node.vendors?.edges.map(edge => edge.node) ?? [];
|
const handlePageChange = (direction: 'next' | 'prev') => {
|
||||||
|
if (!pageInfo) return;
|
||||||
|
|
||||||
|
if (direction === 'next' && !pageInfo.hasNextPage) return;
|
||||||
|
if (direction === 'prev' && !pageInfo.hasPreviousPage) return;
|
||||||
|
|
||||||
|
startTransition(() => {
|
||||||
|
const params = direction === 'next'
|
||||||
|
? { after: pageInfo.endCursor }
|
||||||
|
: { before: pageInfo.startCursor };
|
||||||
|
|
||||||
|
setSearchParams(prev => {
|
||||||
|
if (direction === 'next') {
|
||||||
|
prev.set('after', pageInfo.endCursor!);
|
||||||
|
prev.delete('before');
|
||||||
|
} else {
|
||||||
|
prev.set('before', pageInfo.startCursor!);
|
||||||
|
prev.delete('after');
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
});
|
||||||
|
|
||||||
|
onPageChange(params);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="p-6 space-y-6">
|
<div className="p-6 space-y-6">
|
||||||
@@ -129,7 +168,6 @@ function VendorListContent({
|
|||||||
onCompleted(response: any) {
|
onCompleted(response: any) {
|
||||||
setSearchTerm("");
|
setSearchTerm("");
|
||||||
setFilteredVendors([]);
|
setFilteredVendors([]);
|
||||||
loadQuery({}, {fetchPolicy: 'network-only'});
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
@@ -182,6 +220,23 @@ function VendorListContent({
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
<div className="flex gap-2 justify-end mt-4">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => handlePageChange('prev')}
|
||||||
|
disabled={isPending || !pageInfo?.hasPreviousPage}
|
||||||
|
>
|
||||||
|
{isPending ? "Loading..." : "Previous"}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => handlePageChange('next')}
|
||||||
|
disabled={isPending || !pageInfo?.hasNextPage}
|
||||||
|
>
|
||||||
|
{isPending ? "Loading..." : "Next"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -204,11 +259,31 @@ function VendorListFallback() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function VendorListPage() {
|
export default function VendorListPage() {
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
const [queryRef, loadQuery] = useQueryLoader<VendorListPageQueryType>(vendorListPageQuery);
|
const [queryRef, loadQuery] = useQueryLoader<VendorListPageQueryType>(vendorListPageQuery);
|
||||||
|
|
||||||
|
// Initialize with URL params
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadQuery({});
|
const after = searchParams.get('after');
|
||||||
}, [loadQuery]);
|
const before = searchParams.get('before');
|
||||||
|
|
||||||
|
loadQuery({
|
||||||
|
count: ITEMS_PER_PAGE,
|
||||||
|
after: after || undefined,
|
||||||
|
before: before || undefined,
|
||||||
|
});
|
||||||
|
}, [loadQuery, searchParams]);
|
||||||
|
|
||||||
|
const handlePageChange = ({ before, after }: { before?: string; after?: string }) => {
|
||||||
|
loadQuery(
|
||||||
|
{
|
||||||
|
count: ITEMS_PER_PAGE,
|
||||||
|
before: before || undefined,
|
||||||
|
after: after || undefined,
|
||||||
|
},
|
||||||
|
{ fetchPolicy: 'network-only' }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
if (!queryRef) {
|
if (!queryRef) {
|
||||||
return <VendorListFallback />;
|
return <VendorListFallback />;
|
||||||
@@ -216,7 +291,7 @@ export default function VendorListPage() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Suspense fallback={<VendorListFallback />}>
|
<Suspense fallback={<VendorListFallback />}>
|
||||||
<VendorListContent queryRef={queryRef} />
|
<VendorListContent queryRef={queryRef} onPageChange={handlePageChange} />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* @generated SignedSource<<a55cac2889d9749005056d4fe1d88935>>
|
* @generated SignedSource<<bfb82e1523f2352b45b1cf3135a05b19>>
|
||||||
* @lightSyntaxTransform
|
* @lightSyntaxTransform
|
||||||
* @nogrep
|
* @nogrep
|
||||||
*/
|
*/
|
||||||
@@ -9,12 +9,17 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
|
|
||||||
import { ConcreteRequest } from 'relay-runtime';
|
import { ConcreteRequest } from 'relay-runtime';
|
||||||
export type VendorListPageQuery$variables = Record<PropertyKey, never>;
|
export type VendorListPageQuery$variables = {
|
||||||
|
after?: any | null | undefined;
|
||||||
|
before?: any | null | undefined;
|
||||||
|
count: number;
|
||||||
|
};
|
||||||
export type VendorListPageQuery$data = {
|
export type VendorListPageQuery$data = {
|
||||||
readonly node: {
|
readonly node: {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
readonly vendors?: {
|
readonly vendors?: {
|
||||||
readonly edges: ReadonlyArray<{
|
readonly edges: ReadonlyArray<{
|
||||||
|
readonly cursor: any;
|
||||||
readonly node: {
|
readonly node: {
|
||||||
readonly createdAt: any;
|
readonly createdAt: any;
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
@@ -22,6 +27,12 @@ export type VendorListPageQuery$data = {
|
|||||||
readonly updatedAt: any;
|
readonly updatedAt: any;
|
||||||
};
|
};
|
||||||
}>;
|
}>;
|
||||||
|
readonly pageInfo: {
|
||||||
|
readonly endCursor: any | null | undefined;
|
||||||
|
readonly hasNextPage: boolean;
|
||||||
|
readonly hasPreviousPage: boolean;
|
||||||
|
readonly startCursor: any | null | undefined;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -31,26 +42,57 @@ export type VendorListPageQuery = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const node: ConcreteRequest = (function(){
|
const node: ConcreteRequest = (function(){
|
||||||
var v0 = [
|
var v0 = {
|
||||||
|
"defaultValue": null,
|
||||||
|
"kind": "LocalArgument",
|
||||||
|
"name": "after"
|
||||||
|
},
|
||||||
|
v1 = {
|
||||||
|
"defaultValue": null,
|
||||||
|
"kind": "LocalArgument",
|
||||||
|
"name": "before"
|
||||||
|
},
|
||||||
|
v2 = {
|
||||||
|
"defaultValue": null,
|
||||||
|
"kind": "LocalArgument",
|
||||||
|
"name": "count"
|
||||||
|
},
|
||||||
|
v3 = [
|
||||||
{
|
{
|
||||||
"kind": "Literal",
|
"kind": "Literal",
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"value": "AZSfP_xAcAC5IAAAAAAltA"
|
"value": "AZSfP_xAcAC5IAAAAAAltA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
v1 = {
|
v4 = {
|
||||||
"alias": null,
|
"alias": null,
|
||||||
"args": null,
|
"args": null,
|
||||||
"kind": "ScalarField",
|
"kind": "ScalarField",
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"storageKey": null
|
"storageKey": null
|
||||||
},
|
},
|
||||||
v2 = {
|
v5 = {
|
||||||
"kind": "InlineFragment",
|
"kind": "InlineFragment",
|
||||||
"selections": [
|
"selections": [
|
||||||
{
|
{
|
||||||
"alias": null,
|
"alias": null,
|
||||||
"args": null,
|
"args": [
|
||||||
|
{
|
||||||
|
"kind": "Variable",
|
||||||
|
"name": "after",
|
||||||
|
"variableName": "after"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "Variable",
|
||||||
|
"name": "before",
|
||||||
|
"variableName": "before"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "Variable",
|
||||||
|
"name": "first",
|
||||||
|
"variableName": "count"
|
||||||
|
}
|
||||||
|
],
|
||||||
"concreteType": "VendorConnection",
|
"concreteType": "VendorConnection",
|
||||||
"kind": "LinkedField",
|
"kind": "LinkedField",
|
||||||
"name": "vendors",
|
"name": "vendors",
|
||||||
@@ -72,7 +114,7 @@ v2 = {
|
|||||||
"name": "node",
|
"name": "node",
|
||||||
"plural": false,
|
"plural": false,
|
||||||
"selections": [
|
"selections": [
|
||||||
(v1/*: any*/),
|
(v4/*: any*/),
|
||||||
{
|
{
|
||||||
"alias": null,
|
"alias": null,
|
||||||
"args": null,
|
"args": null,
|
||||||
@@ -96,6 +138,52 @@ v2 = {
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"storageKey": null
|
"storageKey": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"kind": "ScalarField",
|
||||||
|
"name": "cursor",
|
||||||
|
"storageKey": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"storageKey": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"concreteType": "PageInfo",
|
||||||
|
"kind": "LinkedField",
|
||||||
|
"name": "pageInfo",
|
||||||
|
"plural": false,
|
||||||
|
"selections": [
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"kind": "ScalarField",
|
||||||
|
"name": "hasNextPage",
|
||||||
|
"storageKey": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"kind": "ScalarField",
|
||||||
|
"name": "hasPreviousPage",
|
||||||
|
"storageKey": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"kind": "ScalarField",
|
||||||
|
"name": "startCursor",
|
||||||
|
"storageKey": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": null,
|
||||||
|
"args": null,
|
||||||
|
"kind": "ScalarField",
|
||||||
|
"name": "endCursor",
|
||||||
|
"storageKey": null
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"storageKey": null
|
"storageKey": null
|
||||||
@@ -109,21 +197,25 @@ v2 = {
|
|||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
"fragment": {
|
"fragment": {
|
||||||
"argumentDefinitions": [],
|
"argumentDefinitions": [
|
||||||
|
(v0/*: any*/),
|
||||||
|
(v1/*: any*/),
|
||||||
|
(v2/*: any*/)
|
||||||
|
],
|
||||||
"kind": "Fragment",
|
"kind": "Fragment",
|
||||||
"metadata": null,
|
"metadata": null,
|
||||||
"name": "VendorListPageQuery",
|
"name": "VendorListPageQuery",
|
||||||
"selections": [
|
"selections": [
|
||||||
{
|
{
|
||||||
"alias": null,
|
"alias": null,
|
||||||
"args": (v0/*: any*/),
|
"args": (v3/*: any*/),
|
||||||
"concreteType": null,
|
"concreteType": null,
|
||||||
"kind": "LinkedField",
|
"kind": "LinkedField",
|
||||||
"name": "node",
|
"name": "node",
|
||||||
"plural": false,
|
"plural": false,
|
||||||
"selections": [
|
"selections": [
|
||||||
(v1/*: any*/),
|
(v4/*: any*/),
|
||||||
(v2/*: any*/)
|
(v5/*: any*/)
|
||||||
],
|
],
|
||||||
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
|
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
|
||||||
}
|
}
|
||||||
@@ -133,13 +225,17 @@ return {
|
|||||||
},
|
},
|
||||||
"kind": "Request",
|
"kind": "Request",
|
||||||
"operation": {
|
"operation": {
|
||||||
"argumentDefinitions": [],
|
"argumentDefinitions": [
|
||||||
|
(v2/*: any*/),
|
||||||
|
(v0/*: any*/),
|
||||||
|
(v1/*: any*/)
|
||||||
|
],
|
||||||
"kind": "Operation",
|
"kind": "Operation",
|
||||||
"name": "VendorListPageQuery",
|
"name": "VendorListPageQuery",
|
||||||
"selections": [
|
"selections": [
|
||||||
{
|
{
|
||||||
"alias": null,
|
"alias": null,
|
||||||
"args": (v0/*: any*/),
|
"args": (v3/*: any*/),
|
||||||
"concreteType": null,
|
"concreteType": null,
|
||||||
"kind": "LinkedField",
|
"kind": "LinkedField",
|
||||||
"name": "node",
|
"name": "node",
|
||||||
@@ -152,24 +248,24 @@ return {
|
|||||||
"name": "__typename",
|
"name": "__typename",
|
||||||
"storageKey": null
|
"storageKey": null
|
||||||
},
|
},
|
||||||
(v1/*: any*/),
|
(v4/*: any*/),
|
||||||
(v2/*: any*/)
|
(v5/*: any*/)
|
||||||
],
|
],
|
||||||
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
|
"storageKey": "node(id:\"AZSfP_xAcAC5IAAAAAAltA\")"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"params": {
|
"params": {
|
||||||
"cacheID": "1661d7e75c833a569efafa55c204d894",
|
"cacheID": "813b95f0cae45fec89ee444ce9be7ebb",
|
||||||
"id": null,
|
"id": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"name": "VendorListPageQuery",
|
"name": "VendorListPageQuery",
|
||||||
"operationKind": "query",
|
"operationKind": "query",
|
||||||
"text": "query VendorListPageQuery {\n node(id: \"AZSfP_xAcAC5IAAAAAAltA\") {\n __typename\n id\n ... on Organization {\n vendors {\n edges {\n node {\n id\n name\n createdAt\n updatedAt\n }\n }\n }\n }\n }\n}\n"
|
"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"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
(node as any).hash = "e869cdf6203c1a7f42c4c697f574b0c0";
|
(node as any).hash = "46f94dbcc31c870cab30e1b8a2c64095";
|
||||||
|
|
||||||
export default node;
|
export default node;
|
||||||
|
|||||||
Reference in New Issue
Block a user