ENCOA-37: Added the ability for users to download a list of the shown users

This commit is contained in:
Tiago Ribeiro
2024-05-08 15:46:24 +01:00
parent 72fb934d4f
commit 8b2459c304
4 changed files with 105 additions and 73 deletions

View File

@@ -1,4 +1,4 @@
import {useState, useMemo} from 'react';
import {useState, useMemo} from "react";
import Input from "@/components/Low/Input";
/*fields example = [
@@ -6,43 +6,33 @@ import Input from "@/components/Low/Input";
['companyInformation', 'companyInformation', 'name']
]*/
const getFieldValue = (fields: string[], data: any): string => {
if(fields.length === 0) return data;
const [key, ...otherFields] = fields;
if (fields.length === 0) return data;
const [key, ...otherFields] = fields;
if(data[key]) return getFieldValue(otherFields, data[key]);
return data;
if (data[key]) return getFieldValue(otherFields, data[key]);
return data;
};
export function useListSearch<T>(fields: string[][], rows: T[]) {
const [text, setText] = useState("");
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);
}
});
});
}, [fields, rows, text]);
return {
rows: updatedRows,
renderSearch,
};
}
export const useListSearch = (fields: string[][], rows: any[]) => {
const [text, setText] = useState('');
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);
}
})
})
}, [fields, rows, text])
return {
rows: updatedRows,
renderSearch,
}
}