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.

102 lines
2.2 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. headers() {
  12. return [
  13. {
  14. text: 'Name',
  15. align: 'left',
  16. value: 'text'
  17. },
  18. {
  19. text: 'Shortkey',
  20. value: 'suffix_key'
  21. },
  22. {
  23. text: 'Color',
  24. sortable: false,
  25. value: 'background_color'
  26. }
  27. ]
  28. }
  29. }
  30. export const mutations = {
  31. setLabelList(state, payload) {
  32. state.items = payload
  33. },
  34. addLabel(state, label) {
  35. state.items.unshift(label)
  36. },
  37. deleteLabel(state, labelId) {
  38. state.items = state.items.filter(item => item.id !== labelId)
  39. },
  40. updateSelected(state, selected) {
  41. state.selected = selected
  42. },
  43. updateLabel(state, label) {
  44. const item = state.items.find(item => item.id === label.id)
  45. Object.assign(item, label)
  46. },
  47. resetSelected(state) {
  48. state.selected = []
  49. },
  50. setLoading(state, payload) {
  51. state.loading = payload
  52. }
  53. }
  54. export const actions = {
  55. getLabelList({ commit }, config) {
  56. commit('setLoading', true)
  57. return LabelService.getLabelList()
  58. .then((response) => {
  59. commit('setLabelList', response)
  60. })
  61. .catch((error) => {
  62. alert(error)
  63. })
  64. .finally(() => {
  65. commit('setLoading', false)
  66. })
  67. },
  68. createLabel({ commit }, data) {
  69. LabelService.addLabel(data.projectId, data)
  70. .then((response) => {
  71. commit('addLabel', response)
  72. })
  73. .catch((error) => {
  74. alert(error)
  75. })
  76. },
  77. updateLabel({ commit }, data) {
  78. LabelService.updateLabel(data.projectId, data.id, data)
  79. .then((response) => {
  80. commit('updateLabel', response)
  81. })
  82. .catch((error) => {
  83. alert(error)
  84. })
  85. },
  86. deleteLabel({ commit, state }, projectId) {
  87. for (const label of state.selected) {
  88. LabelService.deleteLabel(projectId, label.id)
  89. .then((response) => {
  90. commit('deleteLabel', label.id)
  91. })
  92. .catch((error) => {
  93. alert(error)
  94. })
  95. }
  96. commit('resetSelected')
  97. }
  98. }