|
6 | 6 | <p> |
7 | 7 | Vous pouvez importer des exercices depuis cette interface en tenant compte de ce |
8 | 8 | <a href="https://sourcecodeoer.github.io/sourcecode_api/#operation/createMultipleExercises" target="_blank"> |
9 | | - format</a>. |
| 9 | + format</a>. Votre fichier doit être en UTF-8 ! |
10 | 10 | </p> |
11 | | - <ValidationObserver ref="observer" tag="form" |
| 11 | + <ValidationObserver ref="observer" |
| 12 | + tag="form" |
12 | 13 | @submit.prevent="validateBeforeSubmit"> |
13 | | - <ValidationProvider tag="div" |
14 | | - name="fichier json" |
15 | | - rules="mimes:application/json" |
16 | | - ref="fileObserver" |
17 | | - v-slot="{ errors }"> |
| 14 | + |
| 15 | + <FileInput |
| 16 | + ref="fileObserver" |
| 17 | + rules="required|mimes:application/json" |
| 18 | + name="fichier json" |
| 19 | + @input="updateFile"> |
18 | 20 | <span class="label__name"> |
19 | 21 | Uploadez votre fichier (json) |
20 | 22 | </span> |
21 | | - <input id="Archive" name="archive" ref="inputFile" @change="selectedFile" class="input--secondary-color" |
22 | | - type="file"> |
23 | | - <label for="Archive"> |
24 | | - <Icon type="archive" theme="theme--white"/> |
25 | | - {{labelFileText}}</label> |
26 | | - <span class="error-message">{{errors[0]}}</span> |
27 | | - <span class="message message--red" v-if="filename" |
28 | | - style="text-decoration: underline; cursor: pointer;" |
29 | | - @click="deleteFile">Supprimer le fichier</span> |
30 | | - </ValidationProvider> |
| 23 | + </FileInput> |
31 | 24 |
|
32 | 25 | </ValidationObserver> |
33 | 26 |
|
|
45 | 38 | <script lang="ts"> |
46 | 39 |
|
47 | 40 | import {Component, Vue, Prop, Ref} from "vue-property-decorator"; |
48 | | - import {ValidationProvider, ValidationObserver} from 'vee-validate'; |
| 41 | + import {ValidationObserver} from 'vee-validate'; |
49 | 42 | import {AxiosError} from "axios"; |
50 | 43 | import Icon from "~/components/Symbols/Icon.vue"; |
51 | | - import CustomSelect from "~/components/Input/CustomSelect.vue"; |
| 44 | + import FileInput from "~/components/Input/FileInput.vue"; |
52 | 45 |
|
53 | 46 | @Component({ |
54 | | - components: {CustomSelect, ValidationObserver, ValidationProvider, Icon} |
| 47 | + components: {FileInput, ValidationObserver, Icon} |
55 | 48 | }) |
56 | 49 | export default class ExerciseForm extends Vue { |
57 | 50 | /** |
58 | 51 | * Validation Observer for the zip archive and the url |
59 | 52 | */ |
60 | 53 | @Ref() observer!: InstanceType<typeof ValidationObserver>; |
61 | | - /** |
62 | | - * Observer for the input file element |
63 | | - */ |
64 | | - @Ref() inputFile!: HTMLInputElement; |
65 | | - /** |
66 | | - * Validation Observer for the zip archive and the url |
67 | | - */ |
68 | | - @Ref() fileObserver!: InstanceType<typeof ValidationProvider>; |
69 | | -
|
70 | | - @Prop({type: String, required: true}) title!: string; |
71 | | -
|
72 | | - /** |
73 | | - * The name of the uploaded file |
74 | | - * Default is null |
75 | | - */ |
76 | | - filename: string | null = null; |
77 | 54 |
|
| 55 | + @Ref() fileObserver!: FileInput; |
78 | 56 |
|
79 | | - /** |
80 | | - * Returns the name of the uploaded file or a default message instead |
81 | | - */ |
82 | | - protected get labelFileText() { |
83 | | - if (this.filename !== null) { |
84 | | - if (this.filename.length > 18) { |
85 | | - return this.filename.slice(0, 18) + '...' |
86 | | - } |
87 | | -
|
88 | | - return this.filename |
89 | | - } |
90 | | -
|
91 | | - return 'Choisir un fichier...' |
92 | | - } |
93 | | -
|
94 | | - /** |
95 | | - * Get the json file from the input file element |
96 | | - */ |
97 | | - file(): File | null { |
| 57 | + @Prop({type: String, required: true}) title!: string; |
98 | 58 |
|
99 | | - const inputFile: any = this.fileObserver.value; |
| 59 | + form: {file: File | null} = { |
| 60 | + file: null |
| 61 | + }; |
100 | 62 |
|
101 | | - if (!inputFile) { |
102 | | - return null; |
103 | | - } |
104 | | -
|
105 | | - return inputFile |
106 | | - } |
107 | | -
|
108 | | - /** |
109 | | - * Event for the changed state of the input file |
110 | | - */ |
111 | | - async selectedFile(event: Event) { |
112 | | - const inputElement: HTMLInputElement | null = event.target as HTMLInputElement | null; |
113 | | -
|
114 | | - if (inputElement !== null) { |
115 | | - const files = inputElement.files; |
116 | | - if (files !== null) { |
117 | | - const file: File | null = files.item(0); |
118 | | - this.filename = file !== null ? file.name : null; |
119 | | - await this.fileObserver.validate(file); |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | -
|
124 | | - /** |
125 | | - * Delete file from input and reset the filename |
126 | | - */ |
127 | | - deleteFile() { |
128 | | - this.filename = null; |
129 | | - this.inputFile.files = null; |
130 | | - this.fileObserver.reset(); |
| 63 | + updateFile(file: File | null) { |
| 64 | + this.form.file = file; |
131 | 65 | } |
132 | 66 |
|
133 | 67 | /** |
|
142 | 76 |
|
143 | 77 | if (isValid) { |
144 | 78 |
|
145 | | - const file: File | null = this.file(); |
| 79 | + const file: File | null = this.form.file; |
146 | 80 |
|
147 | 81 | if (file !== null) { |
148 | 82 | reader.onloadend = async () => { |
|
154 | 88 | this.$nuxt.$loading.start(); |
155 | 89 | await this.$axios.$post("/api/bulk/create_exercises", JSON.parse(string)); |
156 | 90 | this.$displaySuccess("L'importation s'est correctement déroulée."); |
| 91 | +
|
| 92 | + this.$nextTick(() => { |
| 93 | + this.form.file; |
| 94 | + // @ts-ignore |
| 95 | + this.fileObserver.deleteFile(); |
| 96 | + this.observer.reset(); |
| 97 | + }) |
157 | 98 | } catch (e) { |
158 | 99 | const error = e as AxiosError; |
159 | 100 |
|
|
170 | 111 | this.$displayError("Une erreur est survenue.") |
171 | 112 | } |
172 | 113 | } else { |
173 | | - this.$displayError("Une erreur est survenue.") |
| 114 | + this.$displayError("Le contenu de votre fichier n'est pas correct.") |
174 | 115 | } |
175 | 116 | } finally { |
176 | 117 | this.$nuxt.$loading.finish(); |
|
0 commit comments