Refactor DashboardTile into trait
This commit is contained in:
parent
2793391f00
commit
553e24800b
@ -2,19 +2,17 @@ use leptos::html::Ul;
|
|||||||
use leptos::leptos_dom::*;
|
use leptos::leptos_dom::*;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_use::{use_element_size, UseElementSizeReturn, use_scroll, UseScrollReturn};
|
use leptos_use::{use_element_size, UseElementSizeReturn, use_scroll, UseScrollReturn};
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use crate::components::dashboard_tile::DashboardTile;
|
use crate::components::dashboard_tile::DashboardTile;
|
||||||
use leptos_icons::*;
|
use leptos_icons::*;
|
||||||
|
|
||||||
/// A row of dashboard tiles, with a title
|
/// A row of dashboard tiles, with a title
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
pub struct DashboardRow {
|
pub struct DashboardRow {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub tiles: Vec<DashboardTile>,
|
pub tiles: Vec<Box<dyn DashboardTile>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DashboardRow {
|
impl DashboardRow {
|
||||||
pub fn new(title: String, tiles: Vec<DashboardTile>) -> Self {
|
pub fn new(title: String, tiles: Vec<Box<dyn DashboardTile>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
title,
|
title,
|
||||||
tiles,
|
tiles,
|
||||||
@ -109,7 +107,7 @@ impl IntoView for DashboardRow {
|
|||||||
{self.tiles.into_iter().map(|tile_info| {
|
{self.tiles.into_iter().map(|tile_info| {
|
||||||
view! {
|
view! {
|
||||||
<li>
|
<li>
|
||||||
{ tile_info }
|
{ tile_info.into_view() }
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>()}
|
}).collect::<Vec<_>>()}
|
||||||
|
@ -1,68 +1,26 @@
|
|||||||
use leptos::leptos_dom::*;
|
use leptos::leptos_dom::*;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use crate::media_type::MediaType;
|
|
||||||
|
|
||||||
/// Info representing what will be displayed in a dashboard tile
|
pub trait DashboardTile {
|
||||||
#[derive(Serialize, Deserialize)]
|
fn image_path(&self) -> String;
|
||||||
pub struct DashboardTile {
|
fn title(&self) -> String;
|
||||||
pub image_path: String,
|
fn link(&self) -> String;
|
||||||
pub title: String,
|
fn description(&self) -> Option<String> { None }
|
||||||
pub media_type: Option<MediaType>,
|
|
||||||
pub artist: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DashboardTile {
|
impl IntoView for &dyn DashboardTile {
|
||||||
pub fn new(image_path: String, title: String, media_type: Option<MediaType>, artist: Option<String>) -> Self {
|
|
||||||
Self {
|
|
||||||
image_path,
|
|
||||||
title,
|
|
||||||
media_type,
|
|
||||||
artist: artist.map(|artist| artist.to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the description of the dashboard tile
|
|
||||||
/// Will display the media type, and the artist if it is available and relevant
|
|
||||||
pub fn description(&self) -> String {
|
|
||||||
match self.media_type {
|
|
||||||
Some(MediaType::Song) => {
|
|
||||||
if let Some(artist) = &self.artist {
|
|
||||||
format!("{} • {}", MediaType::Song.to_string(), artist)
|
|
||||||
} else {
|
|
||||||
MediaType::Song.to_string()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(MediaType::Album) => {
|
|
||||||
if let Some(artist) = &self.artist {
|
|
||||||
format!("{} • {}", MediaType::Album.to_string(), artist)
|
|
||||||
} else {
|
|
||||||
MediaType::Album.to_string()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(MediaType::Artist) => {
|
|
||||||
MediaType::Artist.to_string()
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
if let Some(artist) = &self.artist {
|
|
||||||
artist.to_string()
|
|
||||||
} else {
|
|
||||||
"".to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoView for DashboardTile {
|
|
||||||
fn into_view(self) -> View {
|
fn into_view(self) -> View {
|
||||||
let description = self.description();
|
let link = self.link();
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<div class="dashboard-tile">
|
<div class="dashboard-tile">
|
||||||
<img src={self.image_path} alt="dashboard-tile" />
|
<a href={link}>
|
||||||
<p class="dashboard-tile-title">{self.title}</p>
|
<img src={self.image_path()} alt="dashboard-tile" />
|
||||||
<p class="dashboard-tile-description">{description}</p>
|
<p class="dashboard-tile-title">{self.title()}</p>
|
||||||
|
<p class="dashboard-tile-description">
|
||||||
|
{self.description().unwrap_or_default()}
|
||||||
|
</p>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
}.into_view()
|
}.into_view()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user