Files: ca718f616a051e93adeb22faa3fc06646f667973 / graph / app.js
1619 bytesRaw
1 | const xhr = require('xhr') |
2 | const { Domain } = require('inux') |
3 | const async = require('pull-async') |
4 | const html = require('inu/html') |
5 | |
6 | const { SET, SET_FOCUS, FETCH, FOCUS, set, setFocus, fetch } = require('./actions') |
7 | const GraphView = require('./view') |
8 | const { fetchOne: fetchProfile } = require('../profiles/actions') |
9 | const ProfileView = require('../profiles/view') |
10 | |
11 | module.exports = GraphApp |
12 | |
13 | function GraphApp (config) { |
14 | const graphView = GraphView(config) |
15 | const profileView = ProfileView(config) |
16 | |
17 | return Domain({ |
18 | name: 'graph', |
19 | init: () => ({ |
20 | model: { |
21 | nodes: [], |
22 | links: [] |
23 | }, |
24 | effect: fetch() |
25 | }), |
26 | update: { |
27 | [SET]: (model, graph) => ({ model: graph }), |
28 | [SET_FOCUS]: (model, focus) => ({ |
29 | model: assign({}, model, { focus }) |
30 | }) |
31 | }, |
32 | run: { |
33 | [FETCH]: () => { |
34 | return async(cb => { |
35 | xhr({ |
36 | url: '/api/graph', |
37 | json: true |
38 | }, (err, resp, { body } = {}) => { |
39 | if (err) return cb(err) |
40 | cb(null, set(body)) |
41 | }) |
42 | }) |
43 | }, |
44 | [FOCUS]: (id) => { |
45 | return pull.values([ |
46 | setFocus(id), |
47 | fetchProfile(id) |
48 | ]) |
49 | } |
50 | }, |
51 | routes: [ |
52 | ['/', (params, model, dispatch) => { |
53 | const { graph, profiles } = model |
54 | const { focus } = graph |
55 | const focusedProfile = focus ? profiles[focus] : undefined |
56 | |
57 | return html` |
58 | <div class='main'> |
59 | ${profileView(focusedProfile, dispatch)} |
60 | ${graphView(graph, dispatch)} |
61 | </div> |
62 | ` |
63 | }] |
64 | ] |
65 | }) |
66 | } |
67 |
Built with git-ssb-web