From a1dc92defd879a3251ce8abcca43f9e6bba88304 Mon Sep 17 00:00:00 2001 From: PhilippHofer Date: Tue, 5 Sep 2023 20:48:02 +0000 Subject: [PATCH] Refactor code --- frontend/logbook.ts | 33 ++ frontend/main.ts | 40 ++ frontend/vite.config.js | 1 + src/model/logbook.rs | 1 + src/tera/log.rs | 5 +- templates/forms/event.html.tera | 2 +- templates/forms/trip.html.tera | 2 +- templates/includes/forms/boat.html.tera | 8 +- templates/includes/forms/log.html.tera | 499 +++++++++++------------- templates/includes/macros.html.tera | 4 +- templates/index.html.tera | 2 +- templates/kiosk.html.tera | 2 + templates/log.html.tera | 57 +-- 13 files changed, 346 insertions(+), 310 deletions(-) create mode 100644 frontend/logbook.ts diff --git a/frontend/logbook.ts b/frontend/logbook.ts new file mode 100644 index 0000000..26c5a18 --- /dev/null +++ b/frontend/logbook.ts @@ -0,0 +1,33 @@ +/*document.addEventListener('DOMContentLoaded', function() { + setDistance('.set-distance-js'); +}); + +function setDistance(selector: string) { + const fields = document.querySelectorAll(selector); + if(fields) { + Array.prototype.forEach.call(fields, (field: HTMLInputElement) => { + if(field.dataset.relation){ + const relatedField = document.getElementById(field.dataset.relation); + + if(relatedField) { + field.addEventListener('input', (e) => { + e.preventDefault(); + + const dataList = document.getElementById('destinations'); + if(dataList) { + var option = Array.prototype.find.call(dataList.options, function(option) { + return option.value === field.value; + }); + + // Get distance + const distance = option.getAttribute('distance'); + if(distance) relatedField.value = distance; + } + }); + } + } + }); + } +} +*/ +export {} diff --git a/frontend/main.ts b/frontend/main.ts index 95adeee..d78c41e 100644 --- a/frontend/main.ts +++ b/frontend/main.ts @@ -202,9 +202,22 @@ function initTripSidebar(triggerElement: HTMLElement) { } }); + const prefixedContent = >bodyElement.querySelectorAll('.change-id-js'); + Array.prototype.forEach.call(prefixedContent, (content: HTMLElement) => { + if(content) { + content.id += 'js'; + + if(content.dataset.relation){ + content.dataset.relation += 'js'; + } + } + }); + if(bodyContainerElement) { bodyContainerElement.innerHTML = ''; bodyContainerElement.append(bodyElement); + + addRelationMagic(bodyElement); } if(triggerElement.dataset.day) { let hiddenElement = bodyElement.querySelector('.day-js'); @@ -220,6 +233,33 @@ function initTripSidebar(triggerElement: HTMLElement) { } } +function addRelationMagic(bodyElement: HTMLElement) { + const fields = bodyElement.querySelectorAll('.set-distance-js'); + if(fields) { + Array.prototype.forEach.call(fields, (field: HTMLInputElement) => { + if(field.dataset.relation){ + const relatedField = bodyElement.querySelector('#' + field.dataset.relation); + + if(relatedField) { + field.addEventListener('input', (e) => { + e.preventDefault(); + const dataList = document.getElementById('destinations'); + if(dataList) { + var option = Array.prototype.find.call(dataList.options, function(option) { + return option.value === field.value; + }); + + // Get distance + const distance = option.getAttribute('distance'); + if(distance) relatedField.value = distance; + } + }); + } + } + }); + } +} + function replaceStrings() { const weekdays = document.querySelectorAll('.weekday-js'); Array.prototype.forEach.call(weekdays, (weekday: HTMLElement) => { diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 1c99ca6..8ccb8a1 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -17,6 +17,7 @@ export default defineConfig({ rollupOptions: { input: { main: './main.ts', + logbook: './logbook.ts', // Example for more entry points // test: './src/test.ts', }, diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 1ea7cbc..10b814c 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -204,6 +204,7 @@ ORDER BY departure DESC let mut tx = db.begin().await.unwrap(); let departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap(); + println!("@@@@@@ {:?}", log.arrival); let arrival = log .arrival .map(|a| NaiveDateTime::parse_from_str(&a, "%Y-%m-%dT%H:%M").unwrap()); diff --git a/src/tera/log.rs b/src/tera/log.rs index 9d65dd7..7eb3c2f 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -341,8 +341,9 @@ mod test { let response = req.dispatch().await; let text = response.into_string().await.unwrap(); + println!("{text:?}"); assert!(text.contains("Logbuch")); - assert!(text.contains("Bootsname: Joe")); + assert!(text.contains("Joe")); } #[sqlx::test] @@ -361,7 +362,7 @@ mod test { let text = response.into_string().await.unwrap(); assert!(text.contains("Logbuch")); - assert!(text.contains("Bootsname: Joe")); + assert!(text.contains("Joe")); } #[sqlx::test] diff --git a/templates/forms/event.html.tera b/templates/forms/event.html.tera index 4f2e6c2..1144eab 100644 --- a/templates/forms/event.html.tera +++ b/templates/forms/event.html.tera @@ -10,7 +10,7 @@ {{ macros::checkbox(label='Gäste erlauben', name='tripdetails.allow_guests') }} {{ macros::checkbox(label='Immer anzeigen', name='tripdetails.always_show') }} {{ macros::input(label='Anmerkungen', name='tripdetails.notes', type='input') }} - {{ macros::select(data=trip_types, select_name='tripdetails.trip_type', default='Reguläre Ausfahrt') }} + {{ macros::select(data=trip_types, name='tripdetails.trip_type', default='Reguläre Ausfahrt') }} diff --git a/templates/forms/trip.html.tera b/templates/forms/trip.html.tera index d38d9ab..a8d1405 100644 --- a/templates/forms/trip.html.tera +++ b/templates/forms/trip.html.tera @@ -8,7 +8,7 @@ {{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }} {{ macros::checkbox(label='Immer anzeigen', name='always_show') }} {{ macros::input(label='Anmerkungen', name='notes', type='input') }} - {{ macros::select(data=trip_types, select_name='trip_type', default='Reguläre Ausfahrt') }} + {{ macros::select(data=trip_types, name='trip_type', default='Reguläre Ausfahrt') }} diff --git a/templates/includes/forms/boat.html.tera b/templates/includes/forms/boat.html.tera index f29e633..4fee26b 100644 --- a/templates/includes/forms/boat.html.tera +++ b/templates/includes/forms/boat.html.tera @@ -8,8 +8,8 @@ {{ macros::input(label="Anzahl Sitze", name="amount_seats", type="number", required=true, min=1) }} {{ macros::input(label="Baujahr", name="year_built", type="number", min=1950, max=2050) }} {{ macros::input(label="Bootsbauer", name="boatbuilder", type="text") }} - {{ macros::select(data=locations, label='location', select_name='location_id', selected_id=1) }} - {{ macros::select(data=users, label='users', select_name='owner', default="Vereinsboot") }} + {{ macros::select(data=locations, label='location', name='location_id', selected_id=1) }} + {{ macros::select(data=users, label='users', name='owner', default="Vereinsboot") }} {{ macros::checkbox(label="Steuerperson steuert nur", name="default_shipmaster_only_steering")}} {{ macros::checkbox(label="Skull", name="skull", checked=true)}} {{ macros::checkbox(label="Externes Boot (anderer Verein)", name="external")}} @@ -29,8 +29,8 @@
{{ macros::input(label='Name', name='name', type='text', value=boat.name) }} {{ macros::input(label='Amount Seats', name='amount_seats', type='number', min=0, value=boat.amount_seats) }} - {{ macros::select(data=locations, label='location', select_name='location_id', selected_id=boat.location_id) }} - {{ macros::select(data=users, label='users', select_name='owner', selected_id=boat.owner, default="Vereinsboot") }} + {{ macros::select(data=locations, label='location', name='location_id', selected_id=boat.location_id) }} + {{ macros::select(data=users, label='users', name='owner', selected_id=boat.owner, default="Vereinsboot") }} {{ macros::input(label='Baujahr', name='year_built', type='number', min=1950, value=boat.year_built) }} {{ macros::input(label='Bootsbauer', name='boatbuilder', type='text', value=boat.boatbuilder) }} {{ macros::checkbox(label='default_shipmaster_only_steering', name='default_shipmaster_only_steering', id=uuid , checked=boat.default_shipmaster_only_steering) }} diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index 74d5706..5de655c 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -1,297 +1,252 @@ +{# Shows a fancy, optional lists of boats. They are grouped by boat category. + + Inputs: boats + Parameters: only_ones: if set, only 1x boats are shown + #} +{% macro show_boats(only_ones) %} + {% if only_ones %} + {% set_global boats = boats | filter(attribute="amount_seats", value=1) %} + {% endif %} + {% for amount_seats, grouped_boats in boats | group_by(attribute="amount_seats") %} +
+
+ {{ amount_seats }}x +
+ {% for boat in grouped_boats %} +
+ + {{ boat.name }} + {% if boat.owner %} + (privat) + {% endif %} + +
+ {% endfor %} +
+ {% endfor %} +{% endmacro show_boats %} + +{# Shows the form for creating a new logbook entry. #} {% macro new(only_ones, allow_any_shipmaster, shipmaster) %} -
- {{ log::boat_select(only_ones=only_ones) }} + + {{ log::boat_select(only_ones=only_ones) }} - {% if allow_any_shipmaster %} - - {% else %} - - {% endif %} - {% if not only_ones %} - {{ macros::checkbox(label='handgesteuert', name='shipmaster_only_steering') }} - {% endif %} - - {% if not only_ones %} - {{ log::rower_select(id="newrower", selected=[], class="col-span-2") }} - {% endif %} + {% if allow_any_shipmaster %} + + {% else %} + + {% endif %} + {% if not only_ones %} + {{ macros::checkbox(label='handgesteuert', name='shipmaster_only_steering') }} + {% endif %} -
+ {% if not only_ones %} + {{ log::rower_select(id="newrower", selected=[], class="col-span-2") }} + {% endif %} - {{ macros::input(label='Abfahrtszeit', name='departure', type='datetime-local', required=true) }} - {{ macros::input(label='Ankunftszeit', name='arrival', type='datetime-local') }} +
-
- - - - {% for distance in distances %} - -
+
+ + + + {% for distance in distances %} + +
+ {{ macros::input(label='Abfahrtszeit', name='departure', type='datetime-local', required=true) }} + {{ macros::input(label='Ankunftszeit', name='arrival', type='datetime-local') }} -
- {{ macros::input(label="Distanz", name="distance_in_km", type="number", min=1) }} - km -
- {{ macros::input(label="Kommentar", name="comments", type="text", wrapper_class="col-span-2") }} +
+ + + + {% for distance in distances %} + +
-
- - {{ macros::select(data=logtypes, select_name='logtype', default="Normal") }} -
- - - -
- - - +
+ + {{ macros::select(data=logtypes, name='logtype', default="Normal") }} +
+ + {% endmacro new %} -{% macro show_boats(only_ones) %} - {% if only_ones %} - {% set_global boats = boats | filter(attribute="amount_seats", value=1) %} - {% endif %} - {% for amount_seats, grouped_boats in boats | group_by(attribute="amount_seats") %} -
-
- {{ amount_seats }}x -
- {% for boat in grouped_boats %} -
- - {{ boat.name }} -
- {% endfor %} -
- {% endfor %} - - -{% endmacro boats %} {% macro boat_select(only_ones) %} - {% if not only_ones %} - {{ macros::select(data=boats, select_name='boat_id', display=["name", " (","amount_seats", " x)"], extras=["default_shipmaster_only_steering", "amount_seats"], class="col-span-2") }} - {% else %} - {% set ones = boats | filter(attribute="amount_seats", value=1) %} - {{ macros::select(data=ones, select_name='boat_id', display=["name", " (","amount_seats", " x)"], extras=["default_shipmaster_only_steering", "amount_seats"], class="col-span-2") }} - {% endif %} - + {% if not only_ones %} + {{ macros::select(data=boats, name='boat_id', display=["name", " (","amount_seats", " x)"], extras=["default_shipmaster_only_steering", "amount_seats"], class="col-span-2") }} + {% else %} + {% set ones = boats | filter(attribute="amount_seats", value=1) %} + {{ macros::select(data=ones, name='boat_id', display=["name", " (","amount_seats", " x)"], extras=["default_shipmaster_only_steering", "amount_seats"], class="col-span-2") }} + {% endif %} {% endmacro boat_select %} +{% macro rower_select(id, selected, amount_seats='', class='') %} +
+ +
+
+ + {{amount_seats}} + Ruderer auszuwählen +
+{% endmacro rower_select %} + {% macro show(log, state, allowed_to_close=false, only_ones) %} -
-
-
-
-
- - {{ log.departure_timestamp | date(format="%H:%M") }} Uhr - - ({{ log.boat.name }}) -
- - ({{ log.boat.name }})" data-body="#log{{ log.id }}" class="inline-block link-primary mr-3"> - Details - -
- -
- {% if allowed_to_close and state == "on_water" %} - ({{ log.boat.name }})" data-body="#close{{ log.id }}" class="btn btn-dark w-full mt-3"> - Beenden - - {% endif %} -
+
+
+
+
+
+ + {{ log.departure_timestamp | date(format="%H:%M") }} + Uhr + + ({{ log.boat.name }}) +
- -
-
-
-
-{% endmacro show %} +
+ {% if allowed_to_close and state == "on_water" %} + ({{ log.boat.name }})" data-body="#close{{ log.id }}" class="btn btn-dark w-full mt-3"> + Beenden + + {% endif %} +
+ + +
+
+
+{% endmacro show %} {% macro show_old(log, state, allowed_to_close=false, only_ones) %} - Bootsname: {{ log.boat.name }}
- Schiffsführer: {{ log.shipmaster_user.name }}
- {% if log.shipmaster_only_steering %} - Schiffsführer steuert nur - {% endif %} - Weggefahren: {{ log.departure }}
- {% if state == "completed" %} - Angekommen: {{ log.arrival}}
- {% endif %} - {% set amount_rowers = log.rowers | length %} - {% set amount_guests = log.boat.amount_seats - amount_rowers -1 %} - {% if allowed_to_close and state == "on_water" %} - {{ log::home(log=log, only_ones=only_ones) }} - {% else %} - Ziel: {{ log.destination }}
- {% if state == "completed" %} - Km: {{ log.distance_in_km }}
- {% endif %} - {% if log.comments %} - Kommentare: {{ log.comments }}
- {% endif %} - {% if log.logtype %} - Logtype: {{ log.logtype }}
- {% endif %} - {% if amount_guests > 0 or log.rowers | length > 0 %} - Ruderer: - {% endif %} - {% if amount_guests > 0 %} - {{ amount_guests }} Gäste (ohne Account) - {% endif %} - {% for rower in log.rowers %} - {{ rower.name }} - {% endfor %} - {% endif %} -{% endmacro show_old %} + Bootsname: + {{ log.boat.name }}
+ Schiffsführer: + {{ log.shipmaster_user.name }}
+ {% if log.shipmaster_only_steering %} + Schiffsführer steuert nur + {% endif %} + Weggefahren: + {{ log.departure }}
+ {% if state == "completed" %} + Angekommen: + {{ log.arrival}}
+ {% endif %} + {% set amount_rowers = log.rowers | length %} + {% set amount_guests = log.boat.amount_seats - amount_rowers -1 %} + {% if allowed_to_close and state == "on_water" %} + {{ log::home(log=log, only_ones=only_ones) }} + {% else %} + Ziel: + {{ log.destination }}
+ {% if state == "completed" %} + Km: + {{ log.distance_in_km }}
+ {% endif %} + {% if log.comments %} + Kommentare: + {{ log.comments }}
+ {% endif %} + {% if log.logtype %} + Logtype: + {{ log.logtype }}
+ {% endif %} + {% if amount_guests > 0 or log.rowers | length > 0 %} + Ruderer: + {% endif %} + {% if amount_guests > 0 %} + {{ amount_guests }} + Gäste (ohne Account) + {% endif %} + {% for rower in log.rowers %} + {{ rower.name }} + {% endfor %} + {% endif %} +{% endmacro show_old %} {% macro home(log, only_ones) %} -
- {% if not only_ones %} - - {% endif %} -
- - -
+ + {% if not only_ones %} + {{ log::rower_select(id="rowers"~log.id, selected=log.rowers, amount_seats=log.boat.amount_seats) }} + {% endif %} +
+ -
- {{ macros::input(label="Distanz", name="distance_in_km", id="distance_in_km_home", type="number", min=0, value=log.distance_in_km, required=true) }} - km -
- - {{ macros::input(label="Kommentar", name="comments", type="text", value=log.comments) }} + +
-
- - {{ macros::select(data=logtypes, select_name='logtype', default="Normal", selected_id=log.logtype) }} -
- -
-{% endmacro home %} +
+ {{ macros::input(label="Distanz", name="distance_in_km", id="distance_in_km" ~ log.id , type="number", min=0, value=log.distance_in_km, required=true, class="rounded-md change-id-js") }} + km +
+ {{ macros::input(label="Kommentar", name="comments", id="comments" ~ log.id, type="text", value=log.comments, class="rounded-md change-id-js") }} -{% macro rower_select(id, selected, amount_seats='', class='') %} -
- - -
- -
- /{{amount_seats}} Ruderer ausgewählt -
-{% endmacro rower_select %} +
+ + {{ macros::select(data=logtypes, name="logtype", id="logtype" ~ log.id, default="Normal", selected_id=log.logtype, class="rounded-md change-id-js") }} +
+ + +{% endmacro home %} diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index de64ad4..585be2c 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -74,11 +74,11 @@ {% endmacro checkbox %} -{% macro select(data, select_name='trip_type', default='', selected_id='', display='', extras='', class='') %} +{% macro select(data, name='trip_type', default='', id='', selected_id='', display='', extras='', class='') %} {% if display == '' %} {% set display = ["name"] %} {% endif %} - {% if default %} {% endif %} diff --git a/templates/index.html.tera b/templates/index.html.tera index 5a5bd2c..4645ceb 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -239,7 +239,7 @@ {{ macros::input(label='Anmerkungen', name='notes', type='input', value=trip.notes) }} {{ macros::checkbox(label='Immer anzeigen', name='always_show', id=trip.id,checked=trip.always_show) }} {{ macros::checkbox(label='Gesperrt', name='is_locked', id=trip.id,checked=trip.is_locked) }} - {{ macros::select(select_name='trip_type', data=trip_types, default='Reguläre Ausfahrt', selected_id=trip.trip_type_id) }} + {{ macros::select(name='trip_type', data=trip_types, default='Reguläre Ausfahrt', selected_id=trip.trip_type_id) }} diff --git a/templates/kiosk.html.tera b/templates/kiosk.html.tera index 96f3f7a..8150a6d 100644 --- a/templates/kiosk.html.tera +++ b/templates/kiosk.html.tera @@ -39,4 +39,6 @@
{% include "dynamics/sidebar" %} + + {% endblock content%} diff --git a/templates/log.html.tera b/templates/log.html.tera index 045d169..90e7d3f 100644 --- a/templates/log.html.tera +++ b/templates/log.html.tera @@ -11,33 +11,36 @@

Logbuch

-
- -
-

Neue Ausfahrt

-
- {{ log::new(only_ones=loggedin_user.is_cox==false, allow_any_shipmaster=loggedin_user.is_cox, shipmaster=loggedin_user.id) }} -
-
-
-

Am Wasser

+
+ -
-
+
+ {{ log::show_boats(only_ones=false) }} +
+
+
+

Neue Ausfahrt

+
+ {{ log::new(only_ones=loggedin_user.is_cox==false, allow_any_shipmaster=loggedin_user.is_cox, shipmaster=loggedin_user.id) }} +
+
+
+

Am Wasser

-{% include "dynamics/sidebar" %} + {% for log in on_water %} + {% if log.shipmaster == loggedin_user.id %} + {{ log::show(log=log, state="on_water", allowed_to_close=true, only_ones=loggedin_user.is_cox==false) }} + {% else %} + {{ log::show(log=log, state="on_water", only_ones=true) }} + {% endif %} + {% endfor %} +
+
+ + + {% include "dynamics/sidebar" %} + + + {% endblock content%}