Skip to content

Commit

Permalink
move StatisticTable logic to useTableFilters hook
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelemarre committed Jul 10, 2024
1 parent de6212c commit 7558e1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
28 changes: 3 additions & 25 deletions src/webapp/components/table/statistic-table/StatisticTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from "react";
import React from "react";
import styled from "styled-components";
import {
Table,
Expand All @@ -16,7 +16,6 @@ import { MultipleSelector } from "../../selector/MultipleSelector";
import { useTableFilters } from "../../../hooks/useTableFilters";
import { useTableCell } from "../../../hooks/useTableCell";
import { ColoredCell } from "./ColoredCell";
import _ from "../../../../domain/entities/generic/Collection";

export type TableColumn = {
value: string;
Expand Down Expand Up @@ -48,33 +47,12 @@ export type StatisticTableProps = {

export const StatisticTable: React.FC<StatisticTableProps> = React.memo(
({ rows, columns, columnRules, editRiskAssessmentColumns, filters: filtersConfig }) => {
const [searchTerm, setSearchTerm] = useState<string>("");

const [filters, setFilters] = useState<FiltersValuesType>(
filtersConfig.reduce((acc: FiltersValuesType, filter) => {
acc[filter.value] = [];
return acc;
}, {})
);

const calculateColumns = [...editRiskAssessmentColumns, ...Object.keys(columnRules)];

const filteredRows = useTableFilters(rows, searchTerm, filters);
const { searchTerm, setSearchTerm, filters, setFilters, filteredRows, filterOptions } =
useTableFilters(rows, filtersConfig);
const { getCellColor } = useTableCell(editRiskAssessmentColumns, columnRules);

const filterOptions = useCallback(
(column: TableColumn["value"]) => {
return _(rows)
.map(row => ({
value: row[column] || "",
label: row[column] || "",
}))
.uniqBy(filter => filter.value)
.value();
},
[rows]
);

return (
<React.Fragment>
<Container>
Expand Down
34 changes: 27 additions & 7 deletions src/webapp/hooks/useTableFilters.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useMemo } from "react";
import { useCallback, useMemo, useState } from "react";
import _ from "../../domain/entities/generic/Collection";
import {
FiltersValuesType,
FilterType,
StatisticTableProps,
TableColumn,
} from "../components/table/statistic-table/StatisticTable";

export const useTableFilters = (
rows: StatisticTableProps["rows"],
searchTerm: string,
filters: FiltersValuesType
) => {
export const useTableFilters = (rows: StatisticTableProps["rows"], filtersConfig: FilterType[]) => {
const [searchTerm, setSearchTerm] = useState<string>("");

const [filters, setFilters] = useState<FiltersValuesType>(
filtersConfig.reduce((acc: FiltersValuesType, filter) => {
acc[filter.value] = [];
return acc;
}, {})
);

const filteredRows = useMemo(() => {
const allFiltersEmpty = Object.keys(filters).every(
key => (filters[key] || []).length === 0
Expand All @@ -36,5 +43,18 @@ export const useTableFilters = (
}
}, [rows, searchTerm, filters]);

return filteredRows;
const filterOptions = useCallback(
(column: TableColumn["value"]) => {
return _(rows)
.map(row => ({
value: row[column] || "",
label: row[column] || "",
}))
.uniqBy(filter => filter.value)
.value();
},
[rows]
);

return { searchTerm, setSearchTerm, filters, setFilters, filteredRows, filterOptions };
};

0 comments on commit 7558e1a

Please sign in to comment.