git ssb

0+

punkmonk / double



Commit dcd99d6227eb3ed81cda0e5550b37e159116ed64

initial entries/transactions table

austinfrey committed on 10/28/2018, 8:55:59 PM
Parent: 1739ab2f646d5e903603c2b7e59b6dee0c17fd3e

Files changed

package-lock.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: 353462 bytes
New file size: 353759 bytes
stores/ledger.jsView
@@ -3,48 +3,19 @@
33 const FlumeLog = require('flumelog-memory')
44
55 function ledger (state, emitter) {
66 state.accounts = {}
7- state.transactions = []
7 + state.entries = []
88
99 const db = Flume(FlumeLog())
10- .use('transactions', Reduce(
11- 1,
12- transactionReducer,
13- null,
14- null,
15- state.transactions))
16- .use('account', Reduce(
17- 1,
18- accountReducer,
19- null,
20- null,
21- state.accounts))
10 + .use('entries', Reduce(1, entriesReducer, null, null, state.entries))
11 + .use('accounts', Reduce(1, accountReducer, null, null, state.accounts))
2212
2313 state.db = db
2414
25- function transactionReducer (acc, item) {
26- if (item.type !== 'transaction') return acc
27- acc.push(item)
28- return acc
29- }
30-
31- function accountReducer (acc, item) {
32- if (item.type !== 'accounts') return acc
33- const {msg} = item
34- switch (msg.type) {
35- case 'TOGGLE':
36- acc[msg.name].archived = !acc[msg.name].archived
37- return acc
38- default:
39- acc[msg.name] = msg
40- return acc
41- }
42- }
43-
4415 function onLoad () {
45- emitter.on('account', onEvent)
46- emitter.on('transaction:add', onEvent)
16 + emitter.on('accounts', onEvent)
17 + emitter.on('entries', onEvent)
4718 }
4819
4920 function onEvent (data) {
5021 db.append(data, (err, seq) => {
@@ -55,5 +26,24 @@
5526
5627 emitter.on('DOMContentLoaded', onLoad)
5728 }
5829
30 +function entriesReducer (acc, item) {
31 + if (item.type !== 'entries') return acc
32 + acc.push(item)
33 + return acc
34 +}
35 +
36 +function accountReducer (acc, item) {
37 + if (item.type !== 'accounts') return acc
38 + 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
46 + }
47 +}
48 +
5949 module.exports = ledger
views/main.jsView
@@ -4,21 +4,19 @@
44
55 function view (state, emit) {
66 if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
77
8- const {db} = state
9-
108 const form = html`<form class="pv3">
119 <div class="pb2">
1210 <label>debit account</label><br>
13- <select>
11 + <select id="debit">
1412 <option value="true">--- select debit account ---</option>
1513 ${generateAccountList()}
1614 </select>
1715 </div>
1816 <div class="pb2">
1917 <label>credit account</label><br>
20- <select>
18 + <select id="credit">
2119 <option value="true">--- select credit account ---</option>
2220 ${generateAccountList()}
2321 </select>
2422 </div>
@@ -27,15 +25,40 @@
2725 <input id="description" placeholder="description"/>
2826 </form>`
2927
3028 const submit = html`<div class="pb6">
31- <button onclick=${handleTransaction}>submit</button>
29 + <button onclick=${handleEntry}>submit</button>
3230 </div>`
3331
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>`
55 +
3456 return html`<body class="code ph3 lh-copy">
3557 <main>
3658 <div>double</div>
3759 ${form}
60 + ${entries}
3861 ${submit}
3962 </main>
4063 <div>
4164 <form class="pb3">
@@ -44,40 +67,41 @@
4467 <button onclick=${handleAccountCreate}>create account</button>
4568 </div>
4669 <div>
4770 ${Object.keys(state.accounts).map(key => {
48- if (state.accounts[key].archived) return html`<div class="strike" onclick=${handleAccountArchive}>${state.accounts[key].name}</div>`
49- return html`<div onclick=${handleAccountArchive}>${state.accounts[key].name}</div>`
50- })}
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 + })}
5174 </div>
5275 </body>`
5376
5477 function generateAccountList () {
5578 return Object.keys(state.accounts).map(key => {
5679 if (state.accounts[key].archived) return
57- return html`<option value="true">${state.accounts[key].name}</option>`
80 + const name = state.accounts[key].name
81 + return html`<option value=${name}>${name}</option>`
5882 })
5983 }
6084
61- function handleTransaction () {
85 + function handleEntry () {
6286 const amount = document.getElementById('amount').value
6387 const debit = document.getElementById('debit').value
6488 const credit = document.getElementById('credit').value
6589 const description = document.getElementById('description').value
6690
67- emit('transaction:add', {type: 'transaction', msg: { amount, debit, credit, description }})
91 + emit('entries', { type: 'entries', msg: { amount, debit, credit, description } })
6892 }
6993
7094 function handleAccountCreate () {
7195 const account = document.getElementById('account').value
7296
73- emit('account', { type: 'accounts', msg: { name: account, archived: false }})
97 + emit('accounts', { type: 'accounts', msg: { name: account, archived: false } })
7498 }
7599
76100 function handleAccountArchive () {
77101 const name = this.innerHTML
78102
79- emit('account', { type: 'accounts', msg: { name, type: 'TOGGLE' }})
103 + emit('accounts', { type: 'accounts', msg: { name, type: 'TOGGLE' } })
80104 }
81105 }
82106
83107 module.exports = view

Built with git-ssb-web