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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,7 +14,7 @@ export const ENVIRONMENTS_POLLING_INTERVAL = 30000; // in ms
export const SortOptions = ['Name', 'Group', 'Status'] as const; export const SortOptions = ['Name', 'Group', 'Status'] as const;
export type SortType = (typeof SortOptions)[number]; 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); 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 [search, setSearch] = useState('');
const [pageSize, setPageSize] = useState(10); 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(); const { isAdmin } = useUser();
@ -79,8 +81,8 @@ export function TeamMembersList({ users, roles, disabled, teamId }: Props) {
</RowProvider> </RowProvider>
); );
function handleSetSort(colId: string, desc: boolean) { function handleSetSort(colId: string | undefined, desc: boolean) {
setSortBy({ id: colId, desc }); setSortBy(colId ? { id: colId, desc } : undefined);
} }
function handleRemoveMembers(userIds: UserId[]) { function handleRemoveMembers(userIds: UserId[]) {

View File

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

View File

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