aboutsummaryrefslogtreecommitdiffstats
path: root/apps/Notes/helpers/export.js
blob: cdb6ae3cc5fd20bd9cdcf0257020ebcfbdcda4b2 (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
import fetchJson from 'helpers/fetchJson'
import JSZip from 'jszip'
import saveFile from 'helpers/saveFile'
import filename from '../helpers/fileName'

export const handleSelect = (id, ids, setIds) => {
  ids.includes(id)
    ? setIds(ids.filter(i => i !== id))
    : setIds([...ids, id])
}

export const handleSelectAll = (e, notes, setIds) => {
  if (e.target.checked) {
    setIds(notes.map(n => n.noteId))
  } else {
    setIds([])
  }
}

export const handleExport = (e, ids, notes) => {
  e.preventDefault()
  const zip = new JSZip()

  Promise.all(ids.map(async id => {
    const title = notes.find(n => n.noteId === id).title
    const { content } = await fetchJson(`/api/notes/${id}`)
    zip.folder('notes').file(filename(title), content, { binary: true })
  })).then(() => {
    zip.generateAsync({ type: 'blob' })
      .then(c => saveFile(c, 'notes.zip', 'application/zip'))
  })
}