From 6571d053e0e7488635b890193c4cb4fbc4bb137f Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 5 Jul 2025 23:47:25 +0200 Subject: [PATCH] Improve datastore about cached trip details --- web/datastore.js | 55 +++++++++++++++++++++++++++++++++++++++++- web/drafting-board.js | 9 +++++++ web/journeys-search.js | 2 ++ web/traveldrafter.js | 2 ++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/web/datastore.js b/web/datastore.js index 97cab74..35234e5 100644 --- a/web/datastore.js +++ b/web/datastore.js @@ -7,6 +7,16 @@ export class LocationsDataStore { LocationsDataStore.set(locations); } + static rememberAllIfNotExist(location_list) { + let locations = LocationsDataStore.get(); + for (let location of location_list) { + if (!(location.id in locations)) { + locations[location.id] = location; + } + } + LocationsDataStore.set(locations); + } + static get() { return DataStore.get("locations") || {}; } @@ -16,7 +26,6 @@ export class LocationsDataStore { } } - export class RecentLocationsDataStore { static remember(location_id) { let recent_locations = RecentLocationsDataStore.get(); @@ -36,6 +45,48 @@ export class RecentLocationsDataStore { } } +export class TrackedTripsDataStore { + static remember(trip_id, origin_id, destination_id) { + let tracked_trips = TrackedTripsDataStore.get(); + tracked_trips.push({ + trip: trip_id, + origin: origin_id, + destination: destination_id, + }); + TrackedTripsDataStore.set(tracked_trips); + } + + static get() { + return DataStore.get("tracked-trips") || []; + } + + static set(value) { + DataStore.set("tracked-trips", value); + } +} + +export class TripsDataStore { + static remember(trip) { + TripsDataStore.rememberAll([trip]); + } + + static rememberAll(trip_list) { + let tracked_trips = TripsDataStore.get(); + for (let trip of trip_list) { + tracked_trips[trip.id] = trip; + } + TripsDataStore.set(tracked_trips); + } + + static get() { + return DataStore.get("trips") || {}; + } + + static set(value) { + DataStore.set("trips", value); + } +} + export class DataStore { static get(key) { return JSON.parse(window.localStorage.getItem(key)); @@ -47,4 +98,6 @@ export class DataStore { static locations = LocationsDataStore; static recent_locations = RecentLocationsDataStore; + static tracked_trips = TrackedTripsDataStore; + static trips = TripsDataStore; } diff --git a/web/drafting-board.js b/web/drafting-board.js index 4d97703..3a33f34 100644 --- a/web/drafting-board.js +++ b/web/drafting-board.js @@ -1,11 +1,20 @@ import { EL } from "./dom.js"; import { displayTripDetails } from "./trip-details.js"; +import { fetchTrip } from './api.js'; +import { DataStore } from './datastore.js'; let element_board = document.querySelector("#drafting-board-content"); export function addJourneyToDraftingBoard(journey) { element_board.innerText = ""; + for (let leg of journey.legs.filter(item => !("walking" in item))) { + DataStore.tracked_trips.remember(leg.tripId, leg.origin.id, leg.destination.id); + fetchTrip(leg.tripId).then(result => { + DataStore.trips.remember(result.trip); + }); + } + let station_offset = 0; let trip_offset = 1; diff --git a/web/journeys-search.js b/web/journeys-search.js index 0556753..5572f3f 100644 --- a/web/journeys-search.js +++ b/web/journeys-search.js @@ -1,6 +1,7 @@ import { fetchJourneys } from './api.js'; import { attachLocationsSearch } from './locations-search.js'; import { addJourneyToDraftingBoard } from './drafting-board.js'; +import { DataStore } from './datastore.js'; let element_journeys_search = document.querySelector("#journeys-search"); let element_from = document.querySelector("#journey-search-from"); @@ -26,6 +27,7 @@ function createJourneyElement(journey) { let el = document.createElement("div"); for (let leg of journey.legs) { + DataStore.locations.rememberAllIfNotExist([leg.origin, leg.destination]); el.appendChild(createJourneyLegElement(leg)); } diff --git a/web/traveldrafter.js b/web/traveldrafter.js index 5f356fa..c33976b 100644 --- a/web/traveldrafter.js +++ b/web/traveldrafter.js @@ -2,8 +2,10 @@ import * as Api from './api.js'; import { setupPopups } from "./popup.js"; import { setupLocationsSearch } from './locations-search.js'; import { setupJourneysSearch, attachJourneysSearch } from './journeys-search.js'; +import { DataStore } from './datastore.js'; window.Api = Api; +window.DataStore = DataStore; setupPopups(); setupLocationsSearch();