Merge pull request 'Fixed issue that loading state does not reset when database restarts' (#12) from feature/surrealdb-session into main

Reviewed-on: #12
This commit is contained in:
xeovalyte 2024-04-03 13:28:49 +02:00
commit 7637606d9c

View File

@ -92,6 +92,18 @@ struct SurrealResponse {
result: SurrealResult, 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)] #[derive(Serialize, Deserialize, Clone, Debug)]
struct SurrealAction { struct SurrealAction {
id: String, id: String,
@ -261,8 +273,8 @@ pub fn init_surrealdb() {
fn surrealdb_response(response: String) { fn surrealdb_response(response: String) {
let response: SurrealResponse = match serde_json::from_str(&response) { let response: SurrealResponse = match serde_json::from_str(&response) {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(_) => {
log::warn!("{}", err); handle_error(response);
return; 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::<String, FromToStringCodec>("surrealdb-token");
let websocket = expect_context::<SurrealContext>();
remove_token();
websocket.set_loading.set(false);
}
}
/// Function to execute when DB signin is succesful /// Function to execute when DB signin is succesful
fn use_surrealdb(response: SurrealResponse) { fn use_surrealdb(response: SurrealResponse) {
util::toast::add_toast( util::toast::add_toast(