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::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! {
<div class="box elem-background">
<p class="small-text">"<Regulärer Termin>"</p>
<p><b>{localize_day(&day)}</b></p>
<p>{session.note}</p>
</div>
}.into_any()
DatedSession::Regular { session, day, .. } => view! {
<div class="box elem-background">
<p class="small-text">"<Regulärer Termin>"</p>
<p><b>{localize_day(&day)}</b></p>
<p>{session.note}</p>
</div>
}
DatedSession::Extra(session) => {
view! {
<div class="box highlighted-background">
<p class="small-text">"<Extra Probe>"</p>
<p><b>{localize_day(&session.date.into())}</b></p>
<p>{session.note}</p>
</div>
}.into_any()
.into_any(),
DatedSession::Extra(session) => view! {
<div class="box highlight-background">
<p class="small-text">"<Extra Probe>"</p>
<p><b>{localize_day(&session.date.into())}</b></p>
<p>{session.note}</p>
</div>
}
DatedSession::Cancelled { session, day, .. } => {
view! {
<div class="box cancel-background">
<p class="small-text">"<Entfall>"</p>
<p class="strikethrough bold">{localize_day(&day)}</p>
<p>{session.note}</p>
</div>
}.into_any()
.into_any(),
DatedSession::Cancelled { session, day, .. } => view! {
<div class="box cancel-background">
<p class="small-text">"<Entfall>"</p>
<p class="strikethrough bold">{localize_day(&day)}</p>
<p>{session.note}</p>
</div>
}
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! {
<span class="strikethrough">{localize_day(&day)}</span>
" "
{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! {
<span class="strikethrough">{localize_day(&day)}</span>
" "
{localize_day(&new_day)}
}
.into_any(),
};
view! {
<div class="box change-background">
<p class="small-text">"<Änderung>"</p>
<p class="bold">{day}</p>
<p>{exception.note.or(session.note)}</p>
</div>
}.into_any()
}
.into_any()
}
}
}
@ -160,13 +164,56 @@ pub struct SessionConfig {
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 {
#[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(),
],
}
}