Files: 477a1ff6389236b919641360e1a36179dbcdce1c / volunteers / addNewZines.js
1409 bytesRaw
1 | const smarkt = require('smarkt') |
2 | const _ = require('lodash') |
3 | |
4 | var archive = new DatArchive(window.location) |
5 | |
6 | function store (state, emitter) { |
7 | emitter.on('zine submitted', function (form) { |
8 | // take form, convert it to object, then smarkt string, then write it to file. |
9 | var details = getDetails(form) |
10 | if (details.collections) { // if submission includes a collections entry, convert it to an array |
11 | details.collections = toArray(details.collections) |
12 | } |
13 | var zineJson = JSON.stringify(details, null, 2) |
14 | writeZineTxt(zineJson, emitter) |
15 | }) |
16 | } |
17 | |
18 | function getDetails (form) { |
19 | // take an html form an return it as an object |
20 | var formData = new FormData(form) |
21 | var data = {} |
22 | for (var pair of formData.entries()) { |
23 | data[pair[0]] = pair[1] |
24 | } |
25 | return data |
26 | } |
27 | |
28 | function writeZineTxt (zine, emitter) { |
29 | // take a smarkt text and write it as a .txt in zines folder, |
30 | // then update the library |
31 | zine = JSON.parse(zine) |
32 | var fileName = zine.title.replace(/ /g, '-') + '.txt' |
33 | var pleasantText = smarkt.stringify(zine) |
34 | archive.writeFile(`zines/${fileName}`, pleasantText) |
35 | .then(() => emitter.emit('update library', zine)) // handled by branchManager |
36 | } |
37 | |
38 | function toArray (string) { |
39 | // split a string by commas, remove all white space from each entry, and return the clean array |
40 | var arr = string.split(',') |
41 | arr = _.map(arr, _.trim) |
42 | return arr |
43 | } |
44 | |
45 | module.exports = store |
46 |
Built with git-ssb-web