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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import fetchJson from 'lib/fetchJson'
import filename from '../helpers/fileName'
import saveFile from 'helpers/saveFile'
export const getNote = async (note, setFetchedNote, setPopup, callback) => {
try {
const {content} = await fetchJson(`/api/note/${note.noteId}`)
setFetchedNote({ ...note, content})
callback()
} catch (err) {
setFetchedNote()
setPopup({
content: 'Could not open note',
time: 2000,
error: true,
})
}
}
export const addNote = async (e, mutateNotes, setAction, setPopup) => {
const content = e.currentTarget.content.value
const title = e.currentTarget.title.value
try {
mutateNotes(
await fetchJson('/api/notes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({title, content}),
})
)
setPopup({
content: 'New note added',
time: 2000,
})
setAction('')
} catch (e) {
setPopup({
content: 'Could not save note',
time: 2000,
error: true,
})
}
}
export const updateNote = async (e, note, mutateNotes, setAction, setPopup) => {
const content = e.currentTarget.content.value
const title = e.currentTarget.title.value
const {_id, noteId} = note
try {
mutateNotes(
await fetchJson('/api/notes', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({_id, title, noteId, content}),
})
)
setPopup({
content: 'Note updated',
time: 2000,
})
setAction('')
} catch (e) {
setPopup({
content: 'Could not update note',
time: 2000,
error: true,
})
}
}
export const removeNote = (e, _id, mutateNotes, setPopup, setAction) => {
e.stopPropagation()
const remove = async () => {
try {
await mutateNotes(
await fetchJson('/api/notes', {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({_id}),
})
)
setPopup({
content: 'Note was removed',
time: 2000,
})
setAction('')
} catch (err) {
setPopup({
content: 'Could not remove note',
time: 2000,
error: true,
})
}
}
setPopup({
content: 'Do you want to remove note?',
yes: { label: 'Remove', action: remove },
no: { label: 'Cancel', action: async () => {} },
error: true,
})
}
export const exportNote = async note => {
const {title} = note
const {content} = note.content
? note
: await fetchJson(`/api/note/${note.noteId}`)
saveFile(content, filename(title), 'text/plain')
}
|