forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
37 lines (32 loc) · 899 Bytes
/
notes.js
File metadata and controls
37 lines (32 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import localforage from "localforage";
export async function getNotes() {
let notes = await localforage.getItem("notes");
if (!notes) notes = [];
return notes;
}
export async function createNote({ title, content }) {
let id = Math.random().toString(36).substring(2, 9);
let note = { id, title, content };
let notes = await getNotes();
notes.unshift(note);
await set(notes);
return note;
}
export async function getNote(id) {
let notes = await localforage.getItem("notes");
let note = notes.find((note) => note.id === id);
return note ?? null;
}
export async function deleteNote(id) {
let notes = await localforage.getItem("notes");
let index = notes.findIndex((note) => note.id === id);
if (index > -1) {
notes.splice(index, 1);
await set(notes);
return true;
}
return false;
}
function set(notes) {
return localforage.setItem("notes", notes);
}