volunteers/branchManager.jsView |
---|
1 | 1 … | const smarkt = require('smarkt') |
2 | 2 … | const _ = require('lodash') |
3 | 3 … | const html = require('nanohtml') |
4 | 4 … | |
5 | | - |
6 | 5 … | var archive = new DatArchive(window.location) |
7 | 6 … | |
8 | 7 … | function store (state, emitter) { |
9 | 8 … | state.library = [] |
10 | 9 … | state.info = {} |
11 | 10 … | state.covers = {} |
12 | 11 … | state.identity = '' |
| 12 … | + state.collections = [] |
13 | 13 … | |
14 | 14 … | emitter.on('DOMContentLoaded', function () { |
15 | 15 … | setIdentity() |
16 | 16 … | archive.readdir('zines') |
17 | 17 … | .then(zines => mapToState(zines)) |
| 18 … | + console.log({state}) |
18 | 19 … | }) |
19 | 20 … | |
20 | 21 … | emitter.on('update library', function (zine) { |
21 | 22 … | addToLibrary(zine) |
30 | 31 … | var textJson = smarkt.parse(rawText) |
31 | 32 … | addToLibrary(textJson) |
32 | 33 … | addZineInfo(textJson.url) |
33 | 34 … | addZineCover(textJson.url) |
| 35 … | + gatherCollections(textJson.collections) |
| 36 … | + cleanCollections(state.collections) |
34 | 37 … | }) |
35 | 38 … | }) |
36 | 39 … | } |
37 | 40 … | |
39 | 42 … | state.library = [...state.library, text] |
40 | 43 … | emitter.emit('pushState') |
41 | 44 … | } |
42 | 45 … | |
| 46 … | + function gatherCollections (collections) { |
| 47 … | + if (collections) { |
| 48 … | + state.collections = [...state.collections, collections] |
| 49 … | + emitter.emit('pushState') |
| 50 … | + } |
| 51 … | + } |
| 52 … | + |
| 53 … | + function cleanCollections (collections) { |
| 54 … | + collections = _.flatten(collections) |
| 55 … | + collections = collections.map(collection => _.toLower(collection)) |
| 56 … | + state.collections = _.uniq(collections) |
| 57 … | + emitter.emit('pushState') |
| 58 … | + } |
| 59 … | + |
43 | 60 … | function addZineInfo (datUrl) { |
44 | 61 … | var zine = new DatArchive(datUrl) |
45 | 62 … | zine.readFile('distro/info.txt') |
46 | 63 … | .then(text => { |
47 | | - mapInfoToState(text, zine['url']) |
| 64 … | + mapInfoToState(text, zine['url']) |
48 | 65 … | }) |
49 | 66 … | } |
50 | 67 … | |
51 | 68 … | function addZineCover (datUrl) { |
66 | 83 … | var cover = files.find(file => sansExtension(file) === 'cover') |
67 | 84 … | state.covers[datUrl] = `${datUrl}/distro/${cover}` |
68 | 85 … | emitter.emit('pushState') |
69 | 86 … | } |
70 | | - |
| 87 … | + |
71 | 88 … | async function setIdentity () { |
72 | 89 … | var info = await archive.getInfo() |
73 | 90 … | if (info.isOwner) { |
74 | 91 … | state.identity = 'librarian' |