diff --git a/src/webpage.rs b/src/webpage.rs index 33e0f59..deeb41b 100644 --- a/src/webpage.rs +++ b/src/webpage.rs @@ -1,7 +1,9 @@ use crate::localize_day; -use crate::session::{DatedSession, RegularSession, Session, WithNote}; +use crate::session::{ + AlterException, CancelException, DatedSession, ExtraSession, RegularSession, Session, WithNote, +}; use crate::session_date_calculator::{Day, DayIter, NthWeekday}; -use chrono::Weekday; +use chrono::{Datelike, Days, Months, NaiveDate, Weekday}; use leptos::prelude::*; use leptos::server_fn::request::browser::Request; use serde::{Deserialize, Serialize}; @@ -80,52 +82,54 @@ fn Sessions(config: SessionConfig) -> impl IntoView { #[component] fn DatedSession(session: DatedSession) -> impl IntoView { match session { - DatedSession::Regular { session, day, .. } => { - view! { -
-

""

-

{localize_day(&day)}

-

{session.note}

-
- }.into_any() + DatedSession::Regular { session, day, .. } => view! { +
+

""

+

{localize_day(&day)}

+

{session.note}

+
} - DatedSession::Extra(session) => { - view! { -
-

""

-

{localize_day(&session.date.into())}

-

{session.note}

-
- }.into_any() + .into_any(), + DatedSession::Extra(session) => view! { +
+

""

+

{localize_day(&session.date.into())}

+

{session.note}

+
} - DatedSession::Cancelled { session, day, .. } => { - view! { -
-

""

-

{localize_day(&day)}

-

{session.note}

-
- }.into_any() + .into_any(), + DatedSession::Cancelled { session, day, .. } => view! { +
+

""

+

{localize_day(&day)}

+

{session.note}

+
} - DatedSession::Altered { session, day, exception, .. } => { - let day = move || { - match exception.date.map(Day::from) { - None => view! { {localize_day(&day)} }.into_any(), - Some(new_day) => view! { - {localize_day(&day)} - " " - {localize_day(&new_day)} - }.into_any(), + .into_any(), + DatedSession::Altered { + session, + day, + exception, + .. + } => { + let day = move || match exception.date.map(Day::from) { + None => view! { {localize_day(&day)} }.into_any(), + Some(new_day) => view! { + {localize_day(&day)} + " " + {localize_day(&new_day)} } + .into_any(), }; - + view! {

"<Änderung>"

{day}

{exception.note.or(session.note)}

- }.into_any() + } + .into_any() } } } @@ -160,13 +164,56 @@ pub struct SessionConfig { pub sessions: Vec, } +macro_rules! date { + ($day: expr, $mon: expr, $year: expr) => { + NaiveDate::from_ymd_opt($year, $mon, $day).expect("valid date") + }; +} + impl Default for SessionConfig { + #[allow(clippy::zero_prefixed_literal)] fn default() -> Self { + const YEAR: i32 = 2024; + const NEXT_TUE_SESSION: NaiveDate = date!(18, 02, YEAR); + const NEXT_SUN_SESSION: NaiveDate = date!(02, 03, YEAR); + let next_next_sun_session = { + let some_day_next_month = NEXT_SUN_SESSION.checked_add_months(Months::new(1)).unwrap(); + NaiveDate::from_weekday_of_month_opt( + some_day_next_month.year(), + some_day_next_month.month(), + Weekday::Sun, + 1, + ) + .unwrap() + }; + Self { - motd: Some("Jeder 1. Sonntag und 3. Dienstag im Monat".to_string()), + motd: Some("Jeder 1. Sonntag und 3. Dienstag im Monat".into()), sessions: vec![ - RegularSession::from(NthWeekday::new(1, Weekday::Sun)).with_note("10:00 Uhr").into(), - RegularSession::from(NthWeekday::new(3, Weekday::Tue)).with_note("18:30 Uhr").into(), + RegularSession::from(NthWeekday::new(1, Weekday::Sun)) + .with_note("10:00 Uhr") + .with_exception( + NEXT_SUN_SESSION, + AlterException { + date: Some(NEXT_SUN_SESSION.checked_sub_days(Days::new(1)).unwrap()), + ..Default::default() + }, + ) + .with_exception( + next_next_sun_session, + AlterException { + note: Some("11 Uhr".into()), + ..Default::default() + }, + ) + .into(), + RegularSession::from(NthWeekday::new(3, Weekday::Tue)) + .with_note("18:30 Uhr") + .with_exception(NEXT_TUE_SESSION, CancelException::default()) + .into(), + ExtraSession::from(NEXT_TUE_SESSION.checked_add_days(Days::new(2)).unwrap()) + .with_note("18 Uhr") + .into(), ], } }