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.

252 lines
7.1 KiB

  1. import DocumentService from '@/services/document.service'
  2. import AnnotationService from '@/services/annotation.service'
  3. export const state = () => ({
  4. items: [],
  5. selected: [],
  6. loading: false,
  7. current: 0,
  8. total: 0,
  9. searchOptions: {
  10. limit: 10,
  11. offset: 0,
  12. q: '',
  13. isChecked: '',
  14. filterName: ''
  15. }
  16. })
  17. export const getters = {
  18. isDocumentSelected(state) {
  19. return state.selected.length > 0
  20. },
  21. approved(state) {
  22. if (state.items[state.current]) {
  23. return state.items[state.current].annotation_approver !== null
  24. } else {
  25. return false
  26. }
  27. },
  28. currentDoc(state) {
  29. return state.items[state.current]
  30. }
  31. }
  32. export const mutations = {
  33. setCurrent(state, payload) {
  34. state.current = payload
  35. },
  36. setDocumentList(state, payload) {
  37. state.items = payload
  38. },
  39. addDocument(state, document) {
  40. state.items.unshift(document)
  41. },
  42. deleteDocument(state, documentId) {
  43. state.items = state.items.filter(item => item.id !== documentId)
  44. },
  45. updateSelected(state, selected) {
  46. state.selected = selected
  47. },
  48. updateDocument(state, document) {
  49. const item = state.items.find(item => item.id === document.id)
  50. Object.assign(item, document)
  51. },
  52. resetSelected(state) {
  53. state.selected = []
  54. },
  55. setLoading(state, payload) {
  56. state.loading = payload
  57. },
  58. setTotalItems(state, payload) {
  59. state.total = payload
  60. },
  61. addAnnotation(state, payload) {
  62. state.items[state.current].annotations.push(payload)
  63. },
  64. setAnnotations(state, payload) {
  65. state.items[state.current].annotations = payload
  66. },
  67. deleteAnnotation(state, annotationId) {
  68. state.items[state.current].annotations = state.items[state.current].annotations.filter(item => item.id !== annotationId)
  69. },
  70. clearAnnotations(state) {
  71. state.items[state.current].annotations = []
  72. },
  73. updateAnnotation(state, payload) {
  74. const item = state.items[state.current].annotations.find(item => item.id === payload.id)
  75. Object.assign(item, payload)
  76. },
  77. updateSearchOptions(state, payload) {
  78. state.searchOptions = Object.assign(state.searchOptions, payload)
  79. },
  80. initSearchOptions(state) {
  81. state.searchOptions = {
  82. limit: 10,
  83. offset: 0,
  84. q: '',
  85. isChecked: '',
  86. filterName: ''
  87. }
  88. },
  89. setUserId(state, payload) {
  90. state.userId = payload.id
  91. }
  92. }
  93. export const actions = {
  94. getDocumentList({ commit, state }, payload) {
  95. commit('setLoading', true)
  96. return DocumentService.getDocumentList(payload)
  97. .then((response) => {
  98. commit('setDocumentList', response.data.results)
  99. commit('setTotalItems', response.data.count)
  100. })
  101. .catch((error) => {
  102. alert(error)
  103. })
  104. .finally(() => {
  105. commit('setLoading', false)
  106. })
  107. },
  108. uploadDocument({ commit, dispatch }, data) {
  109. commit('setLoading', true)
  110. const formData = new FormData()
  111. formData.append('file', data.file)
  112. formData.append('format', data.format)
  113. const config = {
  114. headers: {
  115. 'Content-Type': 'multipart/form-data'
  116. }
  117. }
  118. return DocumentService.uploadFile(data.projectId, formData, config)
  119. .then((response) => {
  120. dispatch('getDocumentList', data)
  121. })
  122. .finally(() => {
  123. commit('setLoading', false)
  124. })
  125. },
  126. exportDocument({ commit }, data) {
  127. commit('setLoading', true)
  128. DocumentService.exportFile(data.projectId, data.format, data.onlyApproved)
  129. .then((response) => {
  130. const url = window.URL.createObjectURL(new Blob([response.data]))
  131. const link = document.createElement('a')
  132. link.href = url
  133. link.setAttribute('download', data.fileName + '.' + data.suffix)
  134. document.body.appendChild(link)
  135. link.click()
  136. })
  137. .catch((error) => {
  138. alert(error)
  139. })
  140. .finally(() => {
  141. commit('setLoading', false)
  142. })
  143. },
  144. updateDocument({ commit }, data) {
  145. DocumentService.updateDocument(data.projectId, data.id, data)
  146. .then((response) => {
  147. commit('updateDocument', response.data)
  148. })
  149. .catch((error) => {
  150. alert(error)
  151. })
  152. },
  153. deleteAllDocuments({ commit, state }, projectId) {
  154. DocumentService.deleteAllDocuments(projectId)
  155. .then((response) => {
  156. commit('setDocumentList', [])
  157. commit('setTotalItems', 0)
  158. commit('resetSelected')
  159. })
  160. .catch((error) => {
  161. alert(error)
  162. })
  163. },
  164. deleteDocument({ commit, state }, projectId) {
  165. for (const document of state.selected) {
  166. DocumentService.deleteDocument(projectId, document.id)
  167. .then((response) => {
  168. commit('deleteDocument', document.id)
  169. })
  170. .catch((error) => {
  171. alert(error)
  172. })
  173. }
  174. commit('resetSelected')
  175. },
  176. addAnnotation({ commit, state }, payload) {
  177. const documentId = state.items[state.current].id
  178. AnnotationService.addAnnotation(payload.projectId, documentId, payload)
  179. .then((response) => {
  180. commit('addAnnotation', response.data)
  181. })
  182. .catch((error) => {
  183. alert(error)
  184. })
  185. },
  186. updateAnnotation({ commit, state }, payload) {
  187. const documentId = state.items[state.current].id
  188. AnnotationService.updateAnnotation(payload.projectId, documentId, payload.annotationId, payload)
  189. .then((response) => {
  190. commit('updateAnnotation', response.data)
  191. })
  192. .catch((error) => {
  193. alert(error)
  194. })
  195. },
  196. deleteAnnotation({ commit, state }, payload) {
  197. const documentId = state.items[state.current].id
  198. AnnotationService.deleteAnnotation(payload.projectId, documentId, payload.annotationId)
  199. .then((response) => {
  200. commit('deleteAnnotation', payload.annotationId)
  201. })
  202. .catch((error) => {
  203. alert(error)
  204. })
  205. },
  206. clearAnnotations({ commit, state }, projectId) {
  207. const documentId = state.items[state.current].id
  208. AnnotationService.clearAnnotations(projectId, documentId)
  209. .then((response) => {
  210. commit('clearAnnotations')
  211. })
  212. .catch((error) => {
  213. alert(error)
  214. })
  215. },
  216. approve({ commit, getters }, payload) {
  217. const documentId = getters.currentDoc.id
  218. const data = {
  219. approved: !getters.currentDoc.annotation_approver
  220. }
  221. DocumentService.approveDocument(payload.projectId, documentId, data)
  222. .then((response) => {
  223. commit('updateDocument', response.data)
  224. })
  225. .catch((error) => {
  226. alert(error)
  227. })
  228. },
  229. async autoLabeling({ commit, state }, payload) {
  230. const document = state.items[state.current]
  231. if (document) {
  232. const response = await AnnotationService.autoLabel(payload.projectId, document.id)
  233. commit('setAnnotations', response.data)
  234. // commit('setLoading', true)
  235. // return AnnotationService.autoLabel(payload.projectId, document.id)
  236. // .then((response) => {
  237. // commit('setAnnotations', response.data)
  238. // })
  239. // .catch((error) => {
  240. // const message = error.response.data['detail']
  241. // Promise.reject(error)
  242. // // throw new Error(message)
  243. // })
  244. // .finally(() => {
  245. // commit('setLoading', false)
  246. // })
  247. }
  248. },
  249. }