Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70920ede87 | ||
|
|
7220ec87bc | ||
|
|
a2c851a853 | ||
|
|
51b2b1a98c |
@@ -53,3 +53,8 @@ settings = Settings
|
||||
settings-language = Language
|
||||
settings-theme = Theme
|
||||
view-details = Details
|
||||
pick-week = Go to week
|
||||
hour-from = From
|
||||
hour-to = Until
|
||||
repeat-weeks = Repeat for (weeks)
|
||||
repeat-hint = 1 books a single week. A higher number repeats the same hours every following week.
|
||||
|
||||
@@ -53,3 +53,8 @@ settings = Nastavenia
|
||||
settings-language = Jazyk
|
||||
settings-theme = Téma
|
||||
view-details = Detaily
|
||||
pick-week = Prejsť na týždeň
|
||||
hour-from = Od
|
||||
hour-to = Do
|
||||
repeat-weeks = Opakovať (počet týždňov)
|
||||
repeat-hint = 1 rezervuje jeden týždeň. Vyššie číslo opakuje rovnaké hodiny každý ďalší týždeň.
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<h1 class="text-2xl font-bold">
|
||||
{% if mode == "edit" %}{{ t(key="edit-booking", lang=lang) }}{% else %}{{ t(key="add-booking", lang=lang) }}{% endif %}
|
||||
</h1>
|
||||
<a href="/admin" class="btn btn-ghost btn-sm">« {{ t(key="back-to-calendar", lang=lang) }}</a>
|
||||
<a href="/admin?court={{ court_id }}&week={{ date }}" class="btn btn-ghost btn-sm">« {{ t(key="back-to-calendar", lang=lang) }}</a>
|
||||
</div>
|
||||
|
||||
<div class="card border border-base-300 bg-base-100 shadow-sm">
|
||||
@@ -24,6 +24,34 @@
|
||||
<label class="label"><span class="label-text">{{ t(key="date", lang=lang) }}</span></label>
|
||||
<input type="date" name="date" value="{{ date }}" required class="input input-bordered w-full">
|
||||
</div>
|
||||
{% if mode == "new" %}
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour-from", lang=lang) }}</span></label>
|
||||
<select name="hour_start" id="hour_start" class="select select-bordered w-full">
|
||||
{% for h in hours %}
|
||||
<option value="{{ h.v }}" {% if h.v == hour_start %}selected{% endif %}>{{ h.label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour-to", lang=lang) }}</span></label>
|
||||
<select name="hour_end" id="hour_end" class="select select-bordered w-full">
|
||||
{% for h in hours_end %}
|
||||
<option value="{{ h.v }}" {% if h.v == hour_end %}selected{% endif %}>{{ h.label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="repeat-weeks", lang=lang) }}</span></label>
|
||||
<input type="number" name="repeat_weeks" value="{{ repeat_weeks }}" min="1" max="52" required
|
||||
class="input input-bordered w-full">
|
||||
<label class="label">
|
||||
<span class="label-text-alt opacity-60">{{ t(key="repeat-hint", lang=lang) }}</span>
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour", lang=lang) }}</span></label>
|
||||
<select name="hour" class="select select-bordered w-full">
|
||||
@@ -32,6 +60,7 @@
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="booking-color", lang=lang) }}</span></label>
|
||||
<input type="color" name="color" value="{{ color }}"
|
||||
@@ -55,7 +84,7 @@
|
||||
</div>
|
||||
<div class="flex items-center gap-2 pt-2">
|
||||
<button class="btn btn-neutral">{{ t(key="save", lang=lang) }}</button>
|
||||
<a href="/admin" class="btn btn-ghost">{{ t(key="cancel", lang=lang) }}</a>
|
||||
<a href="/admin?court={{ court_id }}&week={{ date }}" class="btn btn-ghost">{{ t(key="cancel", lang=lang) }}</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -69,3 +98,21 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
{% block js %}
|
||||
{% if mode == "new" %}
|
||||
<script>
|
||||
// Keep the "until" time after the "from" time as the start hour changes.
|
||||
(function () {
|
||||
var s = document.getElementById('hour_start');
|
||||
var e = document.getElementById('hour_end');
|
||||
if (!s || !e) return;
|
||||
s.addEventListener('change', function () {
|
||||
if (parseInt(e.value, 10) <= parseInt(s.value, 10)) {
|
||||
e.value = String(parseInt(s.value, 10) + 1);
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock js %}
|
||||
|
||||
@@ -3,6 +3,24 @@
|
||||
{% block title %}{{ t(key="calendar-title", lang=lang) }}{% endblock title %}
|
||||
|
||||
{% block head %}
|
||||
<style>
|
||||
/* Sharpen the calendar grid. The daisyUI base tones give very low-contrast
|
||||
hairline borders and dimmed text in both themes. Tying the borders and
|
||||
the header tint to base-content keeps the table legible and easy to
|
||||
follow on light and dark alike (public + admin calendars). */
|
||||
#cal th,
|
||||
#cal td {
|
||||
border-color: hsl(var(--bc) / 0.25);
|
||||
}
|
||||
#cal thead tr,
|
||||
#cal tbody td:first-child {
|
||||
background-color: hsl(var(--bc) / 0.1);
|
||||
}
|
||||
#cal tbody td:first-child { opacity: 1; }
|
||||
#cal thead .opacity-50 { opacity: 0.8; }
|
||||
#cal td .opacity-30 { opacity: 0.7; }
|
||||
#cal td a.opacity-30:hover { opacity: 1; }
|
||||
</style>
|
||||
{% if is_admin %}
|
||||
<style>
|
||||
/* Admin calendar — detailed-view toggle. The public calendar is unaffected. */
|
||||
@@ -55,8 +73,12 @@
|
||||
<div class="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<div class="join">
|
||||
{% if can_prev %}
|
||||
<a href="{{ base_path }}?court={{ court_id }}&week={{ prev_week }}"
|
||||
class="btn btn-sm join-item">« {{ t(key="prev-week", lang=lang) }}</a>
|
||||
{% else %}
|
||||
<span class="btn btn-sm join-item btn-disabled" aria-disabled="true">« {{ t(key="prev-week", lang=lang) }}</span>
|
||||
{% endif %}
|
||||
<a href="{{ base_path }}?court={{ court_id }}&week={{ this_week }}"
|
||||
class="btn btn-sm join-item">{{ t(key="this-week", lang=lang) }}</a>
|
||||
<a href="{{ base_path }}?court={{ court_id }}&week={{ next_week }}"
|
||||
@@ -72,12 +94,27 @@
|
||||
</svg>
|
||||
{{ t(key="view-details", lang=lang) }}
|
||||
</button>
|
||||
<form method="get" action="{{ base_path }}" class="relative inline-flex">
|
||||
<input type="hidden" name="court" value="{{ court_id }}">
|
||||
<button type="button" id="cal-jump-btn" class="btn btn-sm gap-1"
|
||||
title="{{ t(key='pick-week', lang=lang) }}">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
|
||||
stroke="currentColor" class="h-4 w-4">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5A2.25 2.25 0 0 1 21 11.25v7.5" />
|
||||
</svg>
|
||||
{{ t(key="pick-week", lang=lang) }}
|
||||
</button>
|
||||
<input type="date" name="week" id="cal-jump" value="{{ week }}"
|
||||
onchange="this.form.submit()" tabindex="-1" aria-hidden="true"
|
||||
class="pointer-events-none absolute inset-0 -z-10 opacity-0">
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="text-sm font-medium opacity-60">{{ court_name }} · {{ week_label }}</div>
|
||||
</div>
|
||||
|
||||
<div class="overflow-x-auto rounded-lg border border-base-300 bg-base-100 shadow-sm">
|
||||
<div class="overflow-x-auto border border-base-300 bg-base-100 shadow-sm">
|
||||
<table id="cal" class="w-full border-collapse text-sm">
|
||||
<thead>
|
||||
<tr class="bg-base-200">
|
||||
@@ -92,6 +129,14 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in rows %}
|
||||
{% if row.free_group %}
|
||||
<tr>
|
||||
<td class="border border-base-300 bg-base-200 px-2 py-1 text-center text-xs font-medium">{{ row.hour_label }}</td>
|
||||
<td colspan="7" class="border border-base-300 px-2 py-1">
|
||||
<div class="text-center text-xs uppercase tracking-wide opacity-30">{{ t(key="free", lang=lang) }}</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td class="border border-base-300 bg-base-200 p-2 text-center font-medium opacity-70">{{ row.hour_label }}</td>
|
||||
{% for cell in row.cells %}
|
||||
@@ -129,6 +174,7 @@
|
||||
</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -163,5 +209,22 @@
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
<script>
|
||||
// "Go to week" — open the native date picker; picking any day loads the
|
||||
// week it belongs to. Admin only.
|
||||
(function () {
|
||||
var btn = document.getElementById('cal-jump-btn');
|
||||
var input = document.getElementById('cal-jump');
|
||||
if (!btn || !input) return;
|
||||
btn.addEventListener('click', function () {
|
||||
if (typeof input.showPicker === 'function') {
|
||||
input.showPicker();
|
||||
} else {
|
||||
input.focus();
|
||||
input.click();
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock js %}
|
||||
|
||||
@@ -256,6 +256,15 @@ fn hour_options() -> Vec<serde_json::Value> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// End-of-block hour options (07:00‥22:00) for the "until" select. A booking
|
||||
/// covers the hours in `[start, end)`, so the end runs one slot past the last
|
||||
/// bookable hour.
|
||||
fn end_hour_options() -> Vec<serde_json::Value> {
|
||||
(FIRST_HOUR + 1..=LAST_HOUR + 1)
|
||||
.map(|h| data!({ "v": h, "label": format!("{h:02}:00") }))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct NewBookingQuery {
|
||||
pub court: Option<i32>,
|
||||
@@ -286,6 +295,9 @@ pub async fn booking_new(
|
||||
.and_then(|c| c.name.clone())
|
||||
.unwrap_or_else(|| format!("Court {court_id}"));
|
||||
|
||||
let hour_start = q.hour.unwrap_or(FIRST_HOUR).clamp(FIRST_HOUR, LAST_HOUR);
|
||||
let hour_end = (hour_start + 1).min(LAST_HOUR + 1);
|
||||
|
||||
format::render().view(
|
||||
&v,
|
||||
"admin/booking_form.html",
|
||||
@@ -298,18 +310,22 @@ pub async fn booking_new(
|
||||
"court_id": court_id,
|
||||
"court_name": court_name,
|
||||
"date": q.date.unwrap_or_default(),
|
||||
"hour": q.hour.unwrap_or(FIRST_HOUR),
|
||||
"hour_start": hour_start,
|
||||
"hour_end": hour_end,
|
||||
"repeat_weeks": 1,
|
||||
"color": "#3b82f6",
|
||||
"name": "",
|
||||
"title": "",
|
||||
"contact": "",
|
||||
"note": "",
|
||||
"hours": hour_options(),
|
||||
"hours_end": end_hour_options(),
|
||||
"booking_id": 0,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Form fields for editing a single existing booking.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct BookingForm {
|
||||
pub court_id: i32,
|
||||
@@ -322,31 +338,86 @@ pub struct BookingForm {
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
/// Form fields for creating bookings. Unlike editing, creation books a range
|
||||
/// of hours (`[hour_start, hour_end)`) and can repeat the block weekly.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct BookingCreateForm {
|
||||
pub court_id: i32,
|
||||
pub date: String,
|
||||
pub hour_start: i32,
|
||||
pub hour_end: i32,
|
||||
pub repeat_weeks: Option<i32>,
|
||||
pub color: String,
|
||||
pub name: String,
|
||||
pub title: Option<String>,
|
||||
pub contact: Option<String>,
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
fn parse_date(s: &str) -> Result<chrono::NaiveDate> {
|
||||
chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d")
|
||||
.map_err(|_| Error::string("invalid date"))
|
||||
}
|
||||
|
||||
/// Creates one or more bookings from the admin form. The selected hour range
|
||||
/// expands into one row per hour, and — when `repeat_weeks > 1` — the whole
|
||||
/// block is duplicated on the same weekday for each following week. A slot
|
||||
/// that is already taken is skipped, so an overlap can never double-book.
|
||||
#[debug_handler]
|
||||
pub async fn booking_create(
|
||||
_auth: AdminAuth,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<BookingForm>,
|
||||
Form(form): Form<BookingCreateForm>,
|
||||
) -> Result<Response> {
|
||||
let date = parse_date(&form.date)?;
|
||||
let start_date = parse_date(&form.date)?;
|
||||
|
||||
// Normalise the hour block to `[hour_start, hour_end)`; a non-positive
|
||||
// span falls back to a single hour so the form cannot create nothing.
|
||||
let hour_start = form.hour_start.clamp(FIRST_HOUR, LAST_HOUR);
|
||||
let hour_end = form.hour_end.clamp(hour_start + 1, LAST_HOUR + 1);
|
||||
let weeks = form.repeat_weeks.unwrap_or(1).clamp(1, 52);
|
||||
|
||||
let title = form.title.filter(|s| !s.is_empty());
|
||||
let contact = form.contact.filter(|s| !s.is_empty());
|
||||
let note = form.note.filter(|s| !s.is_empty());
|
||||
|
||||
let last_date = start_date + chrono::Duration::weeks(i64::from(weeks - 1));
|
||||
|
||||
// Slots already booked on this court within the affected window, used to
|
||||
// skip conflicts rather than insert a duplicate.
|
||||
let taken: std::collections::HashSet<(chrono::NaiveDate, i32)> =
|
||||
bookings::Entity::find()
|
||||
.filter(bookings::Column::CourtId.eq(form.court_id))
|
||||
.filter(bookings::Column::Date.gte(start_date))
|
||||
.filter(bookings::Column::Date.lte(last_date))
|
||||
.all(&ctx.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|b| (b.date, b.hour))
|
||||
.collect();
|
||||
|
||||
for w in 0..weeks {
|
||||
let date = start_date + chrono::Duration::weeks(i64::from(w));
|
||||
for hour in hour_start..hour_end {
|
||||
if taken.contains(&(date, hour)) {
|
||||
continue;
|
||||
}
|
||||
bookings::ActiveModel {
|
||||
court_id: Set(form.court_id),
|
||||
date: Set(date),
|
||||
hour: Set(form.hour),
|
||||
color: Set(form.color),
|
||||
name: Set(form.name),
|
||||
title: Set(form.title.filter(|s| !s.is_empty())),
|
||||
contact: Set(form.contact.filter(|s| !s.is_empty())),
|
||||
note: Set(form.note.filter(|s| !s.is_empty())),
|
||||
hour: Set(hour),
|
||||
color: Set(form.color.clone()),
|
||||
name: Set(form.name.clone()),
|
||||
title: Set(title.clone()),
|
||||
contact: Set(contact.clone()),
|
||||
note: Set(note.clone()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Redirect::to(&format!("/admin?court={}&week={}", form.court_id, form.date)).into_response())
|
||||
}
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ pub struct Cell {
|
||||
pub struct Row {
|
||||
pub hour_label: String,
|
||||
pub cells: Vec<Cell>,
|
||||
/// Public view only: `true` when this row collapses a run of fully-free
|
||||
/// hours into one compact strip. The admin grid never sets it.
|
||||
pub free_group: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -83,6 +86,9 @@ pub struct CalendarPage {
|
||||
pub prev_week: String,
|
||||
pub this_week: String,
|
||||
pub next_week: String,
|
||||
/// `false` on the public calendar once it reaches the two-week look-back
|
||||
/// limit, which disables the "previous week" button. Always `true` for admin.
|
||||
pub can_prev: bool,
|
||||
pub days: Vec<DayHead>,
|
||||
pub rows: Vec<Row>,
|
||||
}
|
||||
@@ -107,6 +113,46 @@ fn week_monday(week: Option<&str>) -> NaiveDate {
|
||||
monday_of(base)
|
||||
}
|
||||
|
||||
/// Collapses runs of fully-free hour rows into one compact strip so a court
|
||||
/// that isn't booked solid doesn't waste vertical space. Rows holding at
|
||||
/// least one booking are kept full-size so bookings stay easy to read.
|
||||
/// Public calendar only — the admin grid always shows every hour.
|
||||
fn group_free_rows(rows: Vec<Row>) -> Vec<Row> {
|
||||
let mut out: Vec<Row> = Vec::new();
|
||||
let mut run: Vec<Row> = Vec::new();
|
||||
for row in rows {
|
||||
if row.cells.iter().all(|c| !c.booked) {
|
||||
run.push(row);
|
||||
} else {
|
||||
flush_free_run(&mut run, &mut out);
|
||||
out.push(row);
|
||||
}
|
||||
}
|
||||
flush_free_run(&mut run, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// Drains a pending run of free rows into `out` as a single collapsed row.
|
||||
fn flush_free_run(run: &mut Vec<Row>, out: &mut Vec<Row>) {
|
||||
if run.is_empty() {
|
||||
return;
|
||||
}
|
||||
let hour_of = |r: &Row| r.cells.first().map_or(FIRST_HOUR, |c| c.hour);
|
||||
let first = run.first().map_or(FIRST_HOUR, hour_of);
|
||||
let last = run.last().map_or(first, hour_of);
|
||||
let hour_label = if run.len() > 1 {
|
||||
format!("{first:02}:00 – {:02}:00", last + 1)
|
||||
} else {
|
||||
format!("{first:02}:00")
|
||||
};
|
||||
out.push(Row {
|
||||
hour_label,
|
||||
cells: Vec::new(),
|
||||
free_group: true,
|
||||
});
|
||||
run.clear();
|
||||
}
|
||||
|
||||
/// Builds the calendar grid for the selected court and week.
|
||||
pub async fn build_calendar(
|
||||
ctx: &AppContext,
|
||||
@@ -142,6 +188,10 @@ pub async fn build_calendar(
|
||||
.unwrap_or_default();
|
||||
|
||||
let monday = week_monday(q_week.as_deref());
|
||||
// The public calendar may look back at most two weeks; the admin has no limit.
|
||||
let min_monday = monday_of(Utc::now().date_naive()) - Duration::weeks(2);
|
||||
let monday = if is_admin { monday } else { monday.max(min_monday) };
|
||||
let can_prev = is_admin || monday > min_monday;
|
||||
let sunday = monday + Duration::days(6);
|
||||
|
||||
let days: Vec<DayHead> = (0..7i64)
|
||||
@@ -201,10 +251,15 @@ pub async fn build_calendar(
|
||||
Row {
|
||||
hour_label: format!("{hour:02}:00"),
|
||||
cells,
|
||||
free_group: false,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// The public calendar collapses empty stretches to stay compact; the admin
|
||||
// grid always shows every hour so each slot stays individually editable.
|
||||
let rows = if is_admin { rows } else { group_free_rows(rows) };
|
||||
|
||||
Ok(CalendarPage {
|
||||
lang: lang.to_string(),
|
||||
is_admin,
|
||||
@@ -221,6 +276,7 @@ pub async fn build_calendar(
|
||||
.format("%Y-%m-%d")
|
||||
.to_string(),
|
||||
next_week: (monday + Duration::days(7)).format("%Y-%m-%d").to_string(),
|
||||
can_prev,
|
||||
days,
|
||||
rows,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user