From 41029e7392477497fe2ab80cbfa77c08654102ce Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 3 Sep 2019 15:06:52 +0900 Subject: [PATCH] Add label service and store --- frontend/services/label.service.js | 32 +++++++++++++ frontend/store/labels.js | 76 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 frontend/services/label.service.js create mode 100644 frontend/store/labels.js diff --git a/frontend/services/label.service.js b/frontend/services/label.service.js new file mode 100644 index 00000000..c04c8d55 --- /dev/null +++ b/frontend/services/label.service.js @@ -0,0 +1,32 @@ +import ApiService from '@/services/api.service' + +class MemberService { + constructor() { + this.request = new ApiService() + } + + getMemberList(projectId) { + return this.request.get(`/projects/${projectId}/users`) + } + + addMember(projectId, userId, role) { + const data = { + id: userId, + role + } + return this.request.post(`/projects/${projectId}/users`, data) + } + + deleteMember(projectId, userId) { + return this.request.delete(`/projects/${projectId}/users/${userId}`) + } + + updateMemberRole(projectId, userId, role) { + const data = { + role + } + return this.request.patch(`/projects/${projectId}/users/${userId}`, data) + } +} + +export default new MemberService() diff --git a/frontend/store/labels.js b/frontend/store/labels.js new file mode 100644 index 00000000..cd6fd558 --- /dev/null +++ b/frontend/store/labels.js @@ -0,0 +1,76 @@ +import LabelService from '@/services/label.service' + +export const state = () => ({ + items: [], + selected: [] +}) + +export const getters = { + isLabelSelected(state) { + return state.selected.length > 0 + } +} + +export const mutations = { + setLabelList(state, payload) { + state.items = payload + }, + addLabel(state, label) { + state.items.unshift(label) + }, + deleteLabel(state, labelId) { + state.items = state.items.filter(item => item.id !== labelId) + }, + updateSelected(state, selected) { + state.selected = selected + }, + updateLabel(state, label) { + const item = state.items.find(item => item.id === label.id) + Object.assign(item, label) + }, + resetSelected(state) { + state.selected = [] + } +} + +export const actions = { + getLabelList(context, config) { + return LabelService.getLabelList() + .then((response) => { + context.commit('setLabelList', response) + }) + .catch((error) => { + alert(error) + }) + }, + addMember({ commit }, data) { + LabelService.addLabel(data.projectId, data.labelId, data) + .then((response) => { + commit('addLabel', response) + }) + .catch((error) => { + alert(error) + }) + }, + updateLabel({ commit }, data) { + LabelService.updateLabel(data.projectId, data.labelId, data) + .then((response) => { + commit('updateLabel', response) + }) + .catch((error) => { + alert(error) + }) + }, + removeLabel({ commit, state }, projectId) { + for (const label of state.selected) { + LabelService.deleteLabel(projectId, label.id) + .then((response) => { + commit('deleteLabel', label.id) + }) + .catch((error) => { + alert(error) + }) + } + commit('resetSelected') + } +}