Include playlist resource in global state
This commit is contained in:
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Model for a playlist
|
||||
#[db_type(crate::schema::playlists)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Playlist {
|
||||
/// A unique id for the playlist
|
||||
#[omit_new]
|
||||
|
@ -2,6 +2,8 @@ use leptos::logging::*;
|
||||
use leptos::prelude::*;
|
||||
|
||||
use crate::api::auth::get_logged_in_user;
|
||||
use crate::api::playlists::get_playlists;
|
||||
use crate::models::backend;
|
||||
use crate::models::backend::User;
|
||||
use crate::models::frontend::PlayStatus;
|
||||
|
||||
@ -18,6 +20,9 @@ pub struct GlobalState {
|
||||
|
||||
/// The current play status
|
||||
pub play_status: RwSignal<PlayStatus>,
|
||||
|
||||
/// A resource that fetches the playlists
|
||||
pub playlists: Resource<Result<Vec<backend::Playlist>, ServerFnError>>,
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
@ -37,9 +42,13 @@ impl GlobalState {
|
||||
},
|
||||
);
|
||||
|
||||
// Refetch playlists when the logged in user changes
|
||||
let playlists = Resource::new(move || logged_in_user.track(), |_| get_playlists());
|
||||
|
||||
Self {
|
||||
logged_in_user,
|
||||
play_status,
|
||||
playlists,
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,6 +59,10 @@ impl GlobalState {
|
||||
pub fn play_status() -> RwSignal<PlayStatus> {
|
||||
expect_context::<Self>().play_status
|
||||
}
|
||||
|
||||
pub fn playlists() -> Resource<Result<Vec<backend::Playlist>, ServerFnError>> {
|
||||
expect_context::<Self>().playlists
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GlobalState {
|
||||
|
Reference in New Issue
Block a user