Opening hours completed. Leptos upgraded to 0.5.2.

This commit is contained in:
2023-11-11 16:27:40 +01:00
parent e7af2d402d
commit 1de6b74665
13 changed files with 469 additions and 77 deletions
+81
View File
@@ -0,0 +1,81 @@
use chrono::Weekday;
use leptos::*;
use crate::backend::data::{DayHours, WeekHours};
use crate::backend::opening_hours::get_hours;
use crate::components::modal_box::DialogOpener;
use crate::locales::trl;
use crate::pages::hours_edit::EditHours;
fn show_time(tm: &str) -> impl Fn() -> String {
if tm.is_empty() {
trl("Closed")
} else {
trl(tm)
}
}
fn show_day(day: &Weekday) -> impl Fn() -> String {
match day {
Weekday::Mon => { trl("Monday") }
Weekday::Tue => { trl("Tuesday") }
Weekday::Wed => { trl("Wednesday") }
Weekday::Thu => { trl("Thursday") }
Weekday::Fri => { trl("Friday") }
Weekday::Sat => { trl("Saturday") }
Weekday::Sun => { trl("Sunday") }
}
}
#[component]
pub fn OpeningHours() -> impl IntoView {
let editor = DialogOpener::new();
let hours = create_blocking_resource(move || editor.visible(), move |_| {get_hours()});
let hrs = create_rw_signal(WeekHours::default());
view! {
<EditHours opener=editor hours=hrs.read_only() />
<div class="card mb-3">
<div class="card-body">
<h5 class="card-title"><i class="bx bxs-watch"></i>" "{trl("Opening hours")}</h5>
<p class="card-text">
<Transition fallback=move || view! {<p>{trl("Loading...")}</p> }>
{move || {
hours.get().map(|h| match h {
Ok(h) => {
let h = create_rw_signal(h);
let d = create_rw_signal((0..7).collect::<Vec<u8>>());
view! {
<div>
<table class="table card-table">
<For each=move || d.get() key=|day| *day
children=move |day| {
let week_day = Weekday::try_from(day).unwrap_or(Weekday::Mon);
let hr_c = h.get().get(&week_day).unwrap_or(&Vec::new()).clone();
let hr_day = hr_c.clone();
view! {
<tr>
<td>{show_day(&week_day)}</td>
<td>{show_time(&DayHours(hr_day).to_string())}</td>
<td><a href="javascript:void(0)" class="card-link" on:click = move |_| {
hrs.set(WeekHours::new(week_day, hr_c.clone()));
editor.show();
}><i class="bx bx-edit-alt me-1"></i></a></td>
</tr>
}
}
/>
</table>
</div>
}
}
Err(e) => {view! {<div><p>{trl("Error loading data")}</p>
<p>{e.to_string()}</p></div>
}}
})
}}
</Transition>
</p>
</div>
</div>
}
}