|
| 1 | +<template> |
| 2 | + <section class="content"> |
| 3 | + |
| 4 | + <h1>{{title}}</h1> |
| 5 | + |
| 6 | + <p> |
| 7 | + Vous pouvez importer des exercices depuis cette interface en tenant compte de ce |
| 8 | + <a href="https://sourcecodeoer.github.io/sourcecode_api/#operation/createMultipleExercises" target="_blank"> |
| 9 | + format</a>. Votre fichier doit être en UTF-8 ! |
| 10 | + </p> |
| 11 | + <ValidationObserver ref="observer" |
| 12 | + tag="form" |
| 13 | + @submit.prevent="validateBeforeSubmit"> |
| 14 | + |
| 15 | + <FileInput |
| 16 | + ref="fileObserver" |
| 17 | + rules="required|mimes:application/json" |
| 18 | + name="fichier json" |
| 19 | + @input="updateFile"> |
| 20 | + <span class="label__name"> |
| 21 | + Uploadez votre fichier (json) |
| 22 | + </span> |
| 23 | + </FileInput> |
| 24 | + |
| 25 | + </ValidationObserver> |
| 26 | + |
| 27 | + <p class="disclaimer">* champs obligatoires</p> |
| 28 | + <div class="cta__validate--wrapper"> |
| 29 | + <button @click="validateBeforeSubmit" |
| 30 | + class="button--ternary-color-reverse cta__validate"> |
| 31 | + Importer les exercices |
| 32 | + </button> |
| 33 | + </div> |
| 34 | + </section> |
| 35 | + |
| 36 | +</template> |
| 37 | + |
| 38 | +<script lang="ts"> |
| 39 | +
|
| 40 | + import {Component, Vue, Prop, Ref} from "vue-property-decorator"; |
| 41 | + import {ValidationObserver} from 'vee-validate'; |
| 42 | + import {AxiosError} from "axios"; |
| 43 | + import Icon from "~/components/Symbols/Icon.vue"; |
| 44 | + import FileInput from "~/components/Input/FileInput.vue"; |
| 45 | +
|
| 46 | + @Component({ |
| 47 | + components: {FileInput, ValidationObserver, Icon} |
| 48 | + }) |
| 49 | + export default class ExerciseForm extends Vue { |
| 50 | + /** |
| 51 | + * Validation Observer for the zip archive and the url |
| 52 | + */ |
| 53 | + @Ref() observer!: InstanceType<typeof ValidationObserver>; |
| 54 | +
|
| 55 | + @Ref() fileObserver!: FileInput; |
| 56 | +
|
| 57 | + @Prop({type: String, required: true}) title!: string; |
| 58 | +
|
| 59 | + form: {file: File | null} = { |
| 60 | + file: null |
| 61 | + }; |
| 62 | +
|
| 63 | + updateFile(file: File | null) { |
| 64 | + this.form.file = file; |
| 65 | + } |
| 66 | +
|
| 67 | + /** |
| 68 | + * Validate the entire page and send the new exercise |
| 69 | + */ |
| 70 | + async validateBeforeSubmit() { |
| 71 | +
|
| 72 | + // Basic validation form |
| 73 | + const isValid = await this.observer.validate(); |
| 74 | +
|
| 75 | + let reader = new FileReader(); |
| 76 | +
|
| 77 | + if (isValid) { |
| 78 | +
|
| 79 | + const file: File | null = this.form.file; |
| 80 | +
|
| 81 | + if (file !== null) { |
| 82 | + reader.onloadend = async () => { |
| 83 | + if (reader.result !== null) { |
| 84 | + const buffer = reader.result as ArrayBuffer; |
| 85 | + const string: string = new TextDecoder().decode(buffer); |
| 86 | +
|
| 87 | + try { |
| 88 | + this.$nuxt.$loading.start(); |
| 89 | + await this.$axios.$post("/api/bulk/create_exercises", JSON.parse(string)); |
| 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 | + }) |
| 98 | + } catch (e) { |
| 99 | + const error = e as AxiosError; |
| 100 | +
|
| 101 | + if (error.response) { |
| 102 | + const status = error.response.status; |
| 103 | +
|
| 104 | + if (status === 400) { |
| 105 | + this.$displayWarning("Votre fichier ne possède pas le bon format.") |
| 106 | + } else if (status === 401) { |
| 107 | + this.$displayWarning("Vous n'êtes pas autorisé à effectuer cette action.") |
| 108 | + } else if (status === 500) { |
| 109 | + this.$displayError("Une erreur est survenue depuis nos serveurs.") |
| 110 | + } else { |
| 111 | + this.$displayError("Une erreur est survenue.") |
| 112 | + } |
| 113 | + } else { |
| 114 | + this.$displayError("Le contenu de votre fichier n'est pas correct.") |
| 115 | + } |
| 116 | + } finally { |
| 117 | + this.$nuxt.$loading.finish(); |
| 118 | + } |
| 119 | +
|
| 120 | + } |
| 121 | + }; |
| 122 | +
|
| 123 | + reader.readAsArrayBuffer(file); |
| 124 | +
|
| 125 | + } |
| 126 | +
|
| 127 | + requestAnimationFrame(() => { |
| 128 | + this.observer.reset(); |
| 129 | + }); |
| 130 | +
|
| 131 | + } else { |
| 132 | + this.$displayWarning("Votre fichier ne possède pas le bon format.", {time: 5000}) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +
|
| 137 | +</script> |
| 138 | + |
| 139 | +<style lang="scss" scoped> |
| 140 | + @import "../../assets/css/exercise-gestion"; |
| 141 | +</style> |
0 commit comments