Skip to content

Commit

Permalink
feat: add url/query sort/search to sign on log table (#3215)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois committed Feb 28, 2023
1 parent ee20737 commit c489151
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions frontend/src/component/signOnLog/SignOnLogTable/SignOnLogTable.tsx
@@ -1,4 +1,4 @@
import { useMemo, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { TablePlaceholder, VirtualizedTable } from 'component/common/Table';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import useToast from 'hooks/useToast';
Expand All @@ -7,7 +7,7 @@ import { PageContent } from 'component/common/PageContent/PageContent';
import { PageHeader } from 'component/common/PageHeader/PageHeader';
import { useMediaQuery } from '@mui/material';
import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
import { useFlexLayout, useSortBy, useTable } from 'react-table';
import { SortingRule, useFlexLayout, useSortBy, useTable } from 'react-table';
import { sortTypes } from 'utils/sortTypes';
import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell';
import { TextCell } from 'component/common/Table/cells/TextCell/TextCell';
Expand All @@ -23,14 +23,41 @@ import { SignOnLogActionsCell } from './SignOnLogActionsCell/SignOnLogActionsCel
import { SignOnLogDeleteDialog } from './SignOnLogDeleteDialog/SignOnLogDeleteDialog';
import { useSignOnLogApi } from 'hooks/api/actions/useSignOnLogApi/useSignOnLogApi';
import { formatDateYMDHMS } from 'utils/formatDate';
import { useSearchParams } from 'react-router-dom';
import { createLocalStorage } from 'utils/createLocalStorage';

export type PageQueryType = Partial<
Record<'sort' | 'order' | 'search', string>
>;

const defaultSort: SortingRule<string> = { id: 'created_at' };

const { value: storedParams, setValue: setStoredParams } = createLocalStorage(
'SignOnLogTable:v1',
defaultSort
);

export const SignOnLogTable = () => {
const { setToastData, setToastApiError } = useToast();

const { events, loading, refetch } = useSignOnLog();
const { removeEvent } = useSignOnLogApi();

const [searchValue, setSearchValue] = useState('');
const [searchParams, setSearchParams] = useSearchParams();
const [initialState] = useState(() => ({
sortBy: [
{
id: searchParams.get('sort') || storedParams.id,
desc: searchParams.has('order')
? searchParams.get('order') === 'desc'
: storedParams.desc,
},
],
hiddenColumns: ['failure_reason'],
globalFilter: searchParams.get('search') || '',
}));

const [searchValue, setSearchValue] = useState(initialState.globalFilter);
const [deleteOpen, setDeleteOpen] = useState(false);
const [selectedEvent, setSelectedEvent] = useState<ISignOnEvent>();

Expand Down Expand Up @@ -120,18 +147,19 @@ export const SignOnLogTable = () => {
[]
);

const [initialState] = useState({
sortBy: [{ id: 'created_at' }],
hiddenColumns: ['failure_reason'],
});

const { data, getSearchText, getSearchContext } = useSearch(
columns,
searchValue,
events
);

const { headerGroups, rows, prepareRow, setHiddenColumns } = useTable(
const {
headerGroups,
rows,
prepareRow,
state: { sortBy },
setHiddenColumns,
} = useTable(
{
columns: columns as any,
data,
Expand Down Expand Up @@ -164,6 +192,25 @@ export const SignOnLogTable = () => {
columns
);

useEffect(() => {
const tableState: PageQueryType = {};
tableState.sort = sortBy[0].id;
if (sortBy[0].desc) {
tableState.order = 'desc';
}
if (searchValue) {
tableState.search = searchValue;
}

setSearchParams(tableState, {
replace: true,
});
setStoredParams({
id: sortBy[0].id,
desc: sortBy[0].desc || false,
});
}, [sortBy, searchValue, setSearchParams]);

return (
<PageContent
isLoading={loading}
Expand Down

0 comments on commit c489151

Please sign in to comment.