fix multi-select; only show private boats owner + kiosk

This commit is contained in:
philipp 2023-09-24 22:17:28 +02:00
parent ce73fd8902
commit 09af9981df
4 changed files with 60 additions and 10 deletions

View File

@ -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 = <HTMLSelectElement>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`;
},

View File

@ -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<BoatWithDetails> {
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<BoatWithDetails> {
let boats = sqlx::query_as!(
Boat,

View File

@ -43,7 +43,7 @@ async fn index(
flash: Option<FlashMessage<'_>>,
adminuser: AdminUser,
) -> Template {
let boats = Boat::all(db).await;
let boats = Boat::for_user(db, &adminuser.user).await;
let coxes: Vec<UserWithWaterStatus> = futures::future::join_all(
User::cox(db)

View File

@ -83,7 +83,7 @@
<option selected value>{{ default }}</option>
{% endif %}
{% for d in data %}
<option value="{{ d.id }}" {% if d.id == selected_id %} selected {% endif %} {% if extras != '' %} {% for extra in extras %} {% if extra != 'on_water' %} data- {{extra}}={{d[extra]}} {% else %} {% if d[extra] %} disabled {% endif %} {% endif %} {% endfor %} {% endif %}>
<option value="{{ d.id }}" {% if d.id == selected_id %} selected {% endif %} {% if extras != '' %} {% for extra in extras %} {% if extra != 'on_water' %} data-{{extra}}={{d[extra]}} {% else %} {% if d[extra] %} disabled {% endif %} {% endif %} {% endfor %} {% endif %}>
{% for displa in display -%}
{%- if d[displa] -%}
{{- d[displa] -}}