feat: add server-side table sorting
This commit is contained in:
@@ -82,6 +82,7 @@ export function useTenantColumns({
|
||||
columns.push({
|
||||
id: 'actions',
|
||||
header: () => <div className="text-right">Actions</div>,
|
||||
enableSorting: false,
|
||||
cell: ({ row }) => {
|
||||
const tenant = row.original;
|
||||
return (
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import type { ReactNode } from 'react';
|
||||
import type { ColumnDef } from '@tanstack/react-table';
|
||||
import type { ColumnDef, SortingState } from '@tanstack/react-table';
|
||||
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import type { Tenant } from '@/types';
|
||||
@@ -14,8 +14,10 @@ interface TenantTableProps {
|
||||
skip: number;
|
||||
limit: number;
|
||||
total: number;
|
||||
sorting: SortingState;
|
||||
onPageChange: (skip: number) => void;
|
||||
onLimitChange: (limit: number) => void;
|
||||
onSortingChange: (sorting: SortingState) => void;
|
||||
}
|
||||
|
||||
export function TenantTable({
|
||||
@@ -26,8 +28,10 @@ export function TenantTable({
|
||||
skip,
|
||||
limit,
|
||||
total,
|
||||
sorting,
|
||||
onPageChange,
|
||||
onLimitChange,
|
||||
onSortingChange,
|
||||
}: TenantTableProps) {
|
||||
return (
|
||||
<DataTable
|
||||
@@ -44,6 +48,8 @@ export function TenantTable({
|
||||
onPageChange,
|
||||
onLimitChange,
|
||||
}}
|
||||
sorting={sorting}
|
||||
onSortingChange={onSortingChange}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useState } from 'react';
|
||||
import type { SortingState } from '@tanstack/react-table';
|
||||
|
||||
import { useDebounce } from '@/hooks/useDebounce';
|
||||
|
||||
@@ -9,6 +10,7 @@ export type TenantStatusFilter = 'all' | 'active' | 'inactive';
|
||||
export function useTenantFilters() {
|
||||
const [skip, setSkip] = useState(0);
|
||||
const [limit, setLimitValue] = useState(10);
|
||||
const [sorting, setSortingValue] = useState<SortingState>([]);
|
||||
const [searchTerm, setSearchTermValue] = useState('');
|
||||
const [statusFilter, setStatusFilterValue] = useState<TenantStatusFilter>('all');
|
||||
const debouncedSearchTerm = useDebounce(searchTerm.trim(), 400);
|
||||
@@ -28,11 +30,18 @@ export function useTenantFilters() {
|
||||
setSkip(0);
|
||||
}, []);
|
||||
|
||||
const setSorting = useCallback((value: SortingState) => {
|
||||
setSortingValue(value);
|
||||
setSkip(0);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
skip,
|
||||
setSkip,
|
||||
limit,
|
||||
setLimit,
|
||||
sorting,
|
||||
setSorting,
|
||||
searchTerm,
|
||||
debouncedSearchTerm,
|
||||
setSearchTerm,
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
import { useMemo } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { SortingState } from '@tanstack/react-table';
|
||||
|
||||
import { getServerSortParams } from '@/components/data-table/sorting';
|
||||
import { clientService, planService, tenantService } from '@/services/api';
|
||||
import type { TenantListParams } from '@/types';
|
||||
|
||||
@@ -14,6 +16,7 @@ interface UseTenantsQueryParams {
|
||||
limit: number;
|
||||
searchTerm: string;
|
||||
statusFilter: TenantStatusFilter;
|
||||
sorting: SortingState;
|
||||
}
|
||||
|
||||
const clientLookupParams = {
|
||||
@@ -37,14 +40,14 @@ function buildTenantListParams({
|
||||
limit,
|
||||
searchTerm,
|
||||
statusFilter,
|
||||
sorting,
|
||||
}: UseTenantsQueryParams): TenantListParams {
|
||||
return {
|
||||
skip,
|
||||
limit,
|
||||
search_term: searchTerm || undefined,
|
||||
is_active: statusFilter === 'all' ? undefined : statusFilter === 'active',
|
||||
sort_by: 'created_at',
|
||||
sort_order: 'desc',
|
||||
...getServerSortParams(sorting),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ export default function TenantsPage() {
|
||||
setSkip,
|
||||
limit,
|
||||
setLimit,
|
||||
sorting,
|
||||
setSorting,
|
||||
searchTerm,
|
||||
debouncedSearchTerm,
|
||||
setSearchTerm,
|
||||
@@ -43,6 +45,7 @@ export default function TenantsPage() {
|
||||
limit,
|
||||
searchTerm: debouncedSearchTerm,
|
||||
statusFilter,
|
||||
sorting,
|
||||
});
|
||||
const clientsLookupQuery = useClientLookupQuery(isSheetOpen);
|
||||
const plansLookupQuery = usePlanLookupQuery(isSheetOpen);
|
||||
@@ -163,8 +166,10 @@ export default function TenantsPage() {
|
||||
skip={skip}
|
||||
limit={limit}
|
||||
total={total}
|
||||
sorting={sorting}
|
||||
onPageChange={setSkip}
|
||||
onLimitChange={setLimit}
|
||||
onSortingChange={setSorting}
|
||||
/>
|
||||
|
||||
<PoweredBy />
|
||||
|
||||
Reference in New Issue
Block a user