Added an export feature for the master statisticl screen

This commit is contained in:
Joao Ramos
2024-09-05 22:42:46 +01:00
parent 70de97766e
commit a61ad2cc7e
6 changed files with 309 additions and 89 deletions

View File

@@ -1,18 +1,6 @@
import {useState, useMemo} from "react";
import Input from "@/components/Low/Input";
/*fields example = [
['id'],
['companyInformation', 'companyInformation', 'name']
]*/
const getFieldValue = (fields: string[], data: any): string => {
if (fields.length === 0) return data;
const [key, ...otherFields] = fields;
if (data[key]) return getFieldValue(otherFields, data[key]);
return data;
};
import { search } from "@/utils/search";
export function useListSearch<T>(fields: string[][], rows: T[]) {
const [text, setText] = useState("");
@@ -20,22 +8,11 @@ export function useListSearch<T>(fields: string[][], rows: T[]) {
const renderSearch = () => <Input label="Search" type="text" name="search" onChange={setText} placeholder="Enter search text" value={text} />;
const updatedRows = useMemo(() => {
const searchText = text.toLowerCase();
return rows.filter((row) => {
return fields.some((fieldsKeys) => {
const value = getFieldValue(fieldsKeys, row);
if (typeof value === "string") {
return value.toLowerCase().includes(searchText);
}
if (typeof value === "number") {
return (value as Number).toString().includes(searchText);
}
});
});
return search(text, fields, rows);
}, [fields, rows, text]);
return {
text,
rows: updatedRows,
renderSearch,
};