aboutsummaryrefslogtreecommitdiffstats
path: root/apps/Notes/helpers/noteActions.js
blob: d93422939b056991c0693025370543ac0cd88648 (plain) (blame)
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
import fetchJson from 'helpers/fetchJson'
import saveFile from 'helpers/saveFile'
import filename from '../helpers/fileName'

export const getNote = async (note, setFetchedNote, t, setPopup, callback) => {
  try {
    const { content } = await fetchJson(`/api/notes/${note.noteId}`)
    setFetchedNote({ ...note, content })
    callback()
  } catch (err) {
    setFetchedNote()
    setPopup({
      content: t('notes_open_error'),
      time: 2000,
      error: true
    })
  }
}

export const addNote = async (e, mutateNotes, setAction, t, 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: t('notes_added'),
      time: 2000
    })
    setAction('')
  } catch (e) {
    setPopup({
      content: t('notes_added_error'),
      time: 2000,
      error: true
    })
  }
}

export const updateNote = async (e, note, mutateNotes, setAction, t, setPopup) => {
  const content = e.currentTarget.content.value
  const title = e.currentTarget.title.value
  const { _id, noteId } = note

  try {
    mutateNotes(
      await fetchJson(`/api/notes/${_id}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ title, noteId, content })
      })
    )
    setPopup({
      content: t('notes_updated'),
      time: 2000
    })
    setAction('')
  } catch (e) {
    setPopup({
      content: t('notes_updated_error'),
      time: 2000,
      error: true
    })
  }
}

export const removeNote = (e, _id, mutateNotes, t, setPopup, setAction) => {
  e.stopPropagation()

  const remove = async () => {
    try {
      await mutateNotes(
        await fetchJson(`/api/notes/${_id}`, {
          method: 'DELETE'
        })
      )
      setPopup({
        content: t('notes_removed'),
        time: 2000
      })
      setAction('')
    } catch (err) {
      setPopup({
        content: t('notes_removed_error'),
        time: 2000,
        error: true
      })
    }
  }

  setPopup({
    content: t('notes_remove_confirm'),
    yes: { label: t('remove'), action: remove },
    no: { label: t('cancel'), action: async () => {} },
    error: true
  })
}

export const exportNote = async note => {
  const { title } = note
  const { content } = note.content
    ? note
    : await fetchJson(`/api/notes/${note.noteId}`)

  saveFile(content, filename(title), 'text/plain')
}