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.

226 lines
6.2 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. deleteAnnotation(state, annotationId) {
  65. state.items[state.current].annotations = state.items[state.current].annotations.filter(item => item.id !== annotationId)
  66. },
  67. clearAnnotations(state) {
  68. state.items[state.current].annotations = []
  69. },
  70. updateAnnotation(state, payload) {
  71. const item = state.items[state.current].annotations.find(item => item.id === payload.id)
  72. Object.assign(item, payload)
  73. },
  74. updateSearchOptions(state, payload) {
  75. state.searchOptions = Object.assign(state.searchOptions, payload)
  76. },
  77. initSearchOptions(state) {
  78. state.searchOptions = {
  79. limit: 10,
  80. offset: 0,
  81. q: '',
  82. isChecked: '',
  83. filterName: ''
  84. }
  85. }
  86. }
  87. export const actions = {
  88. getDocumentList({ commit, state }, payload) {
  89. commit('setLoading', true)
  90. return DocumentService.getDocumentList(payload)
  91. .then((response) => {
  92. commit('setDocumentList', response.data.results)
  93. commit('setTotalItems', response.data.count)
  94. })
  95. .catch((error) => {
  96. alert(error)
  97. })
  98. .finally(() => {
  99. commit('setLoading', false)
  100. })
  101. },
  102. uploadDocument({ commit, dispatch }, data) {
  103. commit('setLoading', true)
  104. const formData = new FormData()
  105. formData.append('file', data.file)
  106. formData.append('format', data.format)
  107. const config = {
  108. headers: {
  109. 'Content-Type': 'multipart/form-data'
  110. }
  111. }
  112. return DocumentService.uploadFile(data.projectId, formData, config)
  113. .then((response) => {
  114. dispatch('getDocumentList', data)
  115. })
  116. .finally(() => {
  117. commit('setLoading', false)
  118. })
  119. },
  120. exportDocument({ commit }, data) {
  121. commit('setLoading', true)
  122. DocumentService.exportFile(data.projectId, data.format)
  123. .then((response) => {
  124. const url = window.URL.createObjectURL(new Blob([response.data]))
  125. const link = document.createElement('a')
  126. link.href = url
  127. link.setAttribute('download', 'file.' + data.format)
  128. document.body.appendChild(link)
  129. link.click()
  130. })
  131. .catch((error) => {
  132. alert(error)
  133. })
  134. .finally(() => {
  135. commit('setLoading', false)
  136. })
  137. },
  138. updateDocument({ commit }, data) {
  139. DocumentService.updateDocument(data.projectId, data.id, data)
  140. .then((response) => {
  141. commit('updateDocument', response.data)
  142. })
  143. .catch((error) => {
  144. alert(error)
  145. })
  146. },
  147. deleteAllDocuments({ commit, state }, projectId) {
  148. DocumentService.deleteAllDocuments(projectId)
  149. .then((response) => {
  150. commit('setDocumentList', [])
  151. commit('setTotalItems', 0)
  152. commit('resetSelected')
  153. })
  154. .catch((error) => {
  155. alert(error)
  156. })
  157. },
  158. deleteDocument({ commit, state }, projectId) {
  159. for (const document of state.selected) {
  160. DocumentService.deleteDocument(projectId, document.id)
  161. .then((response) => {
  162. commit('deleteDocument', document.id)
  163. })
  164. .catch((error) => {
  165. alert(error)
  166. })
  167. }
  168. commit('resetSelected')
  169. },
  170. addAnnotation({ commit, state }, payload) {
  171. const documentId = state.items[state.current].id
  172. AnnotationService.addAnnotation(payload.projectId, documentId, payload)
  173. .then((response) => {
  174. commit('addAnnotation', response.data)
  175. })
  176. .catch((error) => {
  177. alert(error)
  178. })
  179. },
  180. updateAnnotation({ commit, state }, payload) {
  181. const documentId = state.items[state.current].id
  182. AnnotationService.updateAnnotation(payload.projectId, documentId, payload.annotationId, payload)
  183. .then((response) => {
  184. commit('updateAnnotation', response.data)
  185. })
  186. .catch((error) => {
  187. alert(error)
  188. })
  189. },
  190. deleteAnnotation({ commit, state }, payload) {
  191. const documentId = state.items[state.current].id
  192. AnnotationService.deleteAnnotation(payload.projectId, documentId, payload.annotationId)
  193. .then((response) => {
  194. commit('deleteAnnotation', payload.annotationId)
  195. })
  196. .catch((error) => {
  197. alert(error)
  198. })
  199. },
  200. clearAnnotations({ commit, state }, projectId) {
  201. const documentId = state.items[state.current].id
  202. AnnotationService.clearAnnotations(projectId, documentId)
  203. .then((response) => {
  204. commit('clearAnnotations')
  205. })
  206. .catch((error) => {
  207. alert(error)
  208. })
  209. },
  210. approve({ commit, getters }, payload) {
  211. const documentId = getters.currentDoc.id
  212. const data = {
  213. approved: !getters.currentDoc.annotation_approver
  214. }
  215. DocumentService.approveDocument(payload.projectId, documentId, data)
  216. .then((response) => {
  217. commit('updateDocument', response.data)
  218. })
  219. .catch((error) => {
  220. alert(error)
  221. })
  222. }
  223. }