From da96a0921260c31ead13bcce70abf087b102de17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=A3=E7=B6=BA=E5=87=9B?= Date: Sun, 3 Nov 2024 02:25:16 +0800 Subject: [PATCH] fix: cloud sync error wasn't show --- src/utils/ajax.js | 10 +++++++++- src/views/Material/index.js | 30 ++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/utils/ajax.js b/src/utils/ajax.js index a5fa8e48..0f7e605b 100644 --- a/src/utils/ajax.js +++ b/src/utils/ajax.js @@ -55,12 +55,20 @@ export default { mode: 'cors', }).then(r => r.json()), getJson: async id => - fetch(`${JSON_STORAGE_BASE_URL}/${id}`, { mode: 'cors' }).then(r => r.json()), + fetch(`${JSON_STORAGE_BASE_URL}/${id}`, { mode: 'cors' }).then(r => { + if (r.status === 404) return null; + if (r.status !== 200) throw new Error(r.statusText); + return r.json(); + }), updateJson: async (id, obj) => fetch(`${JSON_STORAGE_BASE_URL}/${id}`, { method: 'PUT', body: JSON.stringify(obj), headers: { 'Content-Type': 'application/json' }, mode: 'cors', + }).then(r => { + if (r.status === 404) return false; + if (r.status !== 200) throw new Error(r.statusText); + return true; }), }; diff --git a/src/views/Material/index.js b/src/views/Material/index.js index a3b3bbb2..f59bf9e1 100644 --- a/src/views/Material/index.js +++ b/src/views/Material/index.js @@ -1320,28 +1320,30 @@ export default defineComponent({ this.dataSyncing = true; if (this.syncCode) { Ajax.updateJson(this.syncCode, data) - .then(() => { - this.dataSyncing = false; - if (!silence) this.$snackbar(this.$t('cultivate.snackbar.backupSucceeded')); + .then(success => { + if (success) { + if (!silence) this.$snackbar(this.$t('cultivate.snackbar.backupSucceeded')); + return; + } + this.$snackbar(this.$t('cultivate.snackbar.syncCodeInvalid')); }) .catch(e => { + this.$snackbar(`${this.$t('cultivate.snackbar.backupFailed')} ${e}`); + }) + .finally(() => { this.dataSyncing = false; - this.$snackbar( - `${this.$t('cultivate.snackbar.backupFailed')} ${e.responseText || e.message || ''}`, - ); }); } else { Ajax.createJson(data) .then(({ id }) => { - this.dataSyncing = false; this.syncCode = id; this.$snackbar(this.$t('cultivate.snackbar.backupSucceeded')); }) .catch(e => { + this.$snackbar(`${this.$t('cultivate.snackbar.backupFailed')} ${e}`); + }) + .finally(() => { this.dataSyncing = false; - this.$snackbar( - `${this.$t('cultivate.snackbar.backupFailed')} ${e.responseText || e.message || ''}`, - ); }); } if (!silence) { @@ -1357,20 +1359,20 @@ export default defineComponent({ Ajax.getJson(this.syncCode) .then(data => { if (!data) { - this.dataSyncing = false; - this.$snackbar(this.$t('cultivate.snackbar.restoreFailed')); + this.$snackbar(this.$t('cultivate.snackbar.syncCodeInvalid')); return; } this.ignoreNextInputsChange = true; this.dataForSave = data; this.$snackbar(this.$t('cultivate.snackbar.restoreSucceeded')); - this.dataSyncing = false; }) .catch(e => { - this.dataSyncing = false; this.$snackbar( `${this.$t('cultivate.snackbar.restoreFailed')} ${e.responseText || e.message || ''}`, ); + }) + .finally(() => { + this.dataSyncing = false; }); this.$gtag.event('material_cloud_restore', { event_category: 'material',