From fbee775f5b698384017758e6e80d80d885c3382e Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Sun, 14 Jan 2024 12:29:39 +0000
Subject: [PATCH] Next trip and previous trip links on trip pages

Closes: #110
---
 templates/macros.html    |  4 ++++
 templates/trip_page.html |  7 ++++++-
 web_view.py              | 11 +++++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/templates/macros.html b/templates/macros.html
index f7926e6..cd331cd 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -3,6 +3,10 @@
 {% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %}
 {% macro display_date_no_year(dt) %}{{ dt.strftime("%a %-d %b") }}{% endmacro %}
 
+{% macro trip_link(trip) %}
+    <a href="{{ url_for("trip_page", start=trip.start.isoformat()) }}">{{ trip.title }}</a>
+{% endmacro %}
+
 {% macro conference_row(item, badge) %}
   {% set country = get_country(item.country) if item.country else None %}
   <div class="grid-item text-end">{{ item.start.strftime("%a, %d %b %Y") }}</div>
diff --git a/templates/trip_page.html b/templates/trip_page.html
index a4157a6..45d4ec1 100644
--- a/templates/trip_page.html
+++ b/templates/trip_page.html
@@ -1,6 +1,6 @@
 {% extends "base.html" %}
 
-{% from "macros.html" import display_date_no_year, display_date, conference_row, accommodation_row, flight_row, train_row with context %}
+{% from "macros.html" import trip_link, display_date_no_year, display_date, conference_row, accommodation_row, flight_row, train_row with context %}
 
 {% set row = { "flight": flight_row, "train": train_row } %}
 
@@ -77,6 +77,11 @@
   {% endfor %}
   </div>
 
+  <p>
+    {% if prev_trip %}previous: {{ trip_link(prev_trip) }}{% endif %}
+    {% if next_trip %}next: {{ trip_link(next_trip) }}{% endif %}
+  </p>
+
   {% if coordinates %}
   <div id="map"></div>
   {% endif %}
diff --git a/web_view.py b/web_view.py
index 2f17afb..0c57977 100755
--- a/web_view.py
+++ b/web_view.py
@@ -191,10 +191,15 @@ def trip_list() -> str:
 @app.route("/trip/<start>")
 def trip_page(start: str) -> str:
     """Individual trip page."""
-    trip_list = agenda.trip.build_trip_list()
+    trip_iter = iter(agenda.trip.build_trip_list())
     today = date.today()
 
-    trip = next((trip for trip in trip_list if trip.start.isoformat() == start), None)
+    prev_trip = None
+    for trip in trip_iter:
+        if trip.start.isoformat() == start:
+            break
+        prev_trip = trip
+    next_trip = next(trip_iter, None)
 
     if not trip:
         flask.abort(404)
@@ -208,6 +213,8 @@ def trip_page(start: str) -> str:
     return flask.render_template(
         "trip_page.html",
         trip=trip,
+        prev_trip=prev_trip,
+        next_trip=next_trip,
         today=today,
         coordinates=coordinates,
         routes=routes,