Temp(?) note refactors

This commit is contained in:
2026-01-23 11:53:57 +00:00
parent b5a84c076d
commit b5ac1df4e3
4 changed files with 41 additions and 26 deletions

View File

@@ -1,8 +1,8 @@
import { createContext, useContext, useState, ReactNode, useEffect } from "react";
import { createContext, useContext, useState, ReactNode } from "react";
import { Note } from "../models/Note"
type NotesStoreType = {
notes: Note[];
notes: Map<string, Note>;
createNote: () => void;
updateNote: (noteId: string, changeset: Partial<Note>) => void;
deleteNote: (noteId: string) => void;
@@ -12,26 +12,28 @@ type NotesStoreType = {
const NotesStore = createContext<NotesStoreType | null>(null);
export function NotesStoreProvider({ children }: { children: ReactNode }) {
const [notes, setNotes] = useState<Note[]>([]);
const [notes, setNotes] = useState<Map<string, Note>>(new Map<string, Note>());
const createNote = () => {
const newNote = new Note();
setNotes([...notes, newNote]);
setNotes(prev => new Map(prev.set(newNote.id, newNote)));
return newNote;
}
const updateNote = (noteId: string, changeset: Partial<Note>) => {
setNotes(notes.map(note =>
note.id === noteId ? new Note({ ...note, ...changeset }) : note
))
const noteToUpdate = notes.get(noteId)
const updatedNote = new Note({ ...noteToUpdate, ...changeset })
setNotes(prev => new Map(prev.set(noteId, updatedNote)));
}
const deleteNote = (noteId: string) => {
setNotes(notes.filter(note => note.id !== noteId))
notes.delete(noteId);
setNotes(prev => new Map(prev))
}
const getNoteById = (noteId: string) => {
return notes.find(note => note.id === noteId);
return notes.get(noteId);
}
return (