commit 2ced5b070388d4eaaa0dffbb16bf42d5694c7f43
parent f86b6adbec619415f237f1c8135c81e10e80edb6
Author: Yureka <yuka@yuka.dev>
Date: Tue, 6 Sep 2022 11:52:18 +0200
parent f86b6adbec619415f237f1c8135c81e10e80edb6
Author: Yureka <yuka@yuka.dev>
Date: Tue, 6 Sep 2022 11:52:18 +0200
localstorage
3 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/src/app_functions.js b/src/app_functions.js @@ -84,8 +84,10 @@ const processJourney = journey => { export const getJourneys = async slug => { let data = await db.getJourneysOverview(slug); - data.journeys = await Promise.all(data.journeys.map(x => getJourney(x, data.profile))); - return data; + return { + ...data, + journeys: await Promise.all(data.journeys.map(x => getJourney(x, data.profile))), + }; }; export const getJourney = async (refreshToken, profile) => {
diff --git a/src/dataStorage.js b/src/dataStorage.js @@ -6,21 +6,45 @@ const dbName = devMode ? 'trainsearch_dev' : 'trainsearch'; export let db; class LocalStorage { + constructor() { + this.settings = JSON.parse(localStorage.getItem('settings') || "null"); + this.journey = JSON.parse(localStorage.getItem('journey') || "{}"); + this.journeysHistory = JSON.parse(localStorage.getItem('journeysHistory') || "[]"); + this.journeysOverview = JSON.parse(localStorage.getItem('journeysOverview') || "{}"); + } async addJourneys(journeyEntries, overviewEntry, historyEntry) { + for (let j of journeyEntries) { + this.journey[j.refreshToken] = j; + } + localStorage.setItem('journey', JSON.stringify(this.journey)); + this.journeysOverview[overviewEntry.slug] = overviewEntry; + localStorage.setItem('journeysOverview', JSON.stringify(this.journeysOverview)); + this.addHistoryEntry(historyEntry); } async getJourneysHistory() { + return this.journeysHistory; } async addHistoryEntry(newEntry) { + this.journeysHistory.push(newEntry); + localStorage.setItem('journeysHistory', JSON.stringify(this.journeysHistory)); } async getSettings() { + return this.settings; } - async modifySettings(callback) { + async modifySettings(prevSettings, callback) { + const newSettings = callback(prevSettings); + localStorage.setItem('settings', JSON.stringify(newSettings)); + return newSettings; } async getJourneysOverview(slug) { + return this.journeysOverview[slug]; } async getJourney(refreshToken) { + return this.journey[refreshToken]; } async updateJourney(data) { + this.journey[data.refreshToken] = data; + localStorage.setItem('journey', JSON.stringify(this.journey)); } } @@ -94,9 +118,9 @@ class IDBStorage { async getSettings() { return await this.idb.get('settings', 'settings'); } - async modifySettings(cb) { + async modifySettings(prevSettings, cb) { const tx = this.idb.transaction('settings', 'readwrite'); - const newSettings = callback(JSON.parse(JSON.stringify(this.getSettings()))); + const newSettings = callback(prevSettings); await Promise.all([ tx.store.put(newSettings, 'settings'), tx.done
diff --git a/src/settings.js b/src/settings.js @@ -41,12 +41,12 @@ export const subscribeSettings = cb => { }; export const initSettings = async () => { - settings = (await db.getSettings) || defaultSettings; + settings = (await db.getSettings()) || defaultSettings; for (const cb of subscribers) await cb(); }; export const modifySettings = async callback => { - const newSettings = db.modifySettings(callback); + const newSettings = await db.modifySettings(JSON.parse(JSON.stringify(settings)), callback); Object.freeze(newSettings); settings = newSettings; for (const cb of subscribers) await cb();