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.

127 lines
2.6 KiB

4 years ago
  1. <template>
  2. <base-card
  3. :disabled="!valid"
  4. title="Upload Data"
  5. agree-text="Upload"
  6. cancel-text="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. The file could not be uploaded. Maybe invalid format.
  22. Please check available formats carefully.
  23. </v-alert>
  24. <h2>Select a file format</h2>
  25. <v-radio-group
  26. v-model="selectedFormat"
  27. :rules="fileFormatRules"
  28. >
  29. <v-radio
  30. v-for="(format, i) in formats"
  31. :key="i"
  32. :label="format.text"
  33. :value="format"
  34. />
  35. </v-radio-group>
  36. <v-sheet
  37. v-if="selectedFormat"
  38. :dark="!$vuetify.theme.dark"
  39. :light="$vuetify.theme.dark"
  40. class="mb-5 pa-5"
  41. >
  42. <span v-for="(example, index) in selectedFormat.examples" :key="index">
  43. {{ example }}<br>
  44. </span>
  45. </v-sheet>
  46. <h2>Select a file</h2>
  47. <v-file-input
  48. v-model="file"
  49. :accept="acceptType"
  50. :rules="uploadFileRules"
  51. label="File input"
  52. />
  53. </v-form>
  54. </template>
  55. </base-card>
  56. </template>
  57. <script>
  58. import BaseCard from '@/components/molecules/BaseCard'
  59. import { fileFormatRules, uploadFileRules } from '@/rules/index'
  60. export default {
  61. components: {
  62. BaseCard
  63. },
  64. props: {
  65. uploadDocument: {
  66. type: Function,
  67. default: () => {},
  68. required: true
  69. },
  70. formats: {
  71. type: Array,
  72. default: () => [],
  73. required: true
  74. }
  75. },
  76. data() {
  77. return {
  78. valid: false,
  79. file: null,
  80. selectedFormat: null,
  81. fileFormatRules,
  82. uploadFileRules,
  83. showError: false
  84. }
  85. },
  86. computed: {
  87. acceptType() {
  88. if (this.selectedFormat) {
  89. return this.selectedFormat.accept
  90. } else {
  91. return '.txt,.csv,.json,.jsonl'
  92. }
  93. }
  94. },
  95. methods: {
  96. cancel() {
  97. this.$emit('close')
  98. },
  99. validate() {
  100. return this.$refs.form.validate()
  101. },
  102. reset() {
  103. this.$refs.form.reset()
  104. },
  105. create() {
  106. if (this.validate()) {
  107. this.uploadDocument({
  108. projectId: this.$route.params.id,
  109. format: this.selectedFormat.type,
  110. file: this.file
  111. })
  112. .then((response) => {
  113. this.reset()
  114. this.cancel()
  115. })
  116. .catch(() => {
  117. this.showError = true
  118. })
  119. }
  120. }
  121. }
  122. }
  123. </script>