From c54ec5261dd85d7b04a81ee1a0d9146f92cfc51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez?= Date: Tue, 19 Mar 2024 12:58:05 +0100 Subject: [PATCH] adds table filters --- .../analysis-eudr/category-list/index.tsx | 97 ++++++++++++------- .../supplier-list-table/table/columns.tsx | 9 +- .../supplier-list-table/table/index.tsx | 10 +- client/src/store/features/eudr/index.ts | 25 +++++ 4 files changed, 101 insertions(+), 40 deletions(-) diff --git a/client/src/containers/analysis-eudr/category-list/index.tsx b/client/src/containers/analysis-eudr/category-list/index.tsx index 9c15de6dd..fabb6e405 100644 --- a/client/src/containers/analysis-eudr/category-list/index.tsx +++ b/client/src/containers/analysis-eudr/category-list/index.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import DeforestationFreeSuppliersBreakdown from './breakdown/deforestation-free-suppliers'; import SuppliersWithDeforestationAlertsBreakdown from './breakdown/suppliers-with-deforestation-alerts'; @@ -8,10 +8,12 @@ import { Button } from '@/components/button'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; import { cn } from '@/lib/utils'; import { useEUDRData } from '@/hooks/eudr'; -import { useAppSelector } from '@/store/hooks'; -import { eudr } from '@/store/features/eudr'; +import { useAppDispatch, useAppSelector } from '@/store/hooks'; +import { eudr, setTableFilters } from '@/store/features/eudr'; import { themeColors } from '@/utils/colors'; +import type { EUDRState } from '@/store/features/eudr'; + export const CATEGORIES = [ { name: 'Deforestation-free suppliers', @@ -27,6 +29,15 @@ export const CATEGORIES = [ }, ] as const; +const CATEGORY_TO_FILTER: Record< + (typeof CATEGORIES)[number]['name'], + Partial +> = { + [CATEGORIES[0].name]: 'dfs', + [CATEGORIES[1].name]: 'sda', + [CATEGORIES[2].name]: 'tpl', +} as const; + type CategoryState = Record<(typeof CATEGORIES)[number]['name'], boolean>; export const CategoryList = (): JSX.Element => { @@ -43,7 +54,25 @@ export const CategoryList = (): JSX.Element => { const { viewBy, filters: { dates, suppliers, origins, materials, plots }, + table: { filters: tableFilters }, } = useAppSelector(eudr); + const dispatch = useAppDispatch(); + + const onClickCategory = useCallback( + (category: (typeof CATEGORIES)[number]) => { + toggleCategory((prev) => ({ + ...prev, + [category.name]: !prev[category.name], + })); + + dispatch( + setTableFilters({ + [CATEGORY_TO_FILTER[category.name]]: !tableFilters[CATEGORY_TO_FILTER[category.name]], + }), + ); + }, + [dispatch, tableFilters], + ); const { data } = useEUDRData( { @@ -75,42 +104,38 @@ export const CategoryList = (): JSX.Element => { { - toggleCategory((prev) => ({ - ...prev, - [category.name]: !prev[category.name], - })); - }} + onOpenChange={() => onClickCategory(category)} > -
-
- - {category.name} -
-
-
- {`${category.totalPercentage.toFixed(2)}%`}{' '} - of suppliers -
-
-
+
+
+ + {category.name}
-
- +
+
+ {`${category.totalPercentage.toFixed(2)}%`}{' '} + of suppliers +
+
+
+
+
+ - -
+
+ {category.name === CATEGORIES[0].name && } {category.name === CATEGORIES[1].name && } diff --git a/client/src/containers/analysis-eudr/supplier-list-table/table/columns.tsx b/client/src/containers/analysis-eudr/supplier-list-table/table/columns.tsx index a12ed8049..8e5ff77a8 100644 --- a/client/src/containers/analysis-eudr/supplier-list-table/table/columns.tsx +++ b/client/src/containers/analysis-eudr/supplier-list-table/table/columns.tsx @@ -8,6 +8,8 @@ import { BIG_NUMBER_FORMAT } from 'utils/number-format'; import type { Supplier } from '.'; import type { ColumnDef } from '@tanstack/react-table'; +const numberFormat = new Intl.NumberFormat(undefined, { maximumFractionDigits: 3 }); + export const columns: ColumnDef[] = [ { accessorKey: 'supplierName', @@ -49,7 +51,7 @@ export const columns: ColumnDef[] = [ header: ({ column }) => , cell: ({ row }) => { const dfs: number = row.getValue('dfs'); - return {`${Number.isNaN(dfs) ? '-' : `${dfs.toFixed(2)}%`}`}; + return {`${Number.isNaN(dfs) ? '-' : `${numberFormat.format(dfs)}%`}`}; }, }, { @@ -57,7 +59,7 @@ export const columns: ColumnDef[] = [ header: ({ column }) => , cell: ({ row }) => { const sda: number = row.getValue('sda'); - return {`${Number.isNaN(sda) ? '-' : `${sda.toFixed(2)}%`}`}; + return {`${Number.isNaN(sda) ? '-' : `${numberFormat.format(sda)}%`}`}; }, }, { @@ -65,7 +67,8 @@ export const columns: ColumnDef[] = [ header: ({ column }) => , cell: ({ row }) => { const tpl: number = row.getValue('tpl'); - return {`${Number.isNaN(tpl) ? '-' : `${tpl.toFixed(2)}%`}`}; + + return {`${Number.isNaN(tpl) ? '-' : `${numberFormat.format(tpl)}%`}`}; }, }, { diff --git a/client/src/containers/analysis-eudr/supplier-list-table/table/index.tsx b/client/src/containers/analysis-eudr/supplier-list-table/table/index.tsx index ca9dcd385..73a6ad681 100644 --- a/client/src/containers/analysis-eudr/supplier-list-table/table/index.tsx +++ b/client/src/containers/analysis-eudr/supplier-list-table/table/index.tsx @@ -57,6 +57,7 @@ const SuppliersListTable = (): JSX.Element => { const [sorting, setSorting] = useState([]); const { filters: { dates, suppliers, origins, materials, plots }, + table: { filters: tableFilters }, } = useAppSelector(eudr); const { data, isFetching } = useEUDRData( @@ -69,7 +70,14 @@ const SuppliersListTable = (): JSX.Element => { geoRegionIds: plots?.map(({ value }) => value), }, { - select: (data) => data?.table, + select: (data) => + data?.table.filter((dataRow) => { + if (Object.values(tableFilters).every((filter) => !filter)) return true; + + if (tableFilters.dfs && dataRow.dfs > 0) return true; + if (tableFilters.sda && dataRow.sda > 0) return true; + if (tableFilters.tpl && dataRow.tpl > 0) return true; + }), onSuccess: (data) => { dispatch(setTotalSuppliers(data?.length || 0)); }, diff --git a/client/src/store/features/eudr/index.ts b/client/src/store/features/eudr/index.ts index a7e26b46c..27dbf16fe 100644 --- a/client/src/store/features/eudr/index.ts +++ b/client/src/store/features/eudr/index.ts @@ -30,6 +30,13 @@ export type EUDRState = { to: string; }; }; + table: { + filters: { + dfs: boolean; + sda: boolean; + tpl: boolean; + }; + }; // map basemap: 'light' | 'planet'; planetLayer: LayerConfiguration; @@ -51,6 +58,13 @@ export const initialState: EUDRState = { to: DATES_RANGE[1], }, }, + table: { + filters: { + dfs: false, + sda: false, + tpl: false, + }, + }, basemap: 'light', supplierLayer: { active: true, @@ -104,6 +118,16 @@ export const EUDRSlice = createSlice({ ...action.payload, }, }), + setTableFilters: (state, action: PayloadAction>) => ({ + ...state, + table: { + ...state.table, + filters: { + ...state.table.filters, + ...action.payload, + }, + }, + }), setBasemap: (state, action: PayloadAction) => ({ ...state, basemap: action.payload, @@ -149,6 +173,7 @@ export const { setViewBy, setTotalSuppliers, setFilters, + setTableFilters, setBasemap, setSupplierLayer, setContextualLayer,