const smarkt = require('smarkt') const _ = require('lodash') var archive = new DatArchive(window.location) function store (state, emitter) { emitter.on('zine submitted', function (zineSubmission) { var details = getDetails(zineSubmission) var zineJson = JSON.stringify(details, null, 2) writeZineTxt(zineJson, emitter) }) } function getDetails (form) { var formData = new FormData(form) var data = {} for (var pair of formData.entries()) { data[pair[0]] = pair[1] } return data } function writeZineTxt (zine, emitter) { zine = JSON.parse(zine) var fileName = zine.title.replace(/ /g, '-') + '.txt' var pleasantText = smarkt.stringify(zine) archive.writeFile(`zines/${fileName}`, pleasantText) .then(() => emitter.emit('update library', zine)) } module.exports = store