From 3eb56bbd31eb386b4eaabfb19a979dc0280ae235 Mon Sep 17 00:00:00 2001 From: Aaron Shar Date: Tue, 28 May 2024 19:52:03 -0700 Subject: [PATCH] implemented filtering --- frontend/pages/myNotesPage.tsx | 130 ++++++++++++++++++++------------- frontend/pages/upload.jsx | 2 +- frontend/public/images/x.svg | 1 + 3 files changed, 83 insertions(+), 50 deletions(-) create mode 100644 frontend/public/images/x.svg diff --git a/frontend/pages/myNotesPage.tsx b/frontend/pages/myNotesPage.tsx index 48ac3fc..e588684 100644 --- a/frontend/pages/myNotesPage.tsx +++ b/frontend/pages/myNotesPage.tsx @@ -15,6 +15,7 @@ import { useAuth } from '@/contexts/AuthContext'; import { useRouter } from 'next/router'; import jsPDF from 'jspdf'; import Link from "next/link"; +import Image from 'next/image'; interface Note { note_id: string; @@ -28,13 +29,15 @@ interface Note { } function myNotesPage() { - const { currentUser } = useAuth() - const router = useRouter() - const [notesData, setNotesData] = useState([]) - const [sortedNotes, setSortedNotes] = useState([]) - const [sortOrder, setSortOrder] = useState('asc') - const [priorityTag, setPriorityTag] = useState('') + const { currentUser } = useAuth(); + const router = useRouter(); + const [notesData, setNotesData] = useState([]); + const [sortedNotes, setSortedNotes] = useState([]); + const [filteredNotes, setFilteredNotes] = useState([]); + const [sortOrder, setSortOrder] = useState('asc'); + const [priorityTag, setPriorityTag] = useState(''); const [generatedTags, setGeneratedTags] = useState<{ [key: string]: string[] }>({}); + const [filteredTags, setFilteredTags] = useState([]); if (!currentUser){ @@ -67,12 +70,7 @@ function myNotesPage() { }); setSortedNotes(notesData); setNotesData(notesData); - - - // Generate tags automatically for each note - //for (const note of notesData) { - //generateTags(note); - //} + setFilteredNotes(notesData); return notesData } @@ -83,25 +81,51 @@ function myNotesPage() { const handleSortChange = (event: React.ChangeEvent) => { const order = event.target.value setSortOrder(order) - sortNotes(order, priorityTag) + sortNotes(order) } - const handleTagChange = (event: React.ChangeEvent) => { - const tag = event.target.value; - setPriorityTag(tag) - sortNotes(sortOrder, tag) + const handleNewTag = (event: React.ChangeEvent) => { + event.preventDefault(); + const tag = event.target[0].value; + if (tag.length < 1) { + return; + } + setPriorityTag(""); + const updatedTags = [...filteredTags, tag]; + filterNotesByTags(updatedTags); + setFilteredTags(updatedTags); } - const sortNotes = (order: string, tag: string) => { - const sorted = [...notesData].sort((a, b) => { - const aHasTag = a.tags.includes(tag) ? 1 : 0 - const bHasTag = b.tags.includes(tag) ? 1 : 0 + const handleRemoveTag = (index: number) => { + let tags = [...filteredTags]; + tags.splice(index, 1) + console.log(tags) + filterNotesByTags(tags); + setFilteredTags(tags); + } - if (aHasTag !== bHasTag) { - // Prioritize notes with a tag - return bHasTag - aHasTag + const filterNotesByTags = (filter: string[], newlySortedNotes?: any) => { + let notes = [] + if (newlySortedNotes == null){ + notes = [...sortedNotes]; + } else { + notes = newlySortedNotes; + } + console.log(filter.length) + const filteredNotes = notes.filter((note) => { + const tags = note.tags; + for (let i=0; i { + const sorted = [...notesData].sort((a, b) => { const dateA = new Date(a.last_modified) // to sort by last modified dates const dateB = new Date(b.last_modified) // to sort by last modified dates @@ -120,11 +144,8 @@ function myNotesPage() { } }) setSortedNotes(sorted) + filterNotesByTags(filteredTags, sorted) } - - - - return ( <> @@ -132,29 +153,40 @@ function myNotesPage() {

My Notes

-
- - -
-
- - +
+
+ + +
+
+
+ + setPriorityTag(e.currentTarget.value)} + /> +
+
+
+ {filteredTags.map((tag, index) => ( + + handleRemoveTag(index)}/> + {tag} + + ))} +
-
- {sortedNotes ? (sortedNotes.map((note) => ( + {sortedNotes ? (filteredNotes.map((note) => (
diff --git a/frontend/pages/upload.jsx b/frontend/pages/upload.jsx index c0e96b2..4eaf09e 100644 --- a/frontend/pages/upload.jsx +++ b/frontend/pages/upload.jsx @@ -68,7 +68,7 @@ function UploadPage() { const generateTags = async (text) => { try { const response = await axios.post("http://127.0.0.1:5000/generate-tags", { text }); - return response.data.tags.split(','); + return response.data.tags.split(', '); } catch (error) { console.error("Error generating tags:", error); return []; diff --git a/frontend/public/images/x.svg b/frontend/public/images/x.svg new file mode 100644 index 0000000..7d5875c --- /dev/null +++ b/frontend/public/images/x.svg @@ -0,0 +1 @@ + \ No newline at end of file