react-table#SortingRule TypeScript Examples
The following examples show how to use
react-table#SortingRule.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Vulnerabilities.tsx From crossfeed with Creative Commons Zero v1.0 Universal | 4 votes |
Vulnerabilities: React.FC<{ groupBy?: string }> = ({
groupBy = undefined
}: {
children?: React.ReactNode;
groupBy?: string;
}) => {
const {
currentOrganization,
apiPost,
apiPut,
showAllOrganizations
} = useAuthContext();
const [vulnerabilities, setVulnerabilities] = useState<Vulnerability[]>([]);
const [totalResults, setTotalResults] = useState(0);
const tableRef = useRef<TableInstance<Vulnerability>>(null);
const listClasses = useStyles();
const [noResults, setNoResults] = useState(false);
const updateVulnerability = useCallback(
async (index: number, body: { [key: string]: string }) => {
try {
const res = await apiPut<Vulnerability>(
'/vulnerabilities/' + vulnerabilities[index].id,
{
body: body
}
);
const vulnCopy = [...vulnerabilities];
vulnCopy[index].state = res.state;
vulnCopy[index].substate = res.substate;
vulnCopy[index].actions = res.actions;
setVulnerabilities(vulnCopy);
} catch (e) {
console.error(e);
}
},
[setVulnerabilities, apiPut, vulnerabilities]
);
const columns = useMemo(() => createColumns(updateVulnerability), [
updateVulnerability
]);
const groupedColumns = useMemo(() => createGroupedColumns(), []);
const vulnerabilitiesSearch = useCallback(
async ({
filters,
sort,
page,
pageSize = PAGE_SIZE,
doExport = false,
groupBy = undefined
}: {
filters: Filters<Vulnerability>;
sort: SortingRule<Vulnerability>[];
page: number;
pageSize?: number;
doExport?: boolean;
groupBy?: string;
}): Promise<ApiResponse | undefined> => {
try {
const tableFilters: {
[key: string]: string | boolean | undefined;
} = filters
.filter((f) => Boolean(f.value))
.reduce(
(accum, next) => ({
...accum,
[next.id]: next.value
}),
{}
);
// If not open or closed, substitute for appropriate substate
if (
tableFilters['state'] &&
!['open', 'closed'].includes(tableFilters['state'] as string)
) {
const substate = (tableFilters['state'] as string)
.match(/\((.*)\)/)
?.pop();
if (substate)
tableFilters['substate'] = substate.toLowerCase().replace(' ', '-');
delete tableFilters['state'];
}
if (!showAllOrganizations && currentOrganization) {
if ('rootDomains' in currentOrganization)
tableFilters['organization'] = currentOrganization.id;
else tableFilters['tag'] = currentOrganization.id;
}
if (tableFilters['isKev']) {
// Convert string to boolean filter.
tableFilters['isKev'] = tableFilters['isKev'] === 'true';
}
return await apiPost<ApiResponse>(
doExport ? '/vulnerabilities/export' : '/vulnerabilities/search',
{
body: {
page,
sort: sort[0]?.id ?? 'createdAt',
order: sort[0]?.desc ? 'DESC' : 'ASC',
filters: tableFilters,
pageSize,
groupBy
}
}
);
} catch (e) {
console.error(e);
return;
}
},
[apiPost, currentOrganization, showAllOrganizations]
);
const fetchVulnerabilities = useCallback(
async (query: Query<Vulnerability>) => {
const resp = await vulnerabilitiesSearch({
filters: query.filters,
sort: query.sort,
page: query.page,
groupBy
});
if (!resp) return;
const { result, count } = resp;
setVulnerabilities(result);
setTotalResults(count);
setNoResults(count === 0);
},
[vulnerabilitiesSearch, groupBy]
);
const fetchVulnerabilitiesExport = async (): Promise<string> => {
const { sortBy, filters } = tableRef.current?.state ?? {};
const { url } = (await vulnerabilitiesSearch({
filters: filters!,
sort: sortBy!,
page: 1,
pageSize: -1,
doExport: true
})) as ApiResponse;
return url!;
};
const renderPagination = (table: TableInstance<Vulnerability>) => (
<Paginator
table={table}
totalResults={totalResults}
export={{
name: 'vulnerabilities',
getDataToExport: fetchVulnerabilitiesExport
}}
/>
);
const initialFilterBy: Filters<Vulnerability> = [];
let initialSortBy: SortingRule<Vulnerability>[] = [];
const params = parse(window.location.search);
if (!('state' in params)) params['state'] = 'open';
for (const param of Object.keys(params)) {
if (param === 'sort') {
initialSortBy = [
{
id: params[param] as string,
desc: 'desc' in params ? params['desc'] === 'true' : true
}
];
} else if (param !== 'desc') {
initialFilterBy.push({
id: param,
value: params[param] as string
});
}
}
return (
<div>
<div className={listClasses.contentWrapper}>
<Subnav
items={[
{ title: 'Search Results', path: '/inventory', exact: true },
{ title: 'All Domains', path: '/inventory/domains' },
{ title: 'All Vulnerabilities', path: '/inventory/vulnerabilities' }
]}
></Subnav>
<br></br>
<div className={classes.root}>
<Table<Vulnerability>
renderPagination={renderPagination}
columns={groupBy ? groupedColumns : columns}
data={vulnerabilities}
pageCount={Math.ceil(totalResults / PAGE_SIZE)}
fetchData={fetchVulnerabilities}
tableRef={tableRef}
initialFilterBy={initialFilterBy}
initialSortBy={initialSortBy}
noResults={noResults}
pageSize={PAGE_SIZE}
noResultsMessage={
"We don't see any vulnerabilities that match your criteria."
}
/>
</div>
</div>
</div>
);
}