Create GlobalState
This commit is contained in:
parent
47fc35814d
commit
d42737f856
@ -5,3 +5,5 @@ cfg_if! {
|
||||
pub mod audio;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod state;
|
||||
|
49
src/util/state.rs
Normal file
49
src/util/state.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use leptos::*;
|
||||
use leptos::logging::*;
|
||||
|
||||
use crate::playstatus::PlayStatus;
|
||||
use crate::models::User;
|
||||
use crate::auth::get_logged_in_user;
|
||||
|
||||
/// Global front-end state
|
||||
/// Contains anything frequently needed across multiple components
|
||||
/// Behaves like a singleton, in that provide/expect_context will
|
||||
/// always return the same instance
|
||||
#[derive(Clone)]
|
||||
pub struct GlobalState {
|
||||
/// A resource that fetches the logged in user
|
||||
/// This will not automatically refetch, so any login/logout related code
|
||||
/// should call `refetch` on this resource
|
||||
pub logged_in_user: Resource<(), Option<User>>,
|
||||
|
||||
/// The current play status
|
||||
pub play_status: RwSignal<PlayStatus>,
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
pub fn new() -> Self {
|
||||
let play_status = create_rw_signal(PlayStatus::default());
|
||||
|
||||
let logged_in_user = create_resource(|| (), |_| async {
|
||||
get_logged_in_user().await
|
||||
.inspect_err(|e| {
|
||||
error!("Error getting logged in user: {:?}", e);
|
||||
})
|
||||
.ok()
|
||||
.flatten()
|
||||
});
|
||||
|
||||
Self {
|
||||
logged_in_user,
|
||||
play_status,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logged_in_user() -> Resource<(), Option<User>> {
|
||||
expect_context::<Self>().logged_in_user
|
||||
}
|
||||
|
||||
pub fn play_status() -> RwSignal<PlayStatus> {
|
||||
expect_context::<Self>().play_status
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user