Skip to content

Commit

Permalink
chore(OH2-301): Add tests to table filter function (#680)
Browse files Browse the repository at this point in the history
Co-authored-by: SteveGT96 <[email protected]>
  • Loading branch information
SteveGT96 and SteveGT96 authored Oct 24, 2024
1 parent ccb65ad commit f3bda7a
Show file tree
Hide file tree
Showing 4 changed files with 450 additions and 95 deletions.
108 changes: 13 additions & 95 deletions src/components/accessories/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import moment from "moment";
import React, {
FunctionComponent,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import { filterData } from "libraries/tableUtils";
import React, { FunctionComponent, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import warningIcon from "../../../assets/warning-icon.png";
import {
Expand Down Expand Up @@ -255,95 +249,19 @@ const Table: FunctionComponent<IProps> = ({
setExpanded(!expanded);
};

const removeRowWhere = useCallback(
(
values: Record<string, any>[],
predicate: (row: Record<string, any>) => boolean
) => {
return values.filter((entry) => {
const row = (rawData ?? rowData).find(
(item) => item[rowKey ?? ""] === entry[rowKey ?? ""]
);
return !!row ? !predicate(row) : false;
});
},
[rawData, rowData, rowKey]
const filteredData = useMemo(
() =>
filterData(
rawData,
rowData,
rowKey,
filterColumns,
filters,
manualFilter
),
[filterColumns, filters, manualFilter, rowData]
);

const filteredData = useMemo(() => {
if ((filterColumns?.length ?? 0) === 0 || manualFilter) {
return rowData;
}
let result = rowData;
filterColumns.forEach((field) => {
const filter = filters[field.key];
if (filter) {
switch (field.type) {
case "boolean":
result = removeRowWhere(result, (row) =>
filter.value === undefined
? false
: (row[field.key] ?? false) !== filter.value
);
break;
case "number":
result = removeRowWhere(
result,
(row) =>
(filter.value === undefined
? false
: row[field.key] !== filter.value) ||
(filter.min === undefined
? false
: row[field.key] < filter.min) ||
(filter.max === undefined ? false : row[field.key] > filter.max)
);
break;
case "text":
result = removeRowWhere(result, (row) =>
filter.value === undefined
? false
: !row[field.key]
?.toString()
.toLowerCase()
.includes(filter.value.toString().toLowerCase())
);
break;

case "select":
result = removeRowWhere(result, (row) =>
filter.value === undefined
? false
: row[field.key] !== filter.value
);
break;

default:
result = removeRowWhere(
result,
(row) =>
(filter.value === undefined
? false
: !moment(row[field.key]).isSame(
moment(filter.value as string)
)) ||
(filter.min === undefined
? false
: moment(row[field.key]).isBefore(
moment(filter.min as string)
)) ||
(filter.max === undefined
? false
: moment(row[field.key]).isAfter(
moment(filter.max as string)
))
);
}
}
});
return result;
}, [filterColumns, filters, manualFilter, removeRowWhere, rowData]);

useEffect(() => {
if (onFilterChange && !manualFilter) {
onFilterChange(filters);
Expand Down
Loading

0 comments on commit f3bda7a

Please sign in to comment.