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.

267 lines
7.6 KiB

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