You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

  1. import LabelService from '@/services/label.service'
  2. export const state = () => ({
  3. items: [],
  4. selected: [],
  5. loading: false
  6. })
  7. export const getters = {
  8. isLabelSelected(state) {
  9. return state.selected.length > 0
  10. }
  11. }
  12. export const mutations = {
  13. setLabelList(state, payload) {
  14. state.items = payload
  15. },
  16. addLabel(state, label) {
  17. state.items.unshift(label)
  18. },
  19. deleteLabel(state, labelId) {
  20. state.items = state.items.filter(item => item.id !== labelId)
  21. },
  22. updateSelected(state, selected) {
  23. state.selected = selected
  24. },
  25. updateLabel(state, label) {
  26. const item = state.items.find(item => item.id === label.id)
  27. Object.assign(item, label)
  28. },
  29. resetSelected(state) {
  30. state.selected = []
  31. },
  32. setLoading(state, payload) {
  33. state.loading = payload
  34. }
  35. }
  36. export const actions = {
  37. getLabelList({ commit }, config) {
  38. commit('setLoading', true)
  39. return LabelService.getLabelList()
  40. .then((response) => {
  41. commit('setLabelList', response)
  42. })
  43. .catch((error) => {
  44. alert(error)
  45. })
  46. .finally(() => {
  47. commit('setLoading', false)
  48. })
  49. },
  50. createLabel({ commit }, data) {
  51. LabelService.addLabel(data.projectId, data)
  52. .then((response) => {
  53. commit('addLabel', response)
  54. })
  55. .catch((error) => {
  56. alert(error)
  57. })
  58. },
  59. updateLabel({ commit }, data) {
  60. LabelService.updateLabel(data.projectId, data.id, data)
  61. .then((response) => {
  62. commit('updateLabel', response)
  63. })
  64. .catch((error) => {
  65. alert(error)
  66. })
  67. },
  68. deleteLabel({ commit, state }, projectId) {
  69. for (const label of state.selected) {
  70. LabelService.deleteLabel(projectId, label.id)
  71. .then((response) => {
  72. commit('deleteLabel', label.id)
  73. })
  74. .catch((error) => {
  75. alert(error)
  76. })
  77. }
  78. commit('resetSelected')
  79. }
  80. }