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.

134 lines
2.9 KiB

  1. <template>
  2. <base-card
  3. :disabled="!valid"
  4. :title="$t('dataset.importDataTitle')"
  5. :agree-text="$t('generic.upload')"
  6. :cancel-text="$t('generic.cancel')"
  7. @agree="create"
  8. @cancel="cancel"
  9. >
  10. <template #content>
  11. <v-form
  12. ref="form"
  13. v-model="valid"
  14. >
  15. <v-alert
  16. v-show="showError"
  17. v-model="showError"
  18. type="error"
  19. dismissible
  20. >
  21. {{ $t('errors.fileCannotUpload') + errorMsg }}
  22. </v-alert>
  23. <h2>{{ $t('dataset.importDataMessage1') }}</h2>
  24. <v-radio-group
  25. v-model="selectedFormat"
  26. :rules="fileFormatRules($t('rules.fileFormatRules'))"
  27. >
  28. <v-radio
  29. v-for="(format, i) in formats"
  30. :key="i"
  31. :label="format.text"
  32. :value="format"
  33. />
  34. </v-radio-group>
  35. <v-sheet
  36. v-if="selectedFormat"
  37. :dark="!$vuetify.theme.dark"
  38. :light="$vuetify.theme.dark"
  39. class="mb-5 pa-5"
  40. >
  41. <pre>{{ selectedFormat.example }}</pre>
  42. </v-sheet>
  43. <h2>{{ $t('dataset.importDataMessage2') }}</h2>
  44. <v-file-input
  45. v-model="file"
  46. multiple
  47. :accept="acceptType"
  48. :rules="uploadFileRules($t('rules.uploadFileRules'))"
  49. :label="$t('labels.filePlaceholder')"
  50. />
  51. </v-form>
  52. </template>
  53. </base-card>
  54. </template>
  55. <script>
  56. import BaseCard from '@/components/utils/BaseCard'
  57. import { fileFormatRules, uploadFileRules } from '@/rules/index'
  58. export default {
  59. components: {
  60. BaseCard
  61. },
  62. props: {
  63. uploadDocument: {
  64. type: Function,
  65. default: () => {},
  66. required: true
  67. },
  68. formats: {
  69. type: Array,
  70. default: () => [],
  71. required: true
  72. }
  73. },
  74. data() {
  75. return {
  76. valid: false,
  77. file: null,
  78. selectedFormat: null,
  79. fileFormatRules,
  80. uploadFileRules,
  81. showError: false,
  82. errors: [],
  83. errorMsg: ''
  84. }
  85. },
  86. computed: {
  87. acceptType() {
  88. if (this.selectedFormat) {
  89. return `.${this.selectedFormat.extension}`
  90. } else {
  91. return '.txt'
  92. }
  93. }
  94. },
  95. methods: {
  96. cancel() {
  97. this.$emit('cancel')
  98. },
  99. reset() {
  100. this.$refs.form.reset()
  101. },
  102. create() {
  103. this.errors = []
  104. const promises = []
  105. const type = this.selectedFormat.type
  106. this.file.forEach((item) => {
  107. promises.push({
  108. format: type,
  109. file: item
  110. })
  111. })
  112. let p = Promise.resolve()
  113. promises.forEach((item) => {
  114. p = p.then(() => this.uploadDocument(item.file, item.format)).catch(() => {
  115. this.errors.push(item.file.name)
  116. this.showError = true
  117. })
  118. })
  119. p.finally(() => {
  120. if (!this.errors.length) {
  121. this.reset()
  122. this.$emit('success')
  123. } else {
  124. this.errorMsg = this.errors.join(', ')
  125. }
  126. })
  127. }
  128. }
  129. }
  130. </script>