diff --git a/ao3 webpages/src/pages/notes.jsx b/ao3 webpages/src/pages/notes.jsx index 66e71af..b6f9669 100644 --- a/ao3 webpages/src/pages/notes.jsx +++ b/ao3 webpages/src/pages/notes.jsx @@ -1,7 +1,6 @@ -import { useState } from 'react' -import { Dialog } from '@headlessui/react' +import { useState, useRef, useEffect } from 'react' -const demoNotes = [ +const initialNotes = [ { id: '1', title: 'The Shoebox Project', tags: ['Humor', 'Drama'], completed: false }, { id: '2', title: 'Twist and Shout', tags: ['Angst', 'Historical', 'Tragedy'], completed: false }, { id: '3', title: 'Everyday Love in Stockholm', tags: ['Humor', 'Hurt', 'Loki'], completed: false }, @@ -11,157 +10,234 @@ const demoNotes = [ ] export default function Component() { - const [notes, setNotes] = useState(demoNotes) + const [notes, setNotes] = useState(() => { + const savedNotes = localStorage.getItem('notes') + return savedNotes ? JSON.parse(savedNotes) : initialNotes + }) const [isModalOpen, setIsModalOpen] = useState(false) - const [newNote, setNewNote] = useState({ title: '', tags: '' }) + const [newNote, setNewNote] = useState({ title: '', tags: [] }) + const [currentTag, setCurrentTag] = useState('') const [searchTag, setSearchTag] = useState('') + const [selectedTags, setSelectedTags] = useState([]) + const modalRef = useRef(null) + + useEffect(() => { + localStorage.setItem('notes', JSON.stringify(notes)) + }, [notes]) const addNote = () => { + if (newNote.title.trim() === '') return const newNoteWithId = { ...newNote, id: Date.now().toString(), - tags: newNote.tags.split(',').map(tag => tag.trim()), completed: false } - setNotes([...notes, newNoteWithId]) - setNewNote({ title: '', tags: '' }) + setNotes(prevNotes => [...prevNotes, newNoteWithId]) + setNewNote({ title: '', tags: [] }) + setCurrentTag('') setIsModalOpen(false) } const toggleNoteCompletion = (id) => { - setNotes(notes.map(note => + setNotes(prevNotes => prevNotes.map(note => note.id === id ? { ...note, completed: !note.completed } : note )) } const deleteNote = (id) => { - setNotes(notes.filter(note => note.id !== id)) + setNotes(prevNotes => prevNotes.filter(note => note.id !== id)) + } + + const handleTagInput = (e) => { + const value = e.target.value + if (value.endsWith(' ') && value.trim() !== '') { + setNewNote(prev => ({ + ...prev, + tags: [...prev.tags, currentTag.trim()] + })) + setCurrentTag('') + } else { + setCurrentTag(value) + } + } + + const removeTag = (tagToRemove) => { + setNewNote(prev => ({ + ...prev, + tags: prev.tags.filter(tag => tag !== tagToRemove) + })) } const allTags = Array.from(new Set(notes.flatMap(note => note.tags))) - return ( -
#Add Tags
+ deleteNote(note.id)}>Delete+ {note.tags.map(tag => ( + + #{tag} + + ))} +
+- {note.tags.map(tag => `#${tag}`).join(' ')} -
+#Add Tags
- {note.tags.map(tag => `#${tag}`).join(' ')} -