-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcategories.js
More file actions
66 lines (62 loc) · 1.42 KB
/
categories.js
File metadata and controls
66 lines (62 loc) · 1.42 KB
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
import { castArray } from 'lodash'
export const state = () => {
return {
categories: []
}
}
export const mutations = {
set (state, categories) {
state.categories = castArray(categories)
},
// [vuex] unknown local mutation type: clear, global type: categories/clear
clear (state) {
state.categories = []
}
}
export const getters = {
all (state) {
return state.categories
}
}
export const actions = {
// Called from nuxtServerInit in index
init ({state, dispatch}) {
if (state.categories.length) {
// do not fetch again
return
}
return dispatch('fetch')
},
// Called from plugins/init-store-subscriptions only once
subscribe ({dispatch}) {
return this.app.$api.service('categories')
.on('created', () => {
dispatch('fetch')
})
},
fetch ({commit}) {
return this.app.$api.service('categories').find({
query: {
'$limit': 200,
'$sort': {
slug: 1
}
}
})
.then((result) => {
commit('set', result.data)
})
.catch(() => {
commit('clear')
})
},
create ({dispatch}, category) {
return this.app.$api.service('categories').create(category)
},
patch ({dispatch}, category) {
return this.app.$api.service('categories').patch(category._id, category)
},
delete ({dispatch}, category) {
return this.app.$api.service('categories').remove(category._id)
}
}