Some Changes in Directory in Search functionality and Filter by Names.

This commit is contained in:
Kartik sharma 2025-06-26 10:09:24 +05:30
parent 8916e25940
commit 7898d1ad81
2 changed files with 74 additions and 20 deletions

View File

@ -27,24 +27,66 @@ const NotesCardViewDirectory = ({ notes, setNotes, searchText, selectedNoteNames
fetchNotes(); fetchNotes();
}, []); }, []);
// useEffect(() => {
// const lowerSearch = searchText?.toLowerCase() || "";
// const filtered = allNotes.filter((noteItem) => {
// const plainNote = noteItem?.note?.replace(/<[^>]+>/g, "").toLowerCase();
// const fullName = `${noteItem?.createdBy?.firstName || ""} ${noteItem?.createdBy?.lastName || ""}`.trim(); // Get full name
// const lowerFullName = fullName.toLowerCase(); // Convert to lowercase for comparison
// const createdDate = new Date(noteItem?.createdAt).toLocaleDateString("en-IN").toLowerCase();
// const matchesSearch =
// plainNote.includes(lowerSearch) ||
// lowerFullName.includes(lowerSearch) ||
// createdDate.includes(lowerSearch);
// // Filter logic for multiple selected names
// const matchesNameFilter =
// selectedNoteNames.length === 0 || // If no names are selected, all notes pass this filter
// selectedNoteNames.includes(fullName); // Check if the note's creator is in the selected names array
// return matchesSearch && matchesNameFilter;
// });
// setFilteredNotes(filtered);
// setNotes(filtered);
// setCurrentPage(1);
// setTotalPages(Math.ceil(filtered.length / pageSize));
// }, [searchText, allNotes, selectedNoteNames]); // Add selectedNoteNames to dependencies
useEffect(() => { useEffect(() => {
const lowerSearch = searchText?.toLowerCase() || ""; const lowerSearch = searchText?.toLowerCase() || "";
const filtered = allNotes.filter((noteItem) => { const filtered = allNotes.filter((noteItem) => {
const plainNote = noteItem?.note?.replace(/<[^>]+>/g, "").toLowerCase(); const plainNote = noteItem?.note?.replace(/<[^>]+>/g, "").toLowerCase();
const fullName = `${noteItem?.createdBy?.firstName || ""} ${noteItem?.createdBy?.lastName || ""}`.trim(); // Get full name const fullName = `${noteItem?.createdBy?.firstName || ""} ${noteItem?.createdBy?.lastName || ""}`.trim();
const lowerFullName = fullName.toLowerCase(); // Convert to lowercase for comparison const lowerFullName = fullName.toLowerCase();
const createdDate = new Date(noteItem?.createdAt).toLocaleDateString("en-IN").toLowerCase(); const createdDate = new Date(noteItem?.createdAt).toLocaleDateString("en-IN").toLowerCase();
const matchesSearch = // Collect all string values in the note object to search through
plainNote.includes(lowerSearch) || const stringValues = [];
lowerFullName.includes(lowerSearch) ||
createdDate.includes(lowerSearch); const extractStrings = (obj) => {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const value = obj[key];
if (typeof value === "string") {
stringValues.push(value.toLowerCase());
} else if (typeof value === "object" && value !== null) {
extractStrings(value); // Recursively extract from nested objects
}
}
};
extractStrings(noteItem);
// Add manually stripped note, full name, date, etc.
stringValues.push(plainNote, lowerFullName, createdDate);
const matchesSearch = stringValues.some((val) => val.includes(lowerSearch));
// Filter logic for multiple selected names
const matchesNameFilter = const matchesNameFilter =
selectedNoteNames.length === 0 || // If no names are selected, all notes pass this filter selectedNoteNames.length === 0 || selectedNoteNames.includes(fullName);
selectedNoteNames.includes(fullName); // Check if the note's creator is in the selected names array
return matchesSearch && matchesNameFilter; return matchesSearch && matchesNameFilter;
}); });
@ -53,7 +95,8 @@ const NotesCardViewDirectory = ({ notes, setNotes, searchText, selectedNoteNames
setNotes(filtered); setNotes(filtered);
setCurrentPage(1); setCurrentPage(1);
setTotalPages(Math.ceil(filtered.length / pageSize)); setTotalPages(Math.ceil(filtered.length / pageSize));
}, [searchText, allNotes, selectedNoteNames]); // Add selectedNoteNames to dependencies }, [searchText, allNotes, selectedNoteNames]);
const currentItems = useMemo(() => { const currentItems = useMemo(() => {
const startIndex = (currentPage - 1) * pageSize; const startIndex = (currentPage - 1) * pageSize;

View File

@ -30,21 +30,32 @@ const DirectoryPageHeader = ({
setFiltered(tempSelectedBucketIds?.length + tempSelectedCategoryIds?.length); setFiltered(tempSelectedBucketIds?.length + tempSelectedCategoryIds?.length);
}, [tempSelectedBucketIds, tempSelectedCategoryIds]); }, [tempSelectedBucketIds, tempSelectedCategoryIds]);
useEffect(() => { useEffect(() => {
if (viewType === "notes" && notesToExport && notesToExport.length > 0) { if (viewType === "notes") {
const uniqueNames = [...new Set(notesToExport.map(note => { if (notesToExport && notesToExport.length > 0) {
const firstName = note.createdBy?.firstName || ""; const uniqueNames = [...new Set(notesToExport.map(note => {
const lastName = note.createdBy?.lastName || ""; const firstName = note.createdBy?.firstName || "";
return `${firstName} ${lastName}`.trim(); const lastName = note.createdBy?.lastName || "";
}).filter(name => name !== ""))]; return `${firstName} ${lastName}`.trim();
setNoteCreators(uniqueNames.sort()); // Sort names for consistent display }).filter(name => name !== ""))];
setNoteCreators(uniqueNames.sort());
} else {
setNoteCreators([]);
}
} else { } else {
setNoteCreators([]); setNoteCreators([]);
setSelectedNoteNames([]); // Reset to empty array for multiple selection
} }
}, [notesToExport, viewType, setSelectedNoteNames]); // Add setSelectedNoteNames to dependencies }, [notesToExport, viewType]);
// Separate effect to clear selection only when switching away from notes
useEffect(() => {
if (viewType !== "notes" && selectedNoteNames.length > 0) {
setSelectedNoteNames([]);
}
}, [viewType]);
// New handler for multiple name selections
const handleToggleNoteName = (name) => { const handleToggleNoteName = (name) => {
setSelectedNoteNames(prevSelectedNames => { setSelectedNoteNames(prevSelectedNames => {
if (prevSelectedNames.includes(name)) { if (prevSelectedNames.includes(name)) {