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.

116 lines
2.3 KiB

4 years ago
  1. <template>
  2. <base-card
  3. :disabled="!valid"
  4. title="Upload Label"
  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>Example format</h2>
  25. <v-sheet
  26. v-if="exampleFormat"
  27. :dark="!$vuetify.theme.dark"
  28. :light="$vuetify.theme.dark"
  29. class="mb-5 pa-5"
  30. >
  31. <pre>{{ exampleFormat }}</pre>
  32. </v-sheet>
  33. <h2>Select a file</h2>
  34. <v-file-input
  35. v-model="file"
  36. :rules="uploadFileRules"
  37. accept=".json"
  38. label="File input"
  39. />
  40. </v-form>
  41. </template>
  42. </base-card>
  43. </template>
  44. <script>
  45. import BaseCard from '@/components/molecules/BaseCard'
  46. import { uploadFileRules } from '@/rules/index'
  47. export default {
  48. components: {
  49. BaseCard
  50. },
  51. props: {
  52. uploadLabel: {
  53. type: Function,
  54. default: () => {},
  55. required: true
  56. }
  57. },
  58. data() {
  59. return {
  60. valid: false,
  61. file: null,
  62. uploadFileRules,
  63. showError: false
  64. }
  65. },
  66. computed: {
  67. exampleFormat() {
  68. const data = [
  69. {
  70. text: 'Dog',
  71. suffix_key: 'a',
  72. background_color: '#FF0000',
  73. text_color: '#ffffff'
  74. },
  75. {
  76. text: 'Cat',
  77. suffix_key: 'c',
  78. background_color: '#FF0000',
  79. text_color: '#ffffff'
  80. }
  81. ]
  82. return JSON.stringify(data, null, 4)
  83. }
  84. },
  85. methods: {
  86. cancel() {
  87. this.$emit('close')
  88. },
  89. validate() {
  90. return this.$refs.form.validate()
  91. },
  92. reset() {
  93. this.$refs.form.reset()
  94. },
  95. create() {
  96. if (this.validate()) {
  97. this.uploadLabel({
  98. projectId: this.$route.params.id,
  99. file: this.file
  100. })
  101. .then((response) => {
  102. this.reset()
  103. this.cancel()
  104. })
  105. .catch(() => {
  106. this.showError = true
  107. })
  108. }
  109. }
  110. }
  111. }
  112. </script>