Add card table loading
This commit is contained in:
@@ -11,6 +11,14 @@ use crate::{
|
||||
use perseus::prelude::*;
|
||||
use sycamore::prelude::*;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(client)] {
|
||||
use crate::endpoints::CARD_INFO;
|
||||
use crate::templates::get_api_path;
|
||||
use crate::models::card::CardTable;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct LayoutProps<'a, G: Html> {
|
||||
pub content_state: ContentState,
|
||||
@@ -51,12 +59,42 @@ pub fn Layout<'a, G: Html>(
|
||||
|
||||
let content_state_header = content_state.clone();
|
||||
|
||||
#[cfg(client)]
|
||||
{
|
||||
// TODO -> try to use suspense
|
||||
spawn_local_scoped(cx, async move {
|
||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||
|
||||
let card_table_loaded = (*global_state.constants.is_loaded.get()).clone();
|
||||
if !card_table_loaded {
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(get_api_path(CARD_INFO).as_str())
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// TODO add error handling
|
||||
let response = response.json::<CardTable>().await.unwrap();
|
||||
global_state.constants.card_table.set(Some(response));
|
||||
global_state.constants.is_loaded.set(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
view! { cx,
|
||||
// Main page header, including login functionality
|
||||
Header(content_state = content_state_header)
|
||||
|
||||
// Modals
|
||||
section(class = "flex-2") {
|
||||
(match (*global_state.constants.card_table.get()).clone() {
|
||||
Some(card_table) => { view!{ cx,
|
||||
p { "DONE" }
|
||||
} },
|
||||
None => { view!{ cx, p { "Loading cards" } } },
|
||||
})
|
||||
|
||||
(match *global_state.modals_open.login.get() {
|
||||
OpenState::Open => {
|
||||
view! { cx,
|
||||
|
||||
Reference in New Issue
Block a user