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.

116 lines
3.0 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 }, payload) {
  38. commit('setLoading', true)
  39. return LabelService.getLabelList(payload.projectId)
  40. .then((response) => {
  41. commit('setLabelList', response.data)
  42. })
  43. .catch((error) => {
  44. alert(error)
  45. })
  46. .finally(() => {
  47. commit('setLoading', false)
  48. })
  49. },
  50. createLabel({ commit }, data) {
  51. return LabelService.addLabel(data.projectId, data)
  52. .then((response) => {
  53. commit('addLabel', response.data)
  54. })
  55. },
  56. updateLabel({ commit }, data) {
  57. LabelService.updateLabel(data.projectId, data.id, data)
  58. .then((response) => {
  59. commit('updateLabel', response.data)
  60. })
  61. .catch((error) => {
  62. alert(error)
  63. })
  64. },
  65. deleteLabel({ commit, state }, projectId) {
  66. for (const label of state.selected) {
  67. LabelService.deleteLabel(projectId, label.id)
  68. .then((response) => {
  69. commit('deleteLabel', label.id)
  70. })
  71. .catch((error) => {
  72. alert(error)
  73. })
  74. }
  75. commit('resetSelected')
  76. },
  77. importLabels({ commit }, payload) {
  78. commit('setLoading', true)
  79. const formData = new FormData()
  80. formData.append('file', payload.file)
  81. const reader = new FileReader()
  82. reader.onload = (e) => {
  83. const labels = JSON.parse(e.target.result)
  84. for (const label of labels) {
  85. LabelService.addLabel(payload.projectId, label)
  86. .then((response) => {
  87. commit('addLabel', response.data)
  88. })
  89. }
  90. }
  91. reader.readAsText(payload.file)
  92. commit('setLoading', false)
  93. },
  94. exportLabels({ commit }, payload) {
  95. commit('setLoading', true)
  96. LabelService.getLabelList(payload.projectId)
  97. .then((response) => {
  98. const url = window.URL.createObjectURL(new Blob([JSON.stringify(response.data)]))
  99. const link = document.createElement('a')
  100. link.href = url
  101. link.setAttribute('download', `project_${payload.projectId}_labels.json`)
  102. document.body.appendChild(link)
  103. link.click()
  104. })
  105. .catch((error) => {
  106. alert(error)
  107. })
  108. .finally(() => {
  109. commit('setLoading', false)
  110. })
  111. }
  112. }