-
Notifications
You must be signed in to change notification settings - Fork 24
/
menulist.js
99 lines (94 loc) · 2.85 KB
/
menulist.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
90
91
92
93
94
95
96
97
98
99
import { defaultMutations } from 'vuex-easy-access'
import merge from 'merge-anything'
import easyAccessConf from '@config/vuexEasyAccess'
import currencies from '../../config/currencyDefaults'
const defaultPrices = function () {
return Object.keys(currencies)
.reduce((carry, key) => {
carry[key] = 0
return carry
}, {})
}
export function defaultItem () {
return {name: '', icon: null, prices: defaultPrices(), new: true}
}
function testItems () {
return {
'*': {name: '', icon: null, prices: defaultPrices()},
'_cookie': {name: 'Cookie', icon: '🍪', id: '_cookie', prices: merge(defaultPrices(), {usd: 1, jpy: 100})},
'_coffee': {name: 'Coffee', icon: '☕', id: '_coffee', prices: merge(defaultPrices(), {usd: 3, jpy: 300})},
'_latte': {name: 'Latte', icon: '☕', id: '_latte', prices: merge(defaultPrices(), {usd: 5, jpy: 500})},
'_beer': {name: 'Beer', icon: '🍺', id: '_beer', prices: merge(defaultPrices(), {usd: 5, jpy: 500})},
}
}
function initialState () {
return {
items: testItems(),
}
}
export default {
// vuex-easy-firestore config:
firestorePath: 'users/{userId}/menulist',
firestoreRefType: 'collection',
moduleName: 'user/menulist',
statePropName: 'items',
serverChange: {
defaultValues: {prices: defaultPrices()},
// modifiedHook: function (updateStore, doc, id, store) {
// console.log('doc → ', doc)
// updateStore(doc)
// },
// removedHook: function (updateStore, doc, id, store) {
// console.log('doc → ', doc, ' id ', id)
// updateStore(doc)
// },
},
// module:
state: initialState(),
mutations:
{
...defaultMutations(initialState(), easyAccessConf),
resetStateData (state) {
const newState = initialState()
Object.assign(state, newState)
},
replaceMenulist (state, payload) {
state.items = payload
},
updateState (state, payload) {
Object.keys(payload).forEach(key => {
this._vm.$set(state, key, payload[key])
})
},
clearTestItems (state) {
state.items = {}
},
},
actions:
{
addItem ({state, getters, rootState, rootGetters, commit, dispatch}) {
const item = rootState.modals.menulist.adding.item
delete item.new
dispatch('insert', item)
dispatch('modals/toggle', 'menulist.adding', {root: true})
dispatch('modals/menulist.resetNewItem', null, {root: true})
},
setPrice ({state, getters, rootState, rootGetters, commit, dispatch}, {id, val}) {
const curr = rootState.settings.currency
const prices = {}
prices[curr] = val
dispatch('set', {prices, id})
},
},
getters:
{
'items': (state, getters) => {
return Object.keys(state.items)
.filter(id => id !== '*')
.reduce((carry, id) => {
carry[id] = state.items[id]
return carry
}, {})
}
}
}