git ssb

0+

punkmonk / double



Commit 7dc6be618d7c0a5cf0b4b2c20a9b317f339d986d

added account balances

austinfrey committed on 10/29/2018, 8:48:14 PM
Parent: 144d08f547a26de56fd10b542128be8b56af51b3

Files changed

package-lock.jsonchanged
package.jsonchanged
stores/ledger.jschanged
views/main.jschanged
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 353759 bytes
New file size: 384353 bytes
package.jsonView
@@ -15,8 +15,9 @@
1515 "choo-service-worker": "^2.4.0",
1616 "flumedb": "^1.0.0",
1717 "flumelog-offset": "^3.3.2",
1818 "flumeview-reduce": "^1.3.14",
19 + "hyperscript": "^2.0.2",
1920 "pull-paramap": "^1.2.2",
2021 "pull-stream": "^3.6.9",
2122 "sheetify": "^7.3.3",
2223 "tachyons": "^4.11.1"
@@ -25,8 +26,9 @@
2526 "bankai": "^9.15.0",
2627 "choo-devtools": "^2.5.1",
2728 "choo-scaffold": "^1.2.0",
2829 "dependency-check": "^3.2.1",
30 + "electron": "^3.0.6",
2931 "flumelog-memory": "^0.1.3",
3032 "standard": "^12.0.1"
3133 }
3234 }
stores/ledger.jsView
@@ -8,8 +8,9 @@
88
99 const db = Flume(FlumeLog())
1010 .use('entries', Reduce(1, entriesReducer, null, null, state.entries))
1111 .use('accounts', Reduce(1, accountReducer, null, null, state.accounts))
12 + .use('balances', Reduce(1, accountBalanceReducer, null, null, state.accounts))
1213
1314 state.db = db
1415
1516 function onLoad () {
@@ -26,24 +27,33 @@
2627
2728 emitter.on('DOMContentLoaded', onLoad)
2829 }
2930
31 +function accountBalanceReducer (acc, item) {
32 + if (item.type !== 'entries') return acc
33 + const {msg} = item
34 +
35 + acc[msg.debit].debit += msg.amount
36 + acc[msg.credit].credit += msg.amount
37 + return acc
38 +}
39 +
3040 function entriesReducer (acc, item) {
3141 if (item.type !== 'entries') return acc
3242 acc.push(item)
3343 return acc
3444 }
3545
36-function accountReducer (acc, item) {
46 +function accountReducer (acc, item) { // TODO account for account creation of existing account
3747 if (item.type !== 'accounts') return acc
3848 const { msg } = item
39- switch (msg.type) {
40- case 'TOGGLE':
41- acc[msg.name].archived = !acc[msg.name].archived
42- return acc
43- default:
44- acc[msg.name] = msg
45- return acc
49 +
50 + if (!acc[msg.name]) {
51 + acc[msg.name] = msg
52 + return acc
4653 }
54 +
55 + Object.assign(acc[msg.name], msg)
56 + return acc
4757 }
4858
4959 module.exports = ledger
views/main.jsView
@@ -1,107 +1,111 @@
1-var html = require('choo/html')
1 +const h = require('hyperscript')
2 +const TITLE = 'double - main'
23
3-var TITLE = 'double - main'
4-
54 function view (state, emit) {
65 if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
76
8- const form = html`<form class="pv3">
9- <div class="pb2">
10- <label>debit account</label><br>
11- <select id="debit">
12- <option value="true">--- select debit account ---</option>
13- ${generateAccountList()}
14- </select>
15- </div>
16- <div class="pb2">
17- <label>credit account</label><br>
18- <select id="credit">
19- <option value="true">--- select credit account ---</option>
20- ${generateAccountList()}
21- </select>
22- </div>
23- <div>
24- <input class="mb2" id="amount" placeholder="amount"/><br>
25- <input id="description" placeholder="description"/>
26- </form>`
7 + const form = h('form.pv3', [
8 + h('div.pb2', [
9 + h('label', 'debit account'),
10 + h('div', h('select#debit', [
11 + h('option', {value: 'true'},'select'),
12 + generateAccountList()
13 + ]))
14 + ]),
15 + h('div.pb2', [
16 + h('label', 'credit account'),
17 + h('div', h('select#credit', [
18 + h('option', {value: 'true'},'select'),
19 + generateAccountList()
20 + ]))
21 + ]),
22 + h('div', h('input#amount.mb2', {placeholder: 'amount'})),
23 + h('div', h('input#description', {placeholder: 'description'}))
24 + ])
2725
28- const submit = html`<div class="pb6">
29- <button onclick=${handleEntry}>submit</button>
30- </div>`
26 + const submit = h('div.pb3', h('button', {onclick: handleEntry}, 'submit'))
3127
32- const entries = html`<div class="pb3">
33- <table>
34- <thead><tr>
35- <td>account</td>
36- <td>debit</td>
37- <td>credit</td>
38- </tr></thead>
39- <tbody>
40- ${state.entries.map(entry => ['debit', 'credit'].map(i => i === 'debit'
41- ? html`<tr>
42- <td>${entry.msg.debit}</td>
43- <td>${entry.msg.amount}</td>
44- <td>-</td>
45- </tr>`
46- : html`<tr>
47- <td>${entry.msg.credit}</td>
48- <td>-</td>
49- <td>${entry.msg.amount}</td>
50- </tr>`
51- ))}
52- </tbody>
53- </table>
54- </div>`
28 + const entries = h('div.pb6', [
29 + h('table', [
30 + h('thead', h('tr', [
31 + h('td.pr2', 'account'),
32 + h('td.pr2', 'debit'),
33 + h('td.pr2', 'credit'),
34 + h('td.pr2', 'description')
35 + ])),
36 + h('tbody', state.entries.map(entry => ['debit', 'credit'].map(i => {
37 + return i === 'debit'
38 + ? h('tr', [
39 + h('td', entry.msg.debit),
40 + h('td', entry.msg.amount),
41 + h('td', '-'),
42 + h('td', entry.msg.description)
43 + ])
44 + : h('tr', [
45 + h('td', entry.msg.credit),
46 + h('td', '-'),
47 + h('td', entry.msg.amount),
48 + h('td', entry.msg.description)
49 + ])
50 + })))
51 + ])
52 + ])
5553
56- return html`<body class="code ph3 lh-copy">
57- <main>
58- <div>double</div>
59- ${form}
60- ${entries}
61- ${submit}
62- </main>
63- <div>
64- <form class="pb3">
65- <input id="account"/>
66- </form>
67- <button onclick=${handleAccountCreate}>create account</button>
68- </div>
69- <div>
70- ${Object.keys(state.accounts).map(key => {
71- if (state.accounts[key].archived) return html`<div class="strike" onclick=${handleAccountArchive}>${state.accounts[key].name}</div>`
72- return html`<div onclick=${handleAccountArchive}>${state.accounts[key].name}</div>`
73- })}
74- </div>
75- </body>`
54 + return h('body.code.ph3.lh-copy', [
55 + h('main', [
56 + h('div', 'double'),
57 + form,
58 + submit,
59 + entries
60 + ]),
61 + h('div', [
62 + h('form.pb3', h('input#account')),
63 + h('button', {onclick: handleAccountCreate}, 'create account')
64 + ]),
65 + h('div.pv2', 'account balances'),
66 + Object.keys(state.accounts).map(key => {
67 + if (state.accounts[key].archived) {
68 + return h('div', [
69 + h('div.dib.strike', {onclick: handleAccountArchive}, state.accounts[key].name),
70 + h('div.dib.pr2.strike', state.accounts[key].debit - state.accounts[key].credit)
71 + ])
72 + }
73 + return h('div', [
74 + h('div.dib.pr2', {onclick: handleAccountArchive}, state.accounts[key].name),
75 + h('div.dib', state.accounts[key].debit - state.accounts[key].credit)
76 + ])
77 + })
78 + ])
7679
80 +
7781 function generateAccountList () {
7882 return Object.keys(state.accounts).map(key => {
7983 if (state.accounts[key].archived) return
8084 const name = state.accounts[key].name
81- return html`<option value=${name}>${name}</option>`
85 + return h('option', {value: name}, name)
8286 })
8387 }
8488
8589 function handleEntry () {
86- const amount = document.getElementById('amount').value
90 + const amount = parseFloat(document.getElementById('amount').value)
8791 const debit = document.getElementById('debit').value
8892 const credit = document.getElementById('credit').value
8993 const description = document.getElementById('description').value
9094
9195 emit('entries', { type: 'entries', msg: { amount, debit, credit, description } })
9296 }
9397
9498 function handleAccountCreate () {
95- const account = document.getElementById('account').value
99 + const name = document.getElementById('account').value
96100
97- emit('accounts', { type: 'accounts', msg: { name: account, archived: false } })
101 + emit('accounts', { type: 'accounts', msg: { name, debit: 0, credit: 0, archived: false } })
98102 }
99103
100104 function handleAccountArchive () {
101105 const name = this.innerHTML
102106
103- emit('accounts', { type: 'accounts', msg: { name, type: 'TOGGLE' } })
107 + emit('accounts', { type: 'accounts', msg: { name, archived: true } })
104108 }
105109 }
106110
107111 module.exports = view

Built with git-ssb-web