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.

222 lines
5.6 KiB

5 years ago
  1. import DocumentService from '@/services/document.service'
  2. import AnnotationService from '@/services/annotation.service'
  3. import CSVParser from '@/services/parsers/csv.service'
  4. export const state = () => ({
  5. items: [],
  6. selected: [],
  7. loading: false,
  8. selectedFormat: null,
  9. parsed: {},
  10. current: 0,
  11. total: 0
  12. })
  13. export const getters = {
  14. isDocumentSelected(state) {
  15. return state.selected.length > 0
  16. },
  17. formatList() {
  18. return [
  19. {
  20. type: 'csv',
  21. text: 'Upload a CSV file from your computer',
  22. accept: '.csv'
  23. },
  24. {
  25. type: 'plain',
  26. text: 'Upload text items from your computer',
  27. accept: '.txt'
  28. },
  29. {
  30. type: 'json',
  31. text: 'Upload a JSON file from your computer',
  32. accept: '.json,.jsonl'
  33. }
  34. ]
  35. },
  36. headers() {
  37. return [
  38. {
  39. text: 'Text',
  40. align: 'left',
  41. value: 'text',
  42. sortable: false
  43. },
  44. {
  45. text: 'Metadata',
  46. align: 'left',
  47. value: 'meta',
  48. sortable: false
  49. }
  50. ]
  51. },
  52. parsedDoc(state) {
  53. if ('data' in state.parsed) {
  54. return state.parsed.data
  55. } else {
  56. return []
  57. }
  58. },
  59. currentDoc(state) {
  60. return state.items[state.current]
  61. }
  62. }
  63. export const mutations = {
  64. setCurrent(state, payload) {
  65. state.current = payload
  66. },
  67. setDocumentList(state, payload) {
  68. state.items = payload
  69. },
  70. addDocument(state, document) {
  71. state.items.unshift(document)
  72. },
  73. deleteDocument(state, documentId) {
  74. state.items = state.items.filter(item => item.id !== documentId)
  75. },
  76. updateSelected(state, selected) {
  77. state.selected = selected
  78. },
  79. updateDocument(state, document) {
  80. const item = state.items.find(item => item.id === document.id)
  81. Object.assign(item, document)
  82. },
  83. resetSelected(state) {
  84. state.selected = []
  85. },
  86. setLoading(state, payload) {
  87. state.loading = payload
  88. },
  89. setTotalItems(state, payload) {
  90. state.total = payload
  91. },
  92. parseFile(state, text) {
  93. const parser = new CSVParser()
  94. state.parsed = parser.parse(text)
  95. },
  96. addAnnotation(state, payload) {
  97. state.items[state.current].annotations.push(payload)
  98. },
  99. deleteAnnotation(state, annotationId) {
  100. state.items[state.current].annotations = state.items[state.current].annotations.filter(item => item.id !== annotationId)
  101. },
  102. updateAnnotation(state, payload) {
  103. const item = state.items[state.current].annotations.find(item => item.id === payload.id)
  104. Object.assign(item, payload)
  105. }
  106. }
  107. export const actions = {
  108. getDocumentList({ commit }, payload) {
  109. commit('setLoading', true)
  110. return DocumentService.getDocumentList(payload)
  111. .then((response) => {
  112. commit('setDocumentList', response.results)
  113. commit('setTotalItems', response.count)
  114. })
  115. .catch((error) => {
  116. alert(error)
  117. })
  118. .finally(() => {
  119. commit('setLoading', false)
  120. })
  121. },
  122. uploadDocument({ commit }, data) {
  123. commit('setLoading', true)
  124. DocumentService.uploadFile(data.projectId, data)
  125. .then((response) => {
  126. commit('addDocument', response)
  127. })
  128. .catch((error) => {
  129. alert(error)
  130. })
  131. .finally(() => {
  132. commit('setLoading', false)
  133. })
  134. },
  135. exportDocument({ commit }, data) {
  136. commit('setLoading', true)
  137. DocumentService.exportFile(data.projectId, data.format)
  138. .then((response) => {
  139. const url = window.URL.createObjectURL(new Blob([response.data]))
  140. const link = document.createElement('a')
  141. link.href = url
  142. link.setAttribute('download', 'file.' + data.format)
  143. document.body.appendChild(link)
  144. link.click()
  145. })
  146. .catch((error) => {
  147. alert(error)
  148. })
  149. .finally(() => {
  150. commit('setLoading', false)
  151. })
  152. },
  153. updateDocument({ commit }, data) {
  154. DocumentService.updateDocument(data.projectId, data.id, data)
  155. .then((response) => {
  156. commit('updateDocument', response)
  157. })
  158. .catch((error) => {
  159. alert(error)
  160. })
  161. },
  162. deleteDocument({ commit, state }, projectId) {
  163. for (const document of state.selected) {
  164. DocumentService.deleteDocument(projectId, document.id)
  165. .then((response) => {
  166. commit('deleteDocument', document.id)
  167. })
  168. .catch((error) => {
  169. alert(error)
  170. })
  171. }
  172. commit('resetSelected')
  173. },
  174. nextPage({ commit }) {
  175. },
  176. prevPage({ commit }) {
  177. },
  178. parseFile({ commit }, data) {
  179. const reader = new FileReader()
  180. reader.readAsText(data, 'UTF-8')
  181. reader.onload = (e) => {
  182. commit('parseFile', e.target.result)
  183. }
  184. reader.onerror = (e) => {
  185. alert(e)
  186. }
  187. },
  188. addAnnotation({ commit, state }, payload) {
  189. const documentId = state.items[state.current].id
  190. AnnotationService.addAnnotation(payload.projectId, documentId, payload)
  191. .then((response) => {
  192. commit('addAnnotation', response)
  193. })
  194. .catch((error) => {
  195. alert(error)
  196. })
  197. },
  198. updateAnnotation({ commit, state }, payload) {
  199. const documentId = state.items[state.current].id
  200. AnnotationService.updateAnnotation(payload.projectId, documentId, payload.annotationId, payload)
  201. .then((response) => {
  202. commit('updateAnnotation', response)
  203. })
  204. .catch((error) => {
  205. alert(error)
  206. })
  207. },
  208. deleteAnnotation({ commit, state }, payload) {
  209. const documentId = state.items[state.current].id
  210. AnnotationService.deleteAnnotation(payload.projectId, documentId, payload.annotationId)
  211. .then((response) => {
  212. commit('deleteAnnotation', payload.annotationId)
  213. })
  214. .catch((error) => {
  215. alert(error)
  216. })
  217. }
  218. }