Render exceptions and extra sessions

This commit is contained in:
Leonard Steppy 2025-02-15 21:02:48 +01:00
parent 302d8002c9
commit 048c6c04fb

View File

@ -1,7 +1,9 @@
use crate::localize_day; 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 crate::session_date_calculator::{Day, DayIter, NthWeekday};
use chrono::Weekday; use chrono::{Datelike, Days, Months, NaiveDate, Weekday};
use leptos::prelude::*; use leptos::prelude::*;
use leptos::server_fn::request::browser::Request; use leptos::server_fn::request::browser::Request;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -80,52 +82,54 @@ fn Sessions(config: SessionConfig) -> impl IntoView {
#[component] #[component]
fn DatedSession(session: DatedSession) -> impl IntoView { fn DatedSession(session: DatedSession) -> impl IntoView {
match session { match session {
DatedSession::Regular { session, day, .. } => { DatedSession::Regular { session, day, .. } => view! {
view! { <div class="box elem-background">
<div class="box elem-background"> <p class="small-text">"<Regulärer Termin>"</p>
<p class="small-text">"<Regulärer Termin>"</p> <p><b>{localize_day(&day)}</b></p>
<p><b>{localize_day(&day)}</b></p> <p>{session.note}</p>
<p>{session.note}</p> </div>
</div>
}.into_any()
} }
DatedSession::Extra(session) => { .into_any(),
view! { DatedSession::Extra(session) => view! {
<div class="box highlighted-background"> <div class="box highlight-background">
<p class="small-text">"<Extra Probe>"</p> <p class="small-text">"<Extra Probe>"</p>
<p><b>{localize_day(&session.date.into())}</b></p> <p><b>{localize_day(&session.date.into())}</b></p>
<p>{session.note}</p> <p>{session.note}</p>
</div> </div>
}.into_any()
} }
DatedSession::Cancelled { session, day, .. } => { .into_any(),
view! { DatedSession::Cancelled { session, day, .. } => view! {
<div class="box cancel-background"> <div class="box cancel-background">
<p class="small-text">"<Entfall>"</p> <p class="small-text">"<Entfall>"</p>
<p class="strikethrough bold">{localize_day(&day)}</p> <p class="strikethrough bold">{localize_day(&day)}</p>
<p>{session.note}</p> <p>{session.note}</p>
</div> </div>
}.into_any()
} }
DatedSession::Altered { session, day, exception, .. } => { .into_any(),
let day = move || { DatedSession::Altered {
match exception.date.map(Day::from) { session,
None => view! { {localize_day(&day)} }.into_any(), day,
Some(new_day) => view! { exception,
<span class="strikethrough">{localize_day(&day)}</span> ..
" " } => {
{localize_day(&new_day)} let day = move || match exception.date.map(Day::from) {
}.into_any(), None => view! { {localize_day(&day)} }.into_any(),
Some(new_day) => view! {
<span class="strikethrough">{localize_day(&day)}</span>
" "
{localize_day(&new_day)}
} }
.into_any(),
}; };
view! { view! {
<div class="box change-background"> <div class="box change-background">
<p class="small-text">"<Änderung>"</p> <p class="small-text">"<Änderung>"</p>
<p class="bold">{day}</p> <p class="bold">{day}</p>
<p>{exception.note.or(session.note)}</p> <p>{exception.note.or(session.note)}</p>
</div> </div>
}.into_any() }
.into_any()
} }
} }
} }
@ -160,13 +164,56 @@ pub struct SessionConfig {
pub sessions: Vec<Session>, pub sessions: Vec<Session>,
} }
macro_rules! date {
($day: expr, $mon: expr, $year: expr) => {
NaiveDate::from_ymd_opt($year, $mon, $day).expect("valid date")
};
}
impl Default for SessionConfig { impl Default for SessionConfig {
#[allow(clippy::zero_prefixed_literal)]
fn default() -> Self { 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 { 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![ sessions: vec![
RegularSession::from(NthWeekday::new(1, Weekday::Sun)).with_note("10:00 Uhr").into(), RegularSession::from(NthWeekday::new(1, Weekday::Sun))
RegularSession::from(NthWeekday::new(3, Weekday::Tue)).with_note("18:30 Uhr").into(), .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(),
], ],
} }
} }