Use leptos::either to handle mismatched return types instead of into_view()

This commit is contained in:
2024-12-23 20:58:53 -05:00
parent fe131b1ba2
commit 7732b77eb5
5 changed files with 60 additions and 55 deletions

View File

@ -1,6 +1,7 @@
use std::rc::Rc;
use leptos::prelude::*;
use leptos::either::*;
use leptos::logging::*;
use leptos_icons::*;
@ -127,11 +128,11 @@ pub fn SongImage(image_path: String, song_playing: MaybeSignal<bool>, list_index
view! {
<img class="song-image" src={image_path}/>
{move || if song_playing.get() {
view! { <Icon icon={icondata::BsPauseFill} on:click={pause_song}
{..} class="song-image-overlay song-playing-overlay" /> }.into_view()
Either::Left(view! { <Icon icon={icondata::BsPauseFill} on:click={pause_song}
{..} class="song-image-overlay song-playing-overlay" /> })
} else {
view! { <Icon icon={icondata::BsPlayFill} on:click={play_song}
{..} class="song-image-overlay hide-until-hover" /> }.into_view()
Either::Right(view! { <Icon icon={icondata::BsPlayFill} on:click={play_song}
{..} class="song-image-overlay hide-until-hover" /> })
}}
}
}
@ -147,9 +148,9 @@ pub fn SongArtists(artists: Vec<Artist>) -> impl IntoView {
view! {
{
if let Some(id) = artist.id {
view! { <a href={format!("/artist/{}", id)}>{artist.name.clone()}</a> }.into_view()
Either::Left(view! { <a href={format!("/artist/{}", id)}>{artist.name.clone()}</a> })
} else {
view! { <span>{artist.name.clone()}</span> }.into_view()
Either::Right(view! { <span>{artist.name.clone()}</span> })
}
}
{if i < num_artists - 2 { ", " } else if i == num_artists - 2 { " & " } else { "" }}
@ -165,9 +166,9 @@ pub fn SongAlbum(album: Option<Album>) -> impl IntoView {
<span>
{
if let Some(id) = album.id {
view! { <a href={format!("/album/{}", id)}>{album.title.clone()}</a> }.into_view()
Either::Left(view! { <a href={format!("/album/{}", id)}>{album.title.clone()}</a> })
} else {
view! { <span>{album.title.clone()}</span> }.into_view()
Either::Right(view! { <span>{album.title.clone()}</span> })
}
}
</span>

View File

@ -1,5 +1,6 @@
use leptos::leptos_dom::*;
use leptos::prelude::*;
use leptos::either::*;
use leptos_router::*;
use crate::components::song_list::*;
use crate::api::album::*;
@ -52,12 +53,12 @@ pub fn AlbumPage() -> impl IntoView {
albumdata.with( |albumdata| {
match albumdata {
Some(Ok(s)) => {
view! { <AlbumInfo albumdata=(*s).clone() /> }
EitherOf3::A(view! { <AlbumInfo albumdata=(*s).clone() /> })
},
Some(Err(e)) => {
view! { <div class="error">{format!("Error loading album : {}",e)}</div> }.into_view()
EitherOf3::B(view! { <div class="error">{format!("Error loading album : {}",e)}</div> })
},
None => {view! { }.into_view()}
None => {EitherOf3::C(view! { })}
}
})
}}
@ -71,12 +72,12 @@ pub fn AlbumPage() -> impl IntoView {
song_list.with( |song_list| {
match song_list {
Some(Ok(s)) => {
view! { <SongList songs=(*s).clone()/> }
EitherOf3::A(view! { <SongList songs=(*s).clone()/> })
},
Some(Err(e)) => {
view! { <div class="error">{format!("Error loading albums: : {}",e)}</div> }.into_view()
EitherOf3::B(view! { <div class="error">{format!("Error loading albums: : {}",e)}</div> })
},
None => {view! { }.into_view()}
None => {EitherOf3::C(view! { })}
}
})
}}

View File

@ -1,4 +1,5 @@
use leptos::prelude::*;
use leptos::either::*;
use leptos_router::use_params_map;
use leptos_icons::*;
use server_fn::error::NoCustomError;
@ -20,23 +21,23 @@ pub fn ArtistPage() -> impl IntoView {
{move || params.with(|params| {
match params.get("id").map(|id| id.parse::<i32>()) {
Some(Ok(id)) => {
view! { <ArtistIdProfile id /> }.into_view()
Either::Left(view! { <ArtistIdProfile id /> })
},
Some(Err(e)) => {
view! {
Either::Right(view! {
<Error<String>
title="Invalid Artist ID"
error=e.to_string()
/>
}.into_view()
})
},
None => {
view! {
Either::Right(view! {
<Error<String>
title="No Artist ID"
message="You must specify an artist ID to view their page."
/>
}.into_view()
})
}
}
})}
@ -60,20 +61,20 @@ fn ArtistIdProfile(#[prop(into)] id: MaybeSignal<i32>) -> impl IntoView {
match artist {
Ok(Some(artist)) => {
show_details.set(true);
view! { <ArtistProfile artist /> }.into_view()
EitherOf3::A(view! { <ArtistProfile artist /> })
},
Ok(None) => view! {
Ok(None) => EitherOf3::B(view! {
<Error<String>
title="Artist Not Found"
message=format!("Artist with ID {} not found", id.get())
/>
}.into_view(),
Err(error) => view! {
}),
Err(error) => EitherOf3::C(view! {
<ServerError<NoCustomError>
title="Error Getting Artist"
error
/>
}.into_view(),
}),
}
})}
</Transition>

View File

@ -1,4 +1,5 @@
use leptos::prelude::*;
use leptos::either::*;
use leptos_router::use_params_map;
use leptos_icons::*;
use server_fn::error::NoCustomError;
@ -38,20 +39,20 @@ pub fn Profile() -> impl IntoView {
match params.get("id").map(|id| id.parse::<i32>()) {
None => {
// No id specified, show the current user's profile
view! { <OwnProfile /> }.into_view()
EitherOf3::A(view! { <OwnProfile /> })
},
Some(Ok(id)) => {
// Id specified, get the user and show their profile
view! { <UserIdProfile id /> }.into_view()
EitherOf3::B(view! { <UserIdProfile id /> })
},
Some(Err(e)) => {
// Invalid id, return an error
view! {
EitherOf3::C(view! {
<Error<String>
title="Invalid User ID"
error=e.to_string()
/>
}.into_view()
})
}
}
})}
@ -70,19 +71,19 @@ fn OwnProfile() -> impl IntoView {
match user {
Some(user) => {
let user_id = user.id.unwrap();
view! {
Either::Left(view! {
<UserProfile user />
<TopSongs user_id={user_id} />
<RecentSongs user_id={user_id} />
<TopArtists user_id={user_id} />
}.into_view()
})
},
None => view! {
None => Either::Right(view! {
<Error<String>
title="Not Logged In"
message="You must be logged in to view your profile"
/>
}.into_view(),
}),
}
})}
</Transition>
@ -108,27 +109,27 @@ fn UserIdProfile(#[prop(into)] id: MaybeSignal<i32>) -> impl IntoView {
Ok(Some(user)) => {
show_details.set(true);
view! { <UserProfile user /> }.into_view()
EitherOf3::A(view! { <UserProfile user /> })
},
Ok(None) => {
show_details.set(false);
view! {
EitherOf3::B(view! {
<Error<String>
title="User Not Found"
message=format!("User with ID {} not found", id.get())
/>
}.into_view()
})
},
Err(error) => {
show_details.set(false);
view! {
EitherOf3::C(view! {
<ServerError<NoCustomError>
title="Error Getting User"
error
/>
}.into_view()
})
}
}
})}

View File

@ -1,4 +1,5 @@
use leptos::prelude::*;
use leptos::either::*;
use leptos_router::use_params_map;
use leptos_icons::*;
use server_fn::error::NoCustomError;
@ -25,23 +26,23 @@ pub fn SongPage() -> impl IntoView {
{move || params.with(|params| {
match params.get("id").map(|id| id.parse::<i32>()) {
Some(Ok(id)) => {
view! { <SongDetails id /> }.into_view()
Either::Left(view! { <SongDetails id /> })
},
Some(Err(e)) => {
view! {
Either::Right(view! {
<Error<String>
title="Invalid Song ID"
error=e.to_string()
/>
}.into_view()
})
},
None => {
view! {
Either::Right(view! {
<Error<String>
title="No Song ID"
message="You must specify a song ID to view its page."
/>
}.into_view()
})
}
}
})}
@ -62,23 +63,23 @@ fn SongDetails(#[prop(into)] id: MaybeSignal<i32>) -> impl IntoView {
{move || song_info.get().map(|song| {
match song {
Ok(Some(song)) => {
view! { <SongOverview song /> }.into_view()
EitherOf3::A(view! { <SongOverview song /> })
},
Ok(None) => {
view! {
EitherOf3::B(view! {
<Error<String>
title="Song Not Found"
message=format!("Song with ID {} not found", id.get())
/>
}.into_view()
})
},
Err(error) => {
view! {
EitherOf3::C(view! {
<ServerError<NoCustomError>
title="Error Fetching Song"
error
/>
}.into_view()
})
}
}
})}
@ -154,17 +155,17 @@ fn SongPlays(#[prop(into)] id: MaybeSignal<i32>) -> impl IntoView {
{move || plays.get().map(|plays| {
match plays {
Ok(plays) => {
view! {
Either::Left(view! {
<p>{format!("Plays: {}", plays)}</p>
}.into_view()
})
},
Err(error) => {
view! {
Either::Right(view! {
<ServerError<NoCustomError>
title="Error fetching song plays"
error
/>
}.into_view()
})
}
}
})}
@ -183,17 +184,17 @@ fn MySongPlays(#[prop(into)] id: MaybeSignal<i32>) -> impl IntoView {
{move || plays.get().map(|plays| {
match plays {
Ok(plays) => {
view! {
Either::Left(view! {
<p>{format!("My Plays: {}", plays)}</p>
}.into_view()
})
},
Err(error) => {
view! {
Either::Right(view! {
<ServerError<NoCustomError>
title="Error fetching my song plays"
error
/>
}.into_view()
})
}
}
})}