git ssb

0+

Zach! / dat-zine-library



Tree: 66d7bd513270b804eb7c6a61a46eb1103afd519a

Files: 66d7bd513270b804eb7c6a61a46eb1103afd519a / volunteers / jobs.js

1519 bytesRaw
1const smarkt = require('smarkt')
2const _ = require('lodash')
3
4var archive = new DatArchive(window.location)
5
6function store (state, emitter) {
7 state.library = []
8 state.info = {}
9 state.covers = {}
10 emitter.on('DOMContentLoaded', function () {
11 archive.readdir('zines').then(zines => mapToState(zines))
12 })
13
14 function mapToState (zines) {
15 zines.map(zine => {
16 archive.readFile(`zines/${zine}`)
17 .then(rawText => {
18 var textJson = smarkt.parse(rawText)
19 addToLibrary(textJson)
20 addZineInfo(textJson.url)
21 addZineCover(textJson.url)
22 })
23 })
24 }
25
26 function addToLibrary (text) {
27 state.library = [...state.library, text]
28 emitter.emit('pushState')
29 }
30
31 function addZineInfo (datUrl) {
32 var zine = new DatArchive(datUrl)
33 zine.readFile('distro/info.txt')
34 .then(text => {
35 mapInfoToState(text, zine['url'])
36 })
37 }
38
39 function addZineCover (datUrl) {
40 var zine = new DatArchive(datUrl)
41 zine.readdir('distro')
42 .then(files => {
43 mapCoverToState(files, zine['url'])
44 })
45 }
46
47 function mapInfoToState (info, datUrl) {
48 var infoJson = smarkt.parse(info)
49 state.info[datUrl] = infoJson
50 emitter.emit('pushState')
51 }
52
53 function mapCoverToState (files, datUrl) {
54 var cover = files.find(file => sansExtension(file) === 'cover')
55 state.covers[datUrl] = `${datUrl}/distro/${cover}`
56 console.log(state.covers)
57 emitter.emit('pushState')
58 }
59}
60
61function sansExtension (file) {
62 return file.split('.')[0]
63}
64
65module.exports = store
66

Built with git-ssb-web