Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6175a7f
feat: add data table hook
G3root Aug 28, 2024
9267b5a
feat: add new data table hook
G3root Aug 28, 2024
c8ae1ac
chore: rename hook
G3root Aug 28, 2024
a0106f4
feat: add paginated hook
G3root Aug 28, 2024
99ff880
feat: add offset schema
G3root Aug 28, 2024
07f2d55
feat: use offset schema
G3root Aug 28, 2024
5c8a3d3
fix: session
G3root Aug 28, 2024
1238f71
feat: add const
G3root Aug 28, 2024
612238a
chore: fix error handling
G3root Aug 28, 2024
733b70d
chore: make header optional
G3root Aug 28, 2024
36c9a72
feat: add api client
G3root Aug 28, 2024
bcede0d
chore: add nuqs
G3root Aug 28, 2024
399b222
feat: use pagination
G3root Aug 28, 2024
b207ff0
refactor: hook
G3root Aug 29, 2024
fd34967
feat: add sort
G3root Aug 29, 2024
e7e7449
feat: add sort
G3root Aug 29, 2024
85116f3
chore: hide select
G3root Aug 29, 2024
4ddcf2a
feat: add search filter
G3root Aug 29, 2024
9851bf3
feat: refactor data
G3root Aug 29, 2024
69a423e
feat: add debounce component
G3root Aug 29, 2024
2409c0d
feat: add debounce input
G3root Aug 29, 2024
3523911
feat: add search
G3root Aug 30, 2024
8810540
fix: query
G3root Aug 30, 2024
5acdd81
feat: update tables
G3root Aug 30, 2024
6c5d5f1
feat: add seed
G3root Aug 30, 2024
2fe228f
Merge branch 'main' into pagination-table
G3root Aug 30, 2024
92d651d
chore: add tax id
G3root Sep 2, 2024
e733531
chore: upgrade trpc
G3root Sep 2, 2024
fcf2db6
feat: use new trpc
G3root Sep 2, 2024
9ca66a6
feat: add error boundary
G3root Sep 2, 2024
233b004
chore: rename loading
G3root Sep 2, 2024
33ba09b
feat: use useSuspense
G3root Sep 2, 2024
b91f92d
fix: data
G3root Sep 2, 2024
c477f15
feat: add search param hook
G3root Sep 17, 2024
160cf3d
feat: add search
G3root Sep 17, 2024
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
Prev Previous commit
Next Next commit
feat: use pagination
  • Loading branch information
G3root committed Aug 28, 2024
commit 399b2220180d77beb7f72e2ed40834b71012f7d9
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import StakeholderTable from "@/components/stakeholder/stakeholder-table";
import { Card } from "@/components/ui/card";
import { UnAuthorizedState } from "@/components/ui/un-authorized-state";
import { serverAccessControl } from "@/lib/rbac/access-control";
import { withServerSession } from "@/server/auth";
import { api } from "@/trpc/server";
import { RiGroup2Fill } from "@remixicon/react";
import type { Metadata } from "next";
Expand All @@ -13,6 +14,8 @@ export const metadata: Metadata = {
};

const StakeholdersPage = async () => {
const session = await withServerSession();

const { allow } = await serverAccessControl();
const stakeholders = await allow(api.stakeholder.getStakeholders.query(), [
"stakeholder",
Expand Down Expand Up @@ -55,7 +58,10 @@ const StakeholdersPage = async () => {
</div>

<Card className="mx-auto mt-3 w-[28rem] sm:w-[38rem] md:w-full">
<StakeholderTable stakeholders={stakeholders} />
<StakeholderTable
companyId={session.user.companyId}
stakeholders={stakeholders}
/>
</Card>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import {
StakeholderRelationshipEnum,
StakeholderTypeEnum,
} from "@/prisma/enums";
import type { TGetManyStakeholderRes } from "@/server/api/client-handlers/stakeholder";
import { api } from "@/trpc/react";
import type { RouterOutputs } from "@/trpc/shared";
import { useRouter } from "next/navigation";
import { toast } from "sonner";

export type TStakeholder =
RouterOutputs["stakeholder"]["getStakeholders"][number];
export type TStakeholder = TGetManyStakeholderRes["data"][number];

type TSingleStakeholderForm =
| {
Expand Down
48 changes: 42 additions & 6 deletions src/components/stakeholder/stakeholder-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { SortButton } from "@/components/ui/data-table/data-table-buttons";
import { DataTableContent } from "@/components/ui/data-table/data-table-content";
import { DataTableHeader } from "@/components/ui/data-table/data-table-header";
import { DataTablePagination } from "@/components/ui/data-table/data-table-pagination";
import { useDataTable } from "@/hooks/use-data-table";
import {
usePaginatedQueryParams,
usePaginatedTable,
} from "@/hooks/use-paginated-data-table";
import type { TGetManyStakeholderRes } from "@/server/api/client-handlers/stakeholder";
import { useManyStakeholder } from "@/server/api/client-hooks/stakeholder";
import type { RouterOutputs } from "@/trpc/shared";
import { RiMore2Fill } from "@remixicon/react";
import type { ColumnDef } from "@tanstack/react-table";
import React from "react";
import type { ColumnDef, PaginationState } from "@tanstack/react-table";
import { use, useState } from "react";
import { Allow } from "../rbac/allow";
import { Button } from "../ui/button";
import {
Expand All @@ -30,6 +35,7 @@ type Stakeholder = RouterOutputs["stakeholder"]["getStakeholders"];

type StakeholderTableType = {
stakeholders: Stakeholder;
companyId: string;
};

const getStakeholderType = (type: string) => {
Expand Down Expand Up @@ -72,7 +78,7 @@ const getCurrentRelationship = (relationship: string) => {
}
};

export const columns: ColumnDef<Stakeholder[number]>[] = [
export const columns: ColumnDef<TGetManyStakeholderRes["data"][number]>[] = [
{
id: "select",
header: ({ table }) => (
Expand Down Expand Up @@ -206,8 +212,38 @@ export const columns: ColumnDef<Stakeholder[number]>[] = [
},
];

const StakeholderTable = ({ stakeholders }: StakeholderTableType) => {
const table = useDataTable({ columns, data: stakeholders });
const StakeholderTable = ({ companyId }: StakeholderTableType) => {
const [{ limit, page }, setData] = usePaginatedQueryParams();

const pageIndex = page - 1;
const pageSize = limit;

const { data } = useManyStakeholder({
searchParams: { limit, page },
urlParams: { companyId },
});

const table = usePaginatedTable({
pageCount: data?.meta.pageCount ?? -1,
columns,
data: data?.data ?? [],
state: {
pagination: {
pageIndex,
pageSize,
},
},
onPaginationChange: (updater) => {
if (typeof updater === "function") {
const updateValue = updater({ pageIndex, pageSize });

setData({
limit: updateValue.pageSize,
page: updateValue.pageIndex + 1,
});
}
},
});
return (
<div className="w-full p-6">
<DataTable table={table}>
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/use-paginated-data-table.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { RowData, TableState } from "@tanstack/react-table";
import { parseAsInteger, useQueryStates } from "nuqs";
import { type TDataTableOptions, useDataTable } from "./use-data-table";

type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

type TState = MakeRequired<Partial<TableState>, "pagination">;

export function usePaginatedQueryParams() {
return useQueryStates({
page: parseAsInteger.withDefault(1),
limit: parseAsInteger.withDefault(2),
});
}

export function usePaginatedTable<TData extends RowData>(
options: Omit<TDataTableOptions<TData>, "state"> & { state: TState },
) {
Expand Down
18 changes: 18 additions & 0 deletions src/server/api/client-hooks/stakeholder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use client";

import { useQuery } from "@tanstack/react-query";
import {
type TGetManyStakeholderParams,
getManyStakeholder,
} from "../client-handlers/stakeholder";

export const useManyStakeholder = (data: TGetManyStakeholderParams) =>
useQuery({
queryKey: [
"all-stakeholder",
data.urlParams.companyId,
String(data.searchParams.limit),
String(data.searchParams.page),
],
queryFn: () => getManyStakeholder(data),
});