|
11 | 11 | <ValidationObserver ref="observer" tag="form" |
12 | 12 | @submit.prevent="validateBeforeSubmit"> |
13 | 13 | <ValidationProvider tag="div" |
14 | | - name="archive zip" |
| 14 | + name="fichier json" |
15 | 15 | rules="mimes:application/json" |
16 | 16 | ref="fileObserver" |
17 | 17 | v-slot="{ errors }"> |
|
50 | 50 | import Icon from "~/components/Symbols/Icon.vue"; |
51 | 51 | import CustomSelect from "~/components/Input/CustomSelect.vue"; |
52 | 52 |
|
53 | | - const debounce = require('lodash.debounce'); |
54 | | -
|
55 | 53 | @Component({ |
56 | 54 | components: {CustomSelect, ValidationObserver, ValidationProvider, Icon} |
57 | 55 | }) |
|
147 | 145 | const file: File | null = this.file(); |
148 | 146 |
|
149 | 147 | if (file !== null) { |
150 | | -
|
151 | | - reader.readAsText(file, "UTF-8"); |
152 | | - reader.onload = async (evt: any) => { |
153 | | - const text: string = evt.target.result; |
154 | | - try { |
155 | | - this.$nuxt.$loading.start(); |
156 | | - await this.$axios.$post("/api/bulk/create_exercises", JSON.parse(text)); |
157 | | - this.$displaySuccess("L'importation s'est correctement déroulée."); |
158 | | - } catch (e) { |
159 | | - const error = e as AxiosError; |
160 | | -
|
161 | | - if (error.response) { |
162 | | - const status = error.response.status; |
163 | | -
|
164 | | - if (status === 400) { |
165 | | - this.$displayWarning("Votre fichier ne possède pas le bon format.") |
166 | | - } else if (status === 401) { |
167 | | - this.$displayWarning("Vous n'êtes pas autorisé à effectuer cette action.") |
168 | | - } else if (status === 500) { |
169 | | - this.$displayError("Une erreur est survenue depuis nos serveurs.") |
| 148 | + reader.onloadend = async () => { |
| 149 | + if (reader.result !== null) { |
| 150 | + const buffer = reader.result as ArrayBuffer; |
| 151 | + const string: string = new TextDecoder().decode(buffer); |
| 152 | +
|
| 153 | + try { |
| 154 | + this.$nuxt.$loading.start(); |
| 155 | + await this.$axios.$post("/api/bulk/create_exercises", JSON.parse(string)); |
| 156 | + this.$displaySuccess("L'importation s'est correctement déroulée."); |
| 157 | + } catch (e) { |
| 158 | + const error = e as AxiosError; |
| 159 | +
|
| 160 | + if (error.response) { |
| 161 | + const status = error.response.status; |
| 162 | +
|
| 163 | + if (status === 400) { |
| 164 | + this.$displayWarning("Votre fichier ne possède pas le bon format.") |
| 165 | + } else if (status === 401) { |
| 166 | + this.$displayWarning("Vous n'êtes pas autorisé à effectuer cette action.") |
| 167 | + } else if (status === 500) { |
| 168 | + this.$displayError("Une erreur est survenue depuis nos serveurs.") |
| 169 | + } else { |
| 170 | + this.$displayError("Une erreur est survenue.") |
| 171 | + } |
170 | 172 | } else { |
171 | 173 | this.$displayError("Une erreur est survenue.") |
172 | 174 | } |
173 | | - } else { |
174 | | - this.$displayError("Une erreur est survenue.") |
| 175 | + } finally { |
| 176 | + this.$nuxt.$loading.finish(); |
175 | 177 | } |
176 | 178 |
|
177 | | - } finally { |
178 | | - this.$nuxt.$loading.finish(); |
179 | 179 | } |
180 | 180 | }; |
| 181 | +
|
| 182 | + reader.readAsArrayBuffer(file); |
| 183 | +
|
181 | 184 | } |
182 | 185 |
|
183 | 186 | requestAnimationFrame(() => { |
|
0 commit comments