-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add search bar to filter notes #3
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { DocumentTextIcon,PlusIcon } from '@heroicons/react/24/outline'; | ||
| import { DocumentTextIcon, MagnifyingGlassIcon, PlusIcon } from '@heroicons/react/24/outline'; | ||
| import { useEffect,useState } from 'react'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
|
|
||
|
|
@@ -16,6 +16,7 @@ export function HomePage() { | |
| const { notes, fetchNotes, deleteNote, createNote } = useNotes(); | ||
| const [deleteModalOpen, setDeleteModalOpen] = useState(false); | ||
| const [noteToDelete, setNoteToDelete] = useState<Note | null>(null); | ||
| const [search, setSearch] = useState(''); | ||
|
|
||
| const handleDelete = (note: Note) => { | ||
| setNoteToDelete(note); | ||
|
|
@@ -33,6 +34,11 @@ export function HomePage() { | |
| await createNote({ ...note, title: `${note.title} (copy)` }); | ||
| }; | ||
|
|
||
| const filteredNotes = notes.filter((note) => { | ||
| const q = search.toLowerCase(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return note.title?.toLowerCase().includes(q) || note.text?.toLowerCase().includes(q); | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| const loadNotes = async () => { | ||
| setIsLoading(true); | ||
|
|
@@ -58,7 +64,7 @@ export function HomePage() { | |
|
|
||
| return ( | ||
| <div className="min-h-screen p-6 max-w-6xl mx-auto"> | ||
| <header className="flex items-center justify-between mb-8"> | ||
| <header className="flex items-center justify-between mb-6"> | ||
| <h1 className="text-3xl font-bold text-gray-900">Notes</h1> | ||
| <button | ||
| onClick={() => navigate('/edit/new')} | ||
|
|
@@ -69,14 +75,30 @@ export function HomePage() { | |
| </button> | ||
| </header> | ||
|
|
||
| <div className="relative mb-8"> | ||
| <MagnifyingGlassIcon className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> | ||
| <input | ||
| type="text" | ||
| placeholder="Search notes..." | ||
| value={search} | ||
| onChange={(e) => setSearch(e.target.value)} | ||
| className="w-full pl-9 pr-4 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-300" | ||
| /> | ||
| </div> | ||
|
|
||
| {notes.length === 0 ? ( | ||
| <div className="flex flex-col items-center justify-center py-20 text-gray-500"> | ||
| <DocumentTextIcon className="w-16 h-16 mb-4" /> | ||
| <p className="text-xl">No notes yet. Create one!</p> | ||
| </div> | ||
| ) : filteredNotes.length === 0 ? ( | ||
| <div className="flex flex-col items-center justify-center py-20 text-gray-500"> | ||
| <MagnifyingGlassIcon className="w-16 h-16 mb-4" /> | ||
| <p className="text-xl">No notes match your search.</p> | ||
| </div> | ||
| ) : ( | ||
| <div className="grid grid-cols-[repeat(auto-fit,minmax(280px,1fr))] gap-6"> | ||
| {notes.map((note) => ( | ||
| {filteredNotes.map((note) => ( | ||
| <NoteCard | ||
| key={note.id} | ||
| note={note} | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short variable names affect code readability and complicate code refactoring, because of the difficulty in searching and replacing such short characters.