ctucx.git: trainsearch

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

commit 6808e42445df6ad9a3cabd9762f3b853d865d99b
parent 69dc5689e0279353220bb07445a17baf16613829
Author: Katja (ctucx) <git@ctu.cx>
Date: Mon, 27 Jan 2025 11:22:57 +0100

searchView: profile specific history
3 files changed, 24 insertions(+), 6 deletions(-)
M
src/app_functions.js
|
1
+
M
src/dataStorage.js
|
20
++++++++++++++++++--
M
src/searchView.js
|
9
+++++----
diff --git a/src/app_functions.js b/src/app_functions.js
@@ -28,6 +28,7 @@ const addJourneys = async data => {
 	if (!data) return false;
 
 	const historyEntry = {
+		profile:   data.profile,
 		fromPoint: getFrom(data.journeys),
 		viaPoint:  data.params.via,
 		toPoint:   getTo(data.journeys),
diff --git a/src/dataStorage.js b/src/dataStorage.js
@@ -90,14 +90,30 @@ class IDBStorage {
 		await Promise.all(proms);
 	}
 
-	async getJourneysHistory() {
-		return await this.idb.getAll('journeysHistory');
+	async getHistory(profile) {
+		const tx      = this.idb.transaction('journeysHistory');
+		const history = [];
+
+		for await (const cursor of tx.store) {
+			if (profile !== undefined && profile !== cursor.value.profile) continue;
+			history.push({
+				...cursor.value,
+				key: cursor.key,
+			});
+		}
+
+		return history;
 	}
 
 	async addHistoryEntry(newEntry) {
 		await this.idb.put('journeysHistory', newEntry);
 	}
 
+	async getHistoryEntry(key) {
+		return await this.idb.get('journeysHistory', key);
+	}
+
+
 	async getSettings() {
 		return await this.idb.get('settings', 'settings');
 	}
diff --git a/src/searchView.js b/src/searchView.js
@@ -176,12 +176,12 @@ const searchTemplate = (journeysHistory) => html`
 `;
 
 const journeysHistoryAction = (journeysHistory, element) => showSelectModal([
-	{'label': t('setfromto'),       'action': () => { setFromHistory(journeysHistory.length - 1 - journeysHistory.indexOf(element)); hideOverlay(); }},
-	{'label': t('journeyoverview'), 'action': () => { go('/'+element.slug+'/'+settings.journeysViewMode); hideOverlay(); }}
+	{'label': t('setfromto'),       'action': () => { setFromHistory(element.key); hideOverlay(); }},
+	{'label': t('journeyoverview'), 'action': () => { go(`/${element.slug}/${settings.journeysViewMode}`); hideOverlay(); }}
 ]);
 
 export const searchView = async (clearInputs) => {
-	const journeysHistory = (await db.getJourneysHistory()).slice().reverse();	
+	const journeysHistory = (await db.getHistory(settings.profile)).slice().reverse();
 
 	render(searchTemplate(journeysHistory), ElementById('content'));
 

@@ -402,7 +402,8 @@ const swapFromTo = () => {
 };
 
 const setFromHistory = async id => {
-	const entry = (await db.getJourneysHistory())[id];
+	const entry = (await db.getHistoryEntry(id));
+
 	if (!entry) return;
 
 	setSuggestion(entry.fromPoint, 'from');