|
85 | 85 | const buffer = reader.result as ArrayBuffer; |
86 | 86 | const string: string = new TextDecoder().decode(buffer); |
87 | 87 |
|
88 | | - this.$nuxt.$loading.start(); |
89 | | - let jsonInput: ImportExportFormat | any[]; |
| 88 | + let jsonInput: ImportExportFormat | any; |
| 89 | + // since TypeScript doesn't check type at runtime ; better check that |
| 90 | + const isExportFormat = (obj: any) => |
| 91 | + (obj as ImportExportFormat).categories && |
| 92 | + (obj as ImportExportFormat).exercises && |
| 93 | + Array.isArray(obj.exercises) && |
| 94 | + typeof (obj as ImportExportFormat).categories == "object" |
| 95 | + ; |
90 | 96 |
|
91 | 97 | try { |
| 98 | + this.$nuxt.$loading.start(); |
92 | 99 | // first validate the json before anything else |
93 | 100 | jsonInput = JSON.parse(string); |
94 | 101 | // at the end, both import type use the same endpoint |
95 | 102 | let exercises; |
96 | 103 |
|
97 | 104 | // Type Guards to distinct the two types |
98 | | - if ((jsonInput as ImportExportFormat).categories) { |
| 105 | + if (isExportFormat(jsonInput)) { |
99 | 106 |
|
100 | 107 | const exportFormat = jsonInput as ImportExportFormat; |
101 | 108 |
|
|
125 | 132 | state: ex.state, |
126 | 133 | tags: ex.tags.map(tag => ( |
127 | 134 | { |
128 | | - // TODO : tag state ??? |
129 | 135 | text: tag.text, |
130 | | - category_id: tags_dictionary[exportFormat.categories[tag.category]][0].id |
| 136 | + category_id: tags_dictionary[exportFormat.categories[tag.category]][0].id, |
| 137 | + state: tag.state |
131 | 138 | } |
132 | 139 | )) |
133 | 140 | }) |
|
149 | 156 | }) |
150 | 157 | } catch (e) { |
151 | 158 |
|
152 | | - if (e instanceof SyntaxError) { |
| 159 | + if (e instanceof SyntaxError || e instanceof TypeError || e instanceof ReferenceError) { |
153 | 160 | this.$displayError("Le contenu de votre fichier n'est pas correct."); |
154 | 161 | } else { |
155 | 162 | const error = e as AxiosError; |
|
0 commit comments