Skip to content

Commit 42d159c

Browse files
committed
fix(ImportForm): no bug when adding, deleting then adding the same file
- also, refactor the code to let the FileInput component doing the job.
1 parent fe7239d commit 42d159c

File tree

1 file changed

+29
-88
lines changed

1 file changed

+29
-88
lines changed

components/Gestion/ImportForm.vue

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,21 @@
66
<p>
77
Vous pouvez importer des exercices depuis cette interface en tenant compte de ce
88
<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 !
1010
</p>
11-
<ValidationObserver ref="observer" tag="form"
11+
<ValidationObserver ref="observer"
12+
tag="form"
1213
@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">
1820
<span class="label__name">
1921
Uploadez votre fichier (json)
2022
</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>
3124

3225
</ValidationObserver>
3326

@@ -45,89 +38,30 @@
4538
<script lang="ts">
4639
4740
import {Component, Vue, Prop, Ref} from "vue-property-decorator";
48-
import {ValidationProvider, ValidationObserver} from 'vee-validate';
41+
import {ValidationObserver} from 'vee-validate';
4942
import {AxiosError} from "axios";
5043
import Icon from "~/components/Symbols/Icon.vue";
51-
import CustomSelect from "~/components/Input/CustomSelect.vue";
44+
import FileInput from "~/components/Input/FileInput.vue";
5245
5346
@Component({
54-
components: {CustomSelect, ValidationObserver, ValidationProvider, Icon}
47+
components: {FileInput, ValidationObserver, Icon}
5548
})
5649
export default class ExerciseForm extends Vue {
5750
/**
5851
* Validation Observer for the zip archive and the url
5952
*/
6053
@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;
7754
55+
@Ref() fileObserver!: FileInput;
7856
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;
9858
99-
const inputFile: any = this.fileObserver.value;
59+
form: {file: File | null} = {
60+
file: null
61+
};
10062
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;
13165
}
13266
13367
/**
@@ -142,7 +76,7 @@
14276
14377
if (isValid) {
14478
145-
const file: File | null = this.file();
79+
const file: File | null = this.form.file;
14680
14781
if (file !== null) {
14882
reader.onloadend = async () => {
@@ -154,6 +88,13 @@
15488
this.$nuxt.$loading.start();
15589
await this.$axios.$post("/api/bulk/create_exercises", JSON.parse(string));
15690
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+
})
15798
} catch (e) {
15899
const error = e as AxiosError;
159100
@@ -170,7 +111,7 @@
170111
this.$displayError("Une erreur est survenue.")
171112
}
172113
} else {
173-
this.$displayError("Une erreur est survenue.")
114+
this.$displayError("Le contenu de votre fichier n'est pas correct.")
174115
}
175116
} finally {
176117
this.$nuxt.$loading.finish();

0 commit comments

Comments
 (0)