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();
}, []);
// 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(() => {
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 fullName = `${noteItem?.createdBy?.firstName || ""} ${noteItem?.createdBy?.lastName || ""}`.trim();
const lowerFullName = fullName.toLowerCase();
const createdDate = new Date(noteItem?.createdAt).toLocaleDateString("en-IN").toLowerCase();
const matchesSearch =
plainNote.includes(lowerSearch) ||
lowerFullName.includes(lowerSearch) ||
createdDate.includes(lowerSearch);
// Collect all string values in the note object to search through
const stringValues = [];
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 =
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
selectedNoteNames.length === 0 || selectedNoteNames.includes(fullName);
return matchesSearch && matchesNameFilter;
});
@ -53,7 +95,8 @@ const NotesCardViewDirectory = ({ notes, setNotes, searchText, selectedNoteNames
setNotes(filtered);
setCurrentPage(1);
setTotalPages(Math.ceil(filtered.length / pageSize));
}, [searchText, allNotes, selectedNoteNames]); // Add selectedNoteNames to dependencies
}, [searchText, allNotes, selectedNoteNames]);
const currentItems = useMemo(() => {
const startIndex = (currentPage - 1) * pageSize;

View File

@ -30,21 +30,32 @@ const DirectoryPageHeader = ({
setFiltered(tempSelectedBucketIds?.length + tempSelectedCategoryIds?.length);
}, [tempSelectedBucketIds, tempSelectedCategoryIds]);
useEffect(() => {
if (viewType === "notes" && notesToExport && notesToExport.length > 0) {
const uniqueNames = [...new Set(notesToExport.map(note => {
const firstName = note.createdBy?.firstName || "";
const lastName = note.createdBy?.lastName || "";
return `${firstName} ${lastName}`.trim();
}).filter(name => name !== ""))];
setNoteCreators(uniqueNames.sort()); // Sort names for consistent display
if (viewType === "notes") {
if (notesToExport && notesToExport.length > 0) {
const uniqueNames = [...new Set(notesToExport.map(note => {
const firstName = note.createdBy?.firstName || "";
const lastName = note.createdBy?.lastName || "";
return `${firstName} ${lastName}`.trim();
}).filter(name => name !== ""))];
setNoteCreators(uniqueNames.sort());
} else {
setNoteCreators([]);
}
} else {
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) => {
setSelectedNoteNames(prevSelectedNames => {
if (prevSelectedNames.includes(name)) {