From fea45d5257196d8f9d57d4851d01e107ae563f7a Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 24 Sep 2023 22:17:28 +0200 Subject: [PATCH] fix multi-select; only show private boats owner + kiosk --- frontend/main.ts | 43 +++++++++++++++++++++++------ src/model/boat.rs | 23 +++++++++++++++ src/tera/log.rs | 2 +- templates/includes/macros.html.tera | 2 +- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/frontend/main.ts b/frontend/main.ts index a3a5644..949f557 100644 --- a/frontend/main.ts +++ b/frontend/main.ts @@ -39,9 +39,9 @@ function selectBoatChange() { )[0]; const rowers = Number(originalOption.dataset.amount_seats) - 1; - choiceObjects['newrower'].config.maxItemCount = rowers; - choiceObjects['newrower'].removeActiveItems(rowers); - (rowers === 0 ? choiceObjects['newrower'].disable() : choiceObjects['newrower'].enable()); + + setMaxAmountRowers(rowers); + return opt; } @@ -62,6 +62,27 @@ function selectBoatChange() { choiceObjects[shipmasterSelect.id] = shipmasterChoice; } +function setMaxAmountRowers(rowers){ + let curSelection = choiceObjects['newrower'].getValue(true); + let amount_to_delete = curSelection.length - rowers; + if (amount_to_delete > 0){ + let to_delete = curSelection.slice(-amount_to_delete); + for (let del of to_delete) { + choiceObjects['newrower'].removeActiveItemsByValue(del); + } + } + + choiceObjects['newrower'].config.maxItemCount = rowers; + const only_steering = document.querySelector('#shipmaster_only_steering'); + if (rowers === 0) { + choiceObjects['newrower'].disable() + only_steering.disabled = true; + }else{ + choiceObjects['newrower'].enable() + only_steering.disabled = false; + } +} + function initBoatActions() { const boatSelects = document.querySelectorAll('.boats-js[data-onclick="true"]'); if(boatSelects) { @@ -69,14 +90,20 @@ function initBoatActions() { select.addEventListener('click', function() { if(select.dataset.seats) { const rowers = Number(select.dataset.seats) - 1; - choiceObjects['newrower'].config.maxItemCount = rowers; - choiceObjects['newrower'].removeActiveItems(rowers); + + setMaxAmountRowers(rowers); if (select.dataset.id) { choiceObjects['boat_id'].setChoiceByValue(select.dataset.id); } - - (rowers === 0 ? choiceObjects['newrower'].disable() : choiceObjects['newrower'].enable()); + + window.scrollTo(0, 0); + + const inputElement = document.getElementById("departure") as HTMLInputElement; + const now = new Date(); + const formattedDateTime = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}T${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`; + + inputElement.value = formattedDateTime; } }); }); @@ -100,7 +127,7 @@ function initNewChoice(select: HTMLInputElement) { noChoicesText: 'Keine Ergebnisse gefunden', itemSelectText: 'Zum Auswählen klicken', placeholderValue: 'Ruderer auswählen', - maxItemCount: Number(select.dataset.seats) - 1, + maxItemCount: -1, //TODO maxItemText: (maxItemCount) => { return `Nur ${maxItemCount} Ruderer können hinzugefügt werden`; }, diff --git a/src/model/boat.rs b/src/model/boat.rs index ddd02fb..baf21f7 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -2,6 +2,8 @@ use rocket::serde::{Deserialize, Serialize}; use rocket::FromForm; use sqlx::{FromRow, SqlitePool}; +use super::user::User; + #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct Boat { pub id: i64, @@ -130,6 +132,27 @@ ORDER BY amount_seats DESC Self::boats_to_details(db, boats).await } + pub async fn for_user(db: &SqlitePool, user: &User) -> Vec { + if user.is_admin { + return Self::all(db).await; + } + let boats = sqlx::query_as!( + Boat, + " +SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external +FROM boat +WHERE owner is null or owner = ? +ORDER BY amount_seats DESC + ", + user.id + ) + .fetch_all(db) + .await + .unwrap(); //TODO: fixme + + Self::boats_to_details(db, boats).await + } + pub async fn all_at_location(db: &SqlitePool, location: String) -> Vec { let boats = sqlx::query_as!( Boat, diff --git a/src/tera/log.rs b/src/tera/log.rs index 33e2060..6dabec0 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -43,7 +43,7 @@ async fn index( flash: Option>, adminuser: AdminUser, ) -> Template { - let boats = Boat::all(db).await; + let boats = Boat::for_user(db, &adminuser.user).await; let coxes: Vec = futures::future::join_all( User::cox(db) diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 20d48f6..72cc855 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -83,7 +83,7 @@ {% endif %} {% for d in data %} -