git ssb

0+

Zach! / dat-zine-library



Tree: 477a1ff6389236b919641360e1a36179dbcdce1c

Files: 477a1ff6389236b919641360e1a36179dbcdce1c / volunteers / addNewZines.js

1409 bytesRaw
1const smarkt = require('smarkt')
2const _ = require('lodash')
3
4var archive = new DatArchive(window.location)
5
6function 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
18function 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
28function 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
38function 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
45module.exports = store
46

Built with git-ssb-web