forked from Netflix/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
160 lines (153 loc) · 3.64 KB
/
Copy pathstore.js
File metadata and controls
160 lines (153 loc) · 3.64 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import TaskApi from "@/task/api"
import { getField, updateField } from "vuex-map-fields"
import { debounce } from "lodash"
const getDefaultSelectedState = () => {
return {
resolved_at: null,
resolve_by: null,
creator: null,
assignees: [],
description: null,
status: null,
priority: null,
source: null,
id: null,
loading: false
}
}
const state = {
selected: {
...getDefaultSelectedState()
},
dialogs: {
showCreateEdit: false,
showRemove: false
},
table: {
rows: {
items: [],
total: null
},
options: {
q: "",
page: 1,
itemsPerPage: 10,
sortBy: ["status"],
descending: [false]
},
loading: false
}
}
const getters = {
getField
}
const actions = {
getAll: debounce(({ commit, state }) => {
commit("SET_TABLE_LOADING", true)
return TaskApi.getAll(state.table.options).then(response => {
commit("SET_TABLE_LOADING", false)
commit("SET_TABLE_ROWS", response.data)
})
}, 200),
createEditShow({ commit }, task) {
commit("SET_DIALOG_CREATE_EDIT", true)
if (task) {
commit("SET_SELECTED", task)
}
},
removeShow({ commit }, task) {
commit("SET_DIALOG_DELETE", true)
commit("SET_SELECTED", task)
},
closeCreateEdit({ commit }) {
commit("SET_DIALOG_CREATE_EDIT", false)
commit("RESET_SELECTED")
},
closeRemove({ commit }) {
commit("SET_DIALOG_DELETE", false)
commit("RESET_SELECTED")
},
save({ commit, dispatch }) {
if (!state.selected.id) {
return TaskApi.create(state.selected)
.then(response => {
dispatch("closeCreateEdit")
dispatch("getAll")
commit("app/SET_SNACKBAR", { text: "Task created successfully." }, { root: true })
})
.catch(err => {
commit(
"app/SET_SNACKBAR",
{
text: "Task not created. Reason: " + err.response.data.detail,
color: "red"
},
{ root: true }
)
})
} else {
return TaskApi.update(state.selected.id, state.selected)
.then(response => {
dispatch("closeCreateEdit")
dispatch("getAll")
commit("app/SET_SNACKBAR", { text: "Task updated successfully." }, { root: true })
})
.catch(err => {
commit(
"app/SET_SNACKBAR",
{
text: "Task not updated. Reason: " + err.response.data.detail,
color: "red"
},
{ root: true }
)
})
}
},
remove({ commit, dispatch }) {
return TaskApi.delete(state.selected.id)
.then(function() {
dispatch("closeRemove")
dispatch("getAll")
commit("app/SET_SNACKBAR", { text: "Task deleted successfully." }, { root: true })
})
.catch(err => {
commit(
"app/SET_SNACKBAR",
{
text: "Task not deleted. Reason: " + err.response.data.detail,
color: "red"
},
{ root: true }
)
})
}
}
const mutations = {
updateField,
SET_SELECTED(state, value) {
state.selected = Object.assign(state.selected, value)
},
SET_TABLE_LOADING(state, value) {
state.table.loading = value
},
SET_TABLE_ROWS(state, value) {
state.table.rows = value
},
SET_DIALOG_CREATE_EDIT(state, value) {
state.dialogs.showCreateEdit = value
},
SET_DIALOG_DELETE(state, value) {
state.dialogs.showRemove = value
},
RESET_SELECTED(state) {
state.selected = Object.assign(state.selected, getDefaultSelectedState())
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}