refactor(ui/datatables): allow for not sort

refactor/EE-4337/service-task-datatable
Chaim Lev-Ari 2023-08-19 20:27:52 +03:00
parent f780207b82
commit 92dd6ed7bc
12 changed files with 41 additions and 32 deletions

View File

@ -25,21 +25,24 @@ export function paginationSettings<T extends PaginationTableSettings>(
}
export interface SortableTableSettings {
sortBy: { id: string; desc: boolean };
setSortBy: (id: string, desc: boolean) => void;
sortBy: { id: string; desc: boolean } | undefined;
setSortBy: (id: string | undefined, desc: boolean) => void;
}
export function sortableSettings<T extends SortableTableSettings>(
set: ZustandSetFunc<T>,
initialSortBy: string | { id: string; desc: boolean }
initialSortBy?: string | { id: string; desc: boolean }
): SortableTableSettings {
return {
sortBy:
typeof initialSortBy === 'string'
? { id: initialSortBy, desc: false }
: initialSortBy,
setSortBy: (id: string, desc: boolean) =>
set((s) => ({ ...s, sortBy: { id, desc } })),
setSortBy: (id: string | undefined, desc: boolean) =>
set((s) => ({
...s,
sortBy: typeof id === 'string' ? { id, desc } : id,
})),
};
}
@ -79,7 +82,7 @@ export interface BasicTableSettings
export function createPersistedStore<T extends BasicTableSettings>(
storageKey: string,
initialSortBy: string | { id: string; desc: boolean } = 'name',
initialSortBy?: string | { id: string; desc: boolean },
create: (set: ZustandSetFunc<T>) => Omit<T, keyof BasicTableSettings> = () =>
({} as T)
) {
@ -87,11 +90,8 @@ export function createPersistedStore<T extends BasicTableSettings>(
persist(
(set) =>
({
...sortableSettings(
set as ZustandSetFunc<SortableTableSettings>,
initialSortBy
),
...paginationSettings(set as ZustandSetFunc<PaginationTableSettings>),
...sortableSettings<T>(set, initialSortBy),
...paginationSettings<T>(set),
...create(set),
} as T),
{

View File

@ -23,21 +23,24 @@ export function useTableState<
}
export function useTableStateWithoutStorage(
defaultSortKey: string
defaultSortKey?: string
): BasicTableSettings & {
setSearch: (search: string) => void;
search: string;
} {
const [search, setSearch] = useState('');
const [pageSize, setPageSize] = useState(10);
const [sortBy, setSortBy] = useState({ id: defaultSortKey, desc: false });
const [sortBy, setSortBy] = useState(
defaultSortKey ? { id: defaultSortKey, desc: false } : undefined
);
return {
search,
setSearch,
pageSize,
setPageSize,
setSortBy: (id: string, desc: boolean) => setSortBy({ id, desc }),
setSortBy: (id: string | undefined, desc: boolean) =>
setSortBy(id ? { id, desc } : undefined),
sortBy,
};
}

View File

@ -56,8 +56,8 @@ export function EdgeGroupAssociationTable({
pageLimit: tableState.pageSize,
page: page + 1,
search: tableState.search,
sort: tableState.sortBy.id as 'Group' | 'Name',
order: tableState.sortBy.desc ? 'desc' : 'asc',
sort: tableState.sortBy?.id as 'Group' | 'Name',
order: tableState.sortBy?.desc ? 'desc' : 'asc',
...query,
});
const groupsQuery = useGroups({

View File

@ -9,7 +9,7 @@ import { useEnvironments } from './useEnvironments';
const storageKey = 'edge-devices-waiting-room';
const settingsStore = createPersistedStore(storageKey);
const settingsStore = createPersistedStore(storageKey, 'name');
export function Datatable() {
const tableState = useTableState(settingsStore, storageKey);

View File

@ -44,8 +44,8 @@ export function EnvironmentsDatatable() {
pageLimit: tableState.pageSize,
page: page + 1,
search: tableState.search,
sort: tableState.sortBy.id as 'Group' | 'Name',
order: tableState.sortBy.desc ? 'desc' : 'asc',
sort: tableState.sortBy?.id as 'Group' | 'Name',
order: tableState.sortBy?.desc ? 'desc' : 'asc',
edgeStackId: stackId,
edgeStackStatus: statusFilter,
});

View File

@ -15,7 +15,7 @@ import { IngressControllerClassMap } from '../types';
import { columns } from './columns';
const storageKey = 'ingressClasses';
const settingsStore = createPersistedStore(storageKey);
const settingsStore = createPersistedStore(storageKey, 'name');
interface Props {
onChangeControllers: (

View File

@ -37,8 +37,10 @@ export function EnvironmentsDatatable({
excludeSnapshots: true,
page: page + 1,
pageLimit: tableState.pageSize,
sort: isSortType(tableState.sortBy.id) ? tableState.sortBy.id : undefined,
order: tableState.sortBy.desc ? 'desc' : 'asc',
sort: isSortType(tableState.sortBy?.id)
? tableState.sortBy?.id
: undefined,
order: tableState.sortBy?.desc ? 'desc' : 'asc',
},
{ enabled: groupsQuery.isSuccess, refetchInterval: 30 * 1000 }
);

View File

@ -38,8 +38,8 @@ export function GroupAssociationTable({
pageLimit: tableState.pageSize,
page: page + 1,
search: tableState.search,
sort: tableState.sortBy.id as 'Name',
order: tableState.sortBy.desc ? 'desc' : 'asc',
sort: tableState.sortBy?.id as 'Name',
order: tableState.sortBy?.desc ? 'desc' : 'asc',
...query,
});

View File

@ -14,7 +14,7 @@ export const ENVIRONMENTS_POLLING_INTERVAL = 30000; // in ms
export const SortOptions = ['Name', 'Group', 'Status'] as const;
export type SortType = (typeof SortOptions)[number];
export function isSortType(value: string): value is SortType {
export function isSortType(value?: string): value is SortType {
return SortOptions.includes(value as SortType);
}

View File

@ -33,7 +33,9 @@ export function TeamMembersList({ users, roles, disabled, teamId }: Props) {
const [search, setSearch] = useState('');
const [pageSize, setPageSize] = useState(10);
const [sortBy, setSortBy] = useState({ id: 'name', desc: false });
const [sortBy, setSortBy] = useState<
{ id: string; desc: boolean } | undefined
>({ id: 'name', desc: false });
const { isAdmin } = useUser();
@ -79,8 +81,8 @@ export function TeamMembersList({ users, roles, disabled, teamId }: Props) {
</RowProvider>
);
function handleSetSort(colId: string, desc: boolean) {
setSortBy({ id: colId, desc });
function handleSetSort(colId: string | undefined, desc: boolean) {
setSortBy(colId ? { id: colId, desc } : undefined);
}
function handleRemoveMembers(userIds: UserId[]) {

View File

@ -25,7 +25,9 @@ export function UsersList({ users, disabled, teamId }: Props) {
const [search, setSearch] = useState('');
const [pageSize, setPageSize] = useState(10);
const addMemberMutation = useAddMemberMutation(teamId);
const [sortBy, setSortBy] = useState({ id: 'name', desc: false });
const [sortBy, setSortBy] = useState<
{ id: string; desc: boolean } | undefined
>({ id: 'name', desc: false });
const { isAdmin } = useUser();
@ -62,8 +64,8 @@ export function UsersList({ users, disabled, teamId }: Props) {
</RowProvider>
);
function handleSetSort(colId: string, desc: boolean) {
setSortBy({ id: colId, desc });
function handleSetSort(colId: string | undefined, desc: boolean) {
setSortBy(colId ? { id: colId, desc } : undefined);
}
function handleAddAllMembers(userIds: UserId[]) {

View File

@ -25,7 +25,7 @@ interface Props {
isAdmin: boolean;
}
const settingsStore = createPersistedStore(storageKey);
const settingsStore = createPersistedStore(storageKey, 'name');
export function TeamsDatatable({ teams, isAdmin }: Props) {
const { handleRemove } = useRemoveMutation();