From bcf843765a24a562dfd5593387105f7e34f947c7 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Wed, 3 Apr 2024 13:27:27 +0200 Subject: [PATCH] Fixed issue that loading state does not reset when database restarts --- application/src/util/surrealdb.rs | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs index bf1d6a0..2ac4cf3 100644 --- a/application/src/util/surrealdb.rs +++ b/application/src/util/surrealdb.rs @@ -92,6 +92,18 @@ struct SurrealResponse { result: SurrealResult, } +#[derive(Serialize, Deserialize, Clone)] +struct SurrealResultError { + code: i32, + message: String, +} + +#[derive(Serialize, Deserialize, Clone)] +struct SurrealResponseError { + id: u32, + error: SurrealResultError, +} + #[derive(Serialize, Deserialize, Clone, Debug)] struct SurrealAction { id: String, @@ -261,8 +273,8 @@ pub fn init_surrealdb() { fn surrealdb_response(response: String) { let response: SurrealResponse = match serde_json::from_str(&response) { Ok(res) => res, - Err(err) => { - log::warn!("{}", err); + Err(_) => { + handle_error(response); return; } }; @@ -282,6 +294,26 @@ fn surrealdb_response(response: String) { } } +/// Function to execute when an error is returned from Surrealdb +fn handle_error(response: String) { + let response: SurrealResponseError = match serde_json::from_str(&response) { + Ok(res) => res, + Err(err) => { + log::warn!("{}", err); + return; + } + }; + + if response.id == 0 && response.error.code == -32000 { + let (_, _, remove_token) = + use_local_storage::("surrealdb-token"); + let websocket = expect_context::(); + + remove_token(); + websocket.set_loading.set(false); + } +} + /// Function to execute when DB signin is succesful fn use_surrealdb(response: SurrealResponse) { util::toast::add_toast( -- 2.45.2