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.

115 lines
2.3 KiB

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