-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (80 loc) · 2.09 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { observable /*computed*/ } from 'mobx'
import Menu from './components/Menu'
import DisplayOrder from './components/DisplayOrder'
import AddToOrder from './components/AddToOrder'
import AddToMenu from './components/AddToMenu'
import Ticker from './components/Ticker'
// * observable object
// ? useful when you want everything observable
export let appState = observable({
items: [
{
name: 'Trout',
price: 10,
},
{
name: 'Salmon',
price: 10,
},
],
order: [
/* {name, quantity} */
],
get total() {
return this.order.reduce((runningTotal, orderItem) => {
const item = this.items.find((item) => item.name === orderItem.name)
const subTotal = item.price * orderItem.quantity
return runningTotal + subTotal
}, 0)
},
})
// * class with observable properties
// ? useful if you want to mix observable and non-observable properties?
// class AppState {
// constructor() {
// this.items = [
// {
// name: 'Trout',
// price: 10,
// },
// {
// name: 'Salmon',
// price: 10,
// },
// ]
// this.order = [
// // {name, quantity}
// ]
// }
// @observable items
// @observable order
// @computed get total() {
// return this.order.reduce((runningTotal, orderItem) => {
// const item = this.items.find((item) => item.name === orderItem.name)
// const subTotal = item.price * orderItem.quantity
// return runningTotal + subTotal
// }, 0)
// }
// }
// export let appState = new AppState()
// mount
const app = document.getElementById('app')
app.appendChild(Menu())
app.appendChild(AddToMenu())
app.appendChild(DisplayOrder())
app.appendChild(AddToOrder())
const tickers = document.getElementById('tickers')
appState.items.forEach((item) => tickers.appendChild(Ticker(item)))
// helpers
export function money(float) {
return float.toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
})
}
// price fluctuation
setInterval(() => {
appState.items.forEach((item) => {
item.price += (Math.random() - 0.5) / 2
})
}, 50)