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.

133 lines
2.7 KiB

  1. <template>
  2. <base-card
  3. :disabled="!valid"
  4. @agree="create"
  5. @cancel="cancel"
  6. title="Upload Data"
  7. agree-text="Upload"
  8. cancel-text="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. <code
  37. v-if="selectedFormat"
  38. class="mb-10 pa-5 highlight"
  39. >
  40. <span v-for="(example, index) in selectedFormat.examples" :key="index">{{ example }}</span>
  41. </code>
  42. <h2>Select a file</h2>
  43. <v-file-input
  44. v-model="file"
  45. :accept="acceptType"
  46. :rules="uploadFileRules"
  47. label="File input"
  48. />
  49. </v-form>
  50. </template>
  51. </base-card>
  52. </template>
  53. <script>
  54. import BaseCard from '@/components/molecules/BaseCard'
  55. import { fileFormatRules, uploadFileRules } from '@/rules/index'
  56. export default {
  57. components: {
  58. BaseCard
  59. },
  60. props: {
  61. uploadDocument: {
  62. type: Function,
  63. default: () => {},
  64. required: true
  65. },
  66. formats: {
  67. type: Array,
  68. default: () => [],
  69. required: true
  70. }
  71. },
  72. data() {
  73. return {
  74. valid: false,
  75. file: null,
  76. selectedFormat: null,
  77. fileFormatRules,
  78. uploadFileRules,
  79. showError: false
  80. }
  81. },
  82. computed: {
  83. acceptType() {
  84. if (this.selectedFormat) {
  85. return this.selectedFormat.accept
  86. } else {
  87. return '.txt,.csv,.json,.jsonl'
  88. }
  89. }
  90. },
  91. methods: {
  92. cancel() {
  93. this.$emit('close')
  94. },
  95. validate() {
  96. return this.$refs.form.validate()
  97. },
  98. reset() {
  99. this.$refs.form.reset()
  100. },
  101. create() {
  102. if (this.validate()) {
  103. this.uploadDocument({
  104. projectId: this.$route.params.id,
  105. format: this.selectedFormat.type,
  106. file: this.file
  107. })
  108. .then((response) => {
  109. this.reset()
  110. this.cancel()
  111. })
  112. .catch(() => {
  113. this.showError = true
  114. })
  115. }
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .highlight {
  122. font-size: 100%;
  123. width: 100%;
  124. }
  125. .highlight:before {
  126. content: ''
  127. }
  128. </style>