import fetchLabelsUrl removeLabelUrl saveLabelUrl from planner src con

  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { fetchLabelsUrl, removeLabelUrl, saveLabelUrl } from '../../../../planner/src/config/APIUtils'
import { LABEL_COLOR_MISSING, LABEL_NAME_MISSING, UNKNOWN_ERROR } from '../../../../common/constants/messages'
import proxy from '../../../../common/lib/http-common'
let defaultPublishLabels = {
fetchLabels: false,
items: [],
current_label: [],
loaders: {
visible: false,
save: false,
retrieve: false
}
}
export default{
state: {
labels: {
fetchLabels: false,
items: [],
current_label: [],
loaders: {
visible: false,
save: false,
retrieve: false
}
}
},
actions: {
/**
* Get the labels based on a workspace id
* @param commit
* @param getters
* @param state
*/
fetchLabels ({ commit, getters, state }) {
if (getters.getWorkspaces.activeWorkspace._id) {
commit('SET_RETRIEVE_LABEL', true)
proxy.post(fetchLabelsUrl, { workspace_id: getters.getWorkspaces.activeWorkspace._id }).then(
resp => {
if (resp.data.status) {
commit('SET_LABELS', resp.data.labels)
commit('SET_RETRIEVE_LABEL', true)
} else {
commit('SET_LABELS', [])
commit('SET_RETRIEVE_LABEL', false)
}
console.log(resp)
}
).catch()
}
},
/**
* Save a new label to the account
* @param commit
* @param state
* @param dispatch
* @param getters
*/
saveLabel ({ commit, state, dispatch, getters }) {
if (!getters.getNewLabel.name) {
dispatch('toastNotification', { message: LABEL_NAME_MISSING, type: 'error' })
return
}
if (!getters.getNewLabel.color) {
dispatch('toastNotification', { message: LABEL_COLOR_MISSING, type: 'error' })
return
}
// prepare payload
let payload = {
workspace_id: getters.getWorkspaces.activeWorkspace._id,
name: getters.getNewLabel.name,
color: getters.getNewLabel.color
}
// if it is existing item
if (getters.getNewLabel._id) {
payload['label'] = getters.getNewLabel._id
}
commit('SET_SAVE_LABEL_LOADER', true)
proxy.post(saveLabelUrl, payload).then(resp => {
if (resp.data.status) {
commit('SET_SAVE_LABEL_LOADER', false)
document.getElementById('labelcancel').click()
// if the label already exists, update by index
if (payload.label) {
commit('UPDATE_LABEL_BY_INDEX', resp.data.label)
} else {
commit('ADD_LABEL', resp.data.label)
}
}
}).catch()
},
/**
* Delete a label.
* @param commit
* @param getters
* @param dipsatch
* @param label
*/
deleteLabel ({ commit, getters, dipsatch }, label) {
proxy.post(removeLabelUrl, {
workspace_id: getters.getWorkspaces.activeWorkspace._id,
label: label._id
}).then(
resp => {
if (resp.data.status) {
getters.getLabels.splice(getters.getLabels.indexOf(label), 1)
}
}
)
},
/** Reset labels when someone changes the workspace
*
* @param commit
*/
resetLabels ({ commit }) {
commit('resetLabels')
}
},
mutations: {
SET_RETRIEVE_LABEL (state, status) {
state.labels.loaders.retrieve = status
},
SET_LABELS (state, labels) {
state.labels.items = labels
},
ADD_LABEL (state, label) {
state.labels.items.unshift(label)
},
UPDATE_LABEL_BY_INDEX (state, label) {
if (state.labels.items) {
let index = state.labels.items.findIndex(item => label._id === item._id)
console.log('Existing label index', index)
state.labels.items[index] = label
}
},
SET_NEW_LABEL (state, label) {
if (label) {
state.labels.current_label = label
} else state.labels.current_label = {}
},
RESET_LABELS_STATE (state) {
state.labels = JSON.parse(JSON.stringify(defaultPublishLabels))
},
resetLabels (state) {
state.labels.fetchLabels = false
state.labels.items = []
},
// loaders
SET_SAVE_LABEL_LOADER (state, value) {
state.labels.loaders.save = value
}
},
getters: {
getLabels: state => {
return state.labels.items
},
getNewLabel: state => {
return state.labels.current_label
},
getSaveLabelLoader: state => {
return state.labels.loaders.save
}
}
}