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.

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