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::*;
|
||||
use leptos_use::{use_element_size, UseElementSizeReturn, use_scroll, UseScrollReturn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::components::dashboard_tile::DashboardTile;
|
||||
use leptos_icons::*;
|
||||
|
||||
/// A row of dashboard tiles, with a title
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DashboardRow {
|
||||
pub title: String,
|
||||
pub tiles: Vec<DashboardTile>,
|
||||
pub tiles: Vec<Box<dyn DashboardTile>>,
|
||||
}
|
||||
|
||||
impl DashboardRow {
|
||||
pub fn new(title: String, tiles: Vec<DashboardTile>) -> Self {
|
||||
pub fn new(title: String, tiles: Vec<Box<dyn DashboardTile>>) -> Self {
|
||||
Self {
|
||||
title,
|
||||
tiles,
|
||||
@ -109,7 +107,7 @@ impl IntoView for DashboardRow {
|
||||
{self.tiles.into_iter().map(|tile_info| {
|
||||
view! {
|
||||
<li>
|
||||
{ tile_info }
|
||||
{ tile_info.into_view() }
|
||||
</li>
|
||||
}
|
||||
}).collect::<Vec<_>>()}
|
||||
|
@ -1,68 +1,26 @@
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::media_type::MediaType;
|
||||
|
||||
/// Info representing what will be displayed in a dashboard tile
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DashboardTile {
|
||||
pub image_path: String,
|
||||
pub title: String,
|
||||
pub media_type: Option<MediaType>,
|
||||
pub artist: Option<String>,
|
||||
pub trait DashboardTile {
|
||||
fn image_path(&self) -> String;
|
||||
fn title(&self) -> String;
|
||||
fn link(&self) -> String;
|
||||
fn description(&self) -> Option<String> { None }
|
||||
}
|
||||
|
||||
impl 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 {
|
||||
impl IntoView for &dyn DashboardTile {
|
||||
fn into_view(self) -> View {
|
||||
let description = self.description();
|
||||
let link = self.link();
|
||||
|
||||
view! {
|
||||
<div class="dashboard-tile">
|
||||
<img src={self.image_path} alt="dashboard-tile" />
|
||||
<p class="dashboard-tile-title">{self.title}</p>
|
||||
<p class="dashboard-tile-description">{description}</p>
|
||||
<a href={link}>
|
||||
<img src={self.image_path()} alt="dashboard-tile" />
|
||||
<p class="dashboard-tile-title">{self.title()}</p>
|
||||
<p class="dashboard-tile-description">
|
||||
{self.description().unwrap_or_default()}
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
}.into_view()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user