Fix handling of artists resource in song component

This commit is contained in:
Connor Wittman 2024-04-12 23:46:37 -04:00
parent cf07ec2982
commit 13d6d07e8e

View File

@ -1,11 +1,17 @@
use leptos::*; use leptos::*;
use leptos::logging::*;
use crate::api::songs::get_artists; use crate::api::songs::get_artists;
#[component] #[component]
pub fn Song(song_id_arg: Option<i32>, song_image_path: String, song_title: String) -> impl IntoView { pub fn Song(song_id_arg: Option<i32>, song_image_path: String, song_title: String) -> impl IntoView {
let song_artists_resource = create_resource(|| (), move |_| async move { let song_id = Signal::derive(move || song_id_arg);
let artists_vec = get_artists(song_id_arg).await.unwrap_or(Vec::new());
let song_artists_resource = create_resource(song_id, move |song_id| async move {
let artists_vec = get_artists(song_id).await.unwrap_or_else(|_| {
warn!("Error when searching for artists for song");
Vec::new()
});
// convert the vec of artists to a string of artists separated by commas // convert the vec of artists to a string of artists separated by commas
let artists_string = artists_vec.iter().map(|artist| artist.name.clone()).collect::<Vec<String>>().join(", "); let artists_string = artists_vec.iter().map(|artist| artist.name.clone()).collect::<Vec<String>>().join(", ");
artists_string artists_string
@ -17,11 +23,16 @@ pub fn Song(song_id_arg: Option<i32>, song_image_path: String, song_title: Strin
<div class="queue-song-info"> <div class="queue-song-info">
<h3>{song_title}</h3> <h3>{song_title}</h3>
<Suspense <Suspense
fallback=move || view! { <h3>Loading...</h3> } fallback=move || view! {<p class="fallback-artists">""</p>}
> >
{move || { {move || {
song_artists_resource.get().map(|artists_string| view! { song_artists_resource.get().map(|artists_string| {
<h3>{artists_string}</h3> if artists_string.is_empty() {
view! {<p class="fallback-artists">""</p>}
}
else {
view! {<p class="artists">{artists_string}</p>}
}
}) })
}} }}
</Suspense> </Suspense>