Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better-search #71

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 52 additions & 21 deletions client/src/components/CustomTable/CustomTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ClearIcon from "@mui/icons-material/Clear";
import TableHeader from "./TableHeader";
import TableBody from "./TableBody";
import { sortData } from "../../utils/data";
import { debounce } from "lodash"; // Add this import at the top of the file

/**
* @typedef {import('types').TableColumn} TableColumn
Expand Down Expand Up @@ -65,22 +66,27 @@ const CustomTable = ({
const [searchTerm, setSearchTerm] = useState("");
const [localFilteredData, setLocalFilteredData] = useState(tableData.data);
const [page, setPage] = useState(paginationConfig?.page || 0);
const [rowsPerPage, setRowsPerPage] = useState(paginationConfig?.rowsPerPage || 10);
const [rowsPerPage, setRowsPerPage] = useState(
paginationConfig?.rowsPerPage || 10
);
const [orderBy, setOrderBy] = useState("");
const [order, setOrder] = useState("asc");
const [contextMenu, setContextMenu] = useState(null);
const [modalOpen, setModalOpen] = useState(false);
const [selectedRowId, setSelectedRowId] = useState(null);

const isPaginated = !!paginationConfig;
const isServerSidePaginated = isPaginated && paginationConfig.type === "server-side";
const isServerSidePaginated =
isPaginated && paginationConfig.type === "server-side";

const filteredData = useMemo(() => {
return isServerSidePaginated ? tableData.data : localFilteredData;
}, [tableData.data, localFilteredData, isServerSidePaginated]);

const sortedData = useMemo(() => {
return isServerSidePaginated ? filteredData : sortData(filteredData, orderBy, order);
return isServerSidePaginated
? filteredData
: sortData(filteredData, orderBy, order);
}, [filteredData, orderBy, order, isServerSidePaginated]);

const paginatedData = useMemo(() => {
Expand Down Expand Up @@ -224,7 +230,10 @@ const CustomTable = ({
{isLoading ? (
<tbody>
<tr>
<td colSpan={tableData.columns.length} style={{ textAlign: 'center', padding: '20px' }}>
<td
colSpan={tableData.columns.length}
style={{ textAlign: "center", padding: "20px" }}
>
<CircularProgress />
</td>
</tr>
Expand All @@ -241,25 +250,43 @@ const CustomTable = ({
</Table>
</TableContainer>
),
[paginatedData, tableData.columns, tableData.selectedIds, orderBy, order, selectedRowId, isLoading]
[
paginatedData,
tableData.columns,
tableData.selectedIds,
orderBy,
order,
selectedRowId,
isLoading,
]
);

// Create a debounced version of the search function
const debouncedSearch = useMemo(
() =>
debounce((value) => {
if (paginationConfig?.type === "server-side") {
if (value.length > 3 || value.length === 0) {
onSearch(value);
}
} else {
// Perform local wildcard search on all columns
const filtered = tableData.data.filter((item) =>
Object.values(item).some((field) =>
String(field).toLowerCase().includes(value.toLowerCase())
)
);
setLocalFilteredData(filtered);
setPage(0); // Reset to first page when filtering
}
}, 2000),
[paginationConfig, onSearch, tableData.data]
);

// Update the handleSearch function
const handleSearch = (value) => {
setSearchTerm(value);
if (paginationConfig?.type === "server-side") {
if (value.length > 3 || value.length === 0) {
onSearch(value);
}
} else {
// Perform local wildcard search on all columns
const filtered = tableData.data.filter(item =>
Object.values(item).some(field =>
String(field).toLowerCase().includes(value.toLowerCase())
)
);
setLocalFilteredData(filtered);
setPage(0); // Reset to first page when filtering
}
debouncedSearch(value);
};

return (
Expand Down Expand Up @@ -291,7 +318,11 @@ const CustomTable = ({
<TablePagination
rowsPerPageOptions={[5, 10]}
component="div"
count={isServerSidePaginated ? (paginationConfig.totalItems || 0) : filteredData.length}
count={
isServerSidePaginated
? paginationConfig.totalItems || 0
: filteredData.length
}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
Expand Down Expand Up @@ -376,4 +407,4 @@ const CustomTable = ({
);
};

export default CustomTable;
export default CustomTable;
14 changes: 5 additions & 9 deletions client/src/pages/Prospecting/Prospecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,13 @@ const Prospecting = () => {
let response;
if (isRefresh) {
await processNewProspectingActivity(userTimezone);
response = await fetchProspectingActivities(
selectedPeriod,
filteredIds
);
} else {
response = await fetchProspectingActivities(
selectedPeriod,
filteredIds
);
}

response = await fetchProspectingActivities(
selectedPeriod,
filteredIds
);

if (response.statusCode === 200 && response.success) {
setSummaryData(response.data[0].summary);
setRawData(response.data[0].raw_data || []);
Expand Down
Loading