ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

commit 36f155ccd33ae71f27f167ae7d2fc7b9a3e67b5a
parent e069de89c8f7c127c787f3cfc18cc52410fe5434
Author: Katja (ctucx) <git@ctu.cx>
Date: Fri, 31 Jan 2025 06:56:51 +0100

searchView: add last selected journey shortcut to history
4 files changed, 43 insertions(+), 18 deletions(-)
M
src/dataStorage.js
|
8
++++++--
M
src/journeyView.js
|
34
+++++++++++++++++++++++-----------
M
src/languages.js
|
4
+++-
M
src/searchView.js
|
15
+++++++++++----
diff --git a/src/dataStorage.js b/src/dataStorage.js
@@ -113,8 +113,12 @@ class IDBStorage {
 		return history;
 	}
 
-	async addHistoryEntry(newEntry) {
-		await this.idb.put('journeysHistory', newEntry);
+	async addHistoryEntry(entry) {
+		await this.idb.put('journeysHistory', entry);
+	}
+
+	async updateHistoryEntry(key, entry) {
+		await this.idb.put('journeysHistory', entry, key);
 	}
 
 	async getHistoryEntry(key) {
diff --git a/src/journeyView.js b/src/journeyView.js
@@ -8,6 +8,7 @@ import { formatName, formatDateTime, formatDuration, formatPrice, formatTrainTyp
 import { showAlertModal, showLoader, hideOverlay, showModal } from './overlays.js';
 import { go } from './router.js';
 import { t } from './languages.js';
+import { db } from './dataStorage.js';
 
 const legTemplate = (leg, profile) => {
 	const remarks        = leg.remarks || [];

@@ -138,36 +139,46 @@ const journeyTemplate = (data, profile) => {
 
 export const journeyView = async (match, isUpdate) => {
 	if (!isUpdate) showLoader();
-	let profile, refreshToken, data;
+	let profile, refreshToken, journeyObject;
+
 	try {
-		profile = match[0];
-		refreshToken = decodeURIComponent(match[1]);
-		data = await getJourney(refreshToken, profile);
+		profile       = match[0];
+		refreshToken  = decodeURIComponent(match[1]);
+		journeyObject = await getJourney(refreshToken, profile);
+
+		if (journeyObject.slug) {
+			let overviewObject = await db.getJourneysOverview(journeyObject.slug);
+			let historyObject  = await db.getHistoryEntry(overviewObject.historyEntryId);
+
+			historyObject.lastSelectedJourneyId = journeyObject.refreshToken;
+
+			await db.updateHistoryEntry(overviewObject.historyEntryId, historyObject);
+		}
 	} catch(e) {
 		console.error(e);
 		await showAlertModal(e.toString());
 		go('/');
 		return;
 	}
-	for (const leg of data.legs) {
+
+	for (const leg of journeyObject.legs) {
 		if (leg.line && leg.line.name) {
 			const [category, number] = leg.line.name.split(" ");
-			const info = await cachedCoachSequence(category, leg.line.fahrtNr || number, leg.origin.id, leg.plannedDeparture);
-			console.log(info);
-			if (info) {
-				leg.line.trainType = formatTrainTypes(info);
-			}
+			const info               = await cachedCoachSequence(category, leg.line.fahrtNr || number, leg.origin.id, leg.plannedDeparture);
+
+			if (info) leg.line.trainType = formatTrainTypes(info);
 		}
 	}
 
 	hideOverlay();
 
-	render(journeyTemplate(data, profile), ElementById('content'));
+	render(journeyTemplate(journeyObject, profile), ElementById('content'));
 	setThemeColor(queryBackgroundColor('header'));
 };
 
 const refreshJourneyView = async (refreshToken, profile) => {
 	document.querySelector('.icon-reload').classList.add('spinning');
+
 	try {
 		await refreshJourney(refreshToken, profile);
 	} catch(e) {

@@ -175,6 +186,7 @@ const refreshJourneyView = async (refreshToken, profile) => {
 		document.querySelector('.icon-reload').classList.remove('spinning');
 		throw e;
 	}
+
 	journeyView([profile, refreshToken], true);
 	document.querySelector('.icon-reload').classList.remove('spinning');
 };
diff --git a/src/languages.js b/src/languages.js
@@ -82,6 +82,7 @@ const languages = {
 		'loyaltyCardNone':     'keine Ermäßigungskarte',
 		'class':               'Klasse',
 		'titleNoTransfers':    'keine Umstiege zulassen',
+		'lastSelectedJourney': 'Zuletzt gewählte Verbindung',
 	},
 
 	'nl': {

@@ -224,6 +225,7 @@ const languages = {
 		'loyaltyCard':         'Discount Card',
 		'loyaltyCardNone':     'No discount card',
 		'class':               'Class',
-		'titleNoTransfers':    'only direct connections'
+		'titleNoTransfers':    'only direct connections',
+		'lastSelectedJourney': 'Last selected Journey',
 	}
 };
diff --git a/src/searchView.js b/src/searchView.js
@@ -176,10 +176,17 @@ const searchTemplate = (journeysHistory) => html`
 	</div>
 `;
 
-const journeysHistoryAction = (journeysHistory, element) => showSelectModal([
-	{'label': t('setfromto'),       'action': () => { setFromHistory(element.key); hideOverlay(); }},
-	{'label': t('journeyoverview'), 'action': () => { go(`/${element.slug}/${settings.journeysViewMode}`); hideOverlay(); }}
-]);
+const journeysHistoryAction = (journeysHistory, element) => {
+	const options = [
+		{'label': t('setfromto'),           'action': () => { setFromHistory(element.key); hideOverlay(); }},
+		{'label': t('journeyoverview'),     'action': () => go(`/${element.slug}/${settings.journeysViewMode}`)}
+	];
+
+	if (element.lastSelectedJourneyId !== undefined)
+		options.push({'label': t('lastSelectedJourney'), 'action': () => go(`/j/${settings.profile}/${element.lastSelectedJourneyId}`)})
+
+	showSelectModal(options);
+};
 
 export const searchView = async (clearInputs) => {
 	const journeysHistory = (await db.getHistory(settings.profile)).slice().reverse();