Refactor SessionConfig to use new Session data type
This commit is contained in:
parent
0c2716db8e
commit
6f648c0fb7
@ -1,4 +1,5 @@
|
||||
use crate::localize_day;
|
||||
use crate::session::{RegularSession, Session};
|
||||
use crate::session_date_calculator::{DayIter, NthWeekday};
|
||||
use chrono::Weekday;
|
||||
use leptos::prelude::*;
|
||||
@ -39,13 +40,14 @@ pub fn App() -> impl IntoView {
|
||||
|
||||
#[component]
|
||||
fn Sessions(config: SessionConfig) -> impl IntoView {
|
||||
let mut session_iter = DayIter::default().filter(move |day| {
|
||||
let mut session_iter = DayIter::default().flat_map(move |day| {
|
||||
config
|
||||
.sessions
|
||||
.iter()
|
||||
.any(|session_day| session_day.matches(day))
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter_map(move |session| session.clone().into_dated(day.clone()).ok())
|
||||
});
|
||||
let (session_dates, set_session_dates) =
|
||||
let (dated_sessions, mut_dated_sessions) =
|
||||
signal(session_iter.by_ref().take(2).collect::<Vec<_>>());
|
||||
|
||||
view! {
|
||||
@ -55,14 +57,14 @@ fn Sessions(config: SessionConfig) -> impl IntoView {
|
||||
<p>{config.motd}</p>
|
||||
</div>
|
||||
<For
|
||||
each=move || session_dates.get()
|
||||
key=|day| day.date
|
||||
children=move |day| view! { <div class="box elem-background">{localize_day(&day)}</div> }
|
||||
each=move || dated_sessions.get()
|
||||
key=|session| session.day.clone()
|
||||
children=move |session| view! { <div class="box elem-background">{localize_day(&session.day)}</div> }
|
||||
/>
|
||||
<div
|
||||
class="box button elem-background"
|
||||
on:click=move |_| {
|
||||
set_session_dates.write().extend(session_iter.by_ref().take(3));
|
||||
mut_dated_sessions.write().extend(session_iter.by_ref().take(3));
|
||||
}
|
||||
>
|
||||
"Mehr anzeigen"
|
||||
@ -98,7 +100,7 @@ where
|
||||
#[serde(default)]
|
||||
pub struct SessionConfig {
|
||||
pub motd: Option<String>,
|
||||
pub sessions: Vec<NthWeekday>,
|
||||
pub sessions: Vec<Session>,
|
||||
}
|
||||
|
||||
impl Default for SessionConfig {
|
||||
@ -106,8 +108,8 @@ impl Default for SessionConfig {
|
||||
Self {
|
||||
motd: Some("Probe jeden ersten Sonntag im Monat um 10:00 Uhr und jeden dritten Dienstag im Monat um 18:30 Uhr".to_string()),
|
||||
sessions: vec![
|
||||
NthWeekday::new(1, Weekday::Sun),
|
||||
NthWeekday::new(3, Weekday::Tue),
|
||||
RegularSession::from(NthWeekday::new(1, Weekday::Sun)).into(),
|
||||
RegularSession::from(NthWeekday::new(3, Weekday::Tue)).into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user