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.

126 lines
2.7 KiB

4 years ago
  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') }}
  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. <span v-for="(example, index) in selectedFormat.examples" :key="index">
  42. {{ example }}<br>
  43. </span>
  44. </v-sheet>
  45. <h2>{{ $t('dataset.importDataMessage2') }}</h2>
  46. <v-file-input
  47. v-model="file"
  48. :accept="acceptType"
  49. :rules="uploadFileRules($t('rules.uploadFileRules'))"
  50. :label="$t('labels.filePlaceholder')"
  51. />
  52. </v-form>
  53. </template>
  54. </base-card>
  55. </template>
  56. <script>
  57. import BaseCard from '@/components/molecules/BaseCard'
  58. import { fileFormatRules, uploadFileRules } from '@/rules/index'
  59. export default {
  60. components: {
  61. BaseCard
  62. },
  63. props: {
  64. uploadDocument: {
  65. type: Function,
  66. default: () => {},
  67. required: true
  68. },
  69. formats: {
  70. type: Array,
  71. default: () => [],
  72. required: true
  73. }
  74. },
  75. data() {
  76. return {
  77. valid: false,
  78. file: null,
  79. selectedFormat: null,
  80. fileFormatRules,
  81. uploadFileRules,
  82. showError: false
  83. }
  84. },
  85. computed: {
  86. acceptType() {
  87. if (this.selectedFormat) {
  88. return this.selectedFormat.accept
  89. } else {
  90. return '.txt,.csv,.json,.jsonl'
  91. }
  92. }
  93. },
  94. methods: {
  95. cancel() {
  96. this.$emit('close')
  97. },
  98. validate() {
  99. return this.$refs.form.validate()
  100. },
  101. reset() {
  102. this.$refs.form.reset()
  103. },
  104. create() {
  105. if (this.validate()) {
  106. this.uploadDocument({
  107. projectId: this.$route.params.id,
  108. format: this.selectedFormat.type,
  109. file: this.file
  110. })
  111. .then((response) => {
  112. this.reset()
  113. this.cancel()
  114. })
  115. .catch(() => {
  116. this.showError = true
  117. })
  118. }
  119. }
  120. }
  121. }
  122. </script>