diff --git a/front/src/components/table/Table.tsx b/front/src/components/table/Table.tsx index 7de44456be..fd3eec5067 100644 --- a/front/src/components/table/Table.tsx +++ b/front/src/components/table/Table.tsx @@ -16,7 +16,7 @@ type OwnProps = { columns: Array>; viewName: string; viewIcon?: IconProp; - onSortsUpdate?: React.Dispatch>; + onSortsUpdate?: (sorts: Array) => void; }; const StyledTable = styled.table` diff --git a/front/src/components/table/table-header/TableHeader.tsx b/front/src/components/table/table-header/TableHeader.tsx index 18d651ddaf..8253571bc6 100644 --- a/front/src/components/table/table-header/TableHeader.tsx +++ b/front/src/components/table/table-header/TableHeader.tsx @@ -4,12 +4,12 @@ import DropdownButton from './DropdownButton'; import { IconProp } from '@fortawesome/fontawesome-svg-core'; import { faCalendar } from '@fortawesome/pro-regular-svg-icons'; import SortAndFilterBar, { SortType } from './SortAndFilterBar'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; type OwnProps = { viewName: string; viewIcon?: IconProp; - onSortsUpdate?: React.Dispatch>; + onSortsUpdate?: (sorts: Array) => void; }; const StyledContainer = styled.div` @@ -53,15 +53,17 @@ function TableHeader({ viewName, viewIcon, onSortsUpdate }: OwnProps) { label: 'Created at', order: 'asc', id: sortId, - }, + } as SortType, ]); - onSortsUpdate && onSortsUpdate(sorts); }; - const onSortItemUnSelect = (sortId: string) => { setSorts([]); }; + useEffect(() => { + onSortsUpdate && onSortsUpdate(sorts); + }, [sorts, onSortsUpdate]); + const sortsAvailable: Array = [ { id: 'created_at', diff --git a/front/src/pages/people/People.tsx b/front/src/pages/people/People.tsx index 65cff1c701..04e36c52bd 100644 --- a/front/src/pages/people/People.tsx +++ b/front/src/pages/people/People.tsx @@ -34,7 +34,7 @@ export const GET_PEOPLE = gql` // @TODO get those types from generated-code person-order-by type OrderBy = Record; -const defaultOrderBy = [ +const defaultOrderBy: OrderBy[] = [ { created_at: 'desc', }, @@ -49,8 +49,14 @@ const reduceSortsToOrderBy = (sorts: Array): OrderBy[] => { }; function People() { - const [sorts, setSorts] = useState([] as Array); - const orderBy = sorts.length ? reduceSortsToOrderBy(sorts) : defaultOrderBy; + const [, setSorts] = useState([] as Array); + const [orderBy, setOrderBy] = useState(defaultOrderBy); + + const updateSorts = (sorts: Array) => { + setSorts(sorts); + setOrderBy(sorts.length ? reduceSortsToOrderBy(sorts) : defaultOrderBy); + }; + const { data } = useQuery<{ person: GraphqlPerson[] }>(GET_PEOPLE, { variables: { orderBy: orderBy }, }); @@ -58,15 +64,15 @@ function People() { return ( - {data && ( + { - )} + } );