diff --git a/lib/frontend/components/AppContainer.svelte b/lib/frontend/components/AppContainer.svelte index f7f646f..cfb6551 100644 --- a/lib/frontend/components/AppContainer.svelte +++ b/lib/frontend/components/AppContainer.svelte @@ -5,7 +5,7 @@ import { configStore, eventStore } from '../stores' $: showTrackingPanel = $configStore.track === undefined - $: showEventList = $eventStore.length > 0 + $: showEventList = $eventStore.events.length > 0 {#if showTrackingPanel} diff --git a/lib/frontend/components/EventList.svelte b/lib/frontend/components/EventList.svelte index 28405c8..213c698 100644 --- a/lib/frontend/components/EventList.svelte +++ b/lib/frontend/components/EventList.svelte @@ -55,7 +55,7 @@ - {#each $eventStore as event (event.id)} + {#each $eventStore.events as event (event.id)} {event.startTime diff --git a/lib/frontend/services/storageListener.js b/lib/frontend/services/storageListener.js index d023b3f..691fff3 100644 --- a/lib/frontend/services/storageListener.js +++ b/lib/frontend/services/storageListener.js @@ -36,7 +36,7 @@ class StorageListener { configStore.set(JSON.parse(event.newValue)) break case STORAGE_KEYS.EVENTS: - eventStore.set(JSON.parse(event.newValue)) + eventStore.set({ events: JSON.parse(event.newValue) }) break default: return @@ -48,7 +48,7 @@ class StorageListener { } _handleEventStoreChange(value) { - localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value)) + localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value.events || [])) } } diff --git a/lib/frontend/stores/eventStore.js b/lib/frontend/stores/eventStore.js index 4176863..a029e27 100644 --- a/lib/frontend/stores/eventStore.js +++ b/lib/frontend/stores/eventStore.js @@ -4,40 +4,53 @@ import { STORAGE_KEYS } from '../services/storageListener' import { migrateRecord, sortRecord } from '../utils' const createEventStore = () => { - const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]') + const storedState = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]') .map(migrateRecord) .sort(sortRecord) + let state = { events: storedState } + + const getState = () => state + const { subscribe, set, update } = writable(state) const setCalculation = ({ id, link, createdAt, startTime, title }) => { - update((prevState) => ([ - ...prevState, - { - id, - link, - createdAt: createdAt.toString(), - startTime: startTime.toString(), - title, - }, - ])) + update((prevState) => { + const nextState = { + ...prevState, + events: [ + ...prevState.events, + { + id, + link, + createdAt: createdAt.toString(), + startTime: startTime.toString(), + title, + }, + ] + } + + state = nextState + return nextState + }) } const clearCalculation = (id) => { - const calculationIndex = state.findIndex((event) => event.id === id) + const calculationIndex = getState().events.findIndex((event) => event.id === id) if (calculationIndex === -1) { return } - const nextState = [ ...state ] - nextState.splice(calculationIndex, 1) + const nextEvents = [ ...getState().events ] + nextEvents.splice(calculationIndex, 1) + + const nextState = { ...getState(), events: nextEvents } + state = nextState set(nextState) } - const getState = () => state - return { ...state, subscribe, diff --git a/lib/static/app/storage.js b/lib/static/app/storage.js index 9f6eba3..391a304 100644 --- a/lib/static/app/storage.js +++ b/lib/static/app/storage.js @@ -1,29 +1,6 @@ // TODO: this import should be removed once refactoring is finished import { eventStore } from '../../frontend/stores' -const migrateRecord = (record) => { - // NOTE: v3 records - const id = record.id || record.order - const startTime = record.startTime || null - - return { - ...record, - id, - startTime, - } -} - -const getStorage = () => { - const storage = localStorage.getItem('fb-to-ical-events') - - if (!storage) { - localStorage.setItem('fb-to-ical-events', JSON.stringify([])) - return "[]" - } - - return storage -} - const getConfigStorage = () => { const storage = localStorage.getItem('fb-to-ical-config') @@ -39,36 +16,12 @@ const getStorageContents = (storage) => { return JSON.parse(storage) } -const updateStorage = (storageContents) => { - const encodedStorage = JSON.stringify(storageContents) - - localStorage.setItem('fb-to-ical-events', encodedStorage) -} - -const updateConfigStorage = (storageContents, key, value) => { - const encodedStorage = JSON.stringify({ - ...storageContents, - [key]: value, - }) - - localStorage.setItem('fb-to-ical-config', encodedStorage) -} - const saveRecord = ({ id, link, createdAt, startTime, title }) => { eventStore.setCalculation({ id, link, createdAt, startTime, title }) } -const deleteRecord = (id) => { - eventStore.clearCalculation(id) -} - export { - migrateRecord, - getStorage, getConfigStorage, getStorageContents, - updateStorage, - updateConfigStorage, saveRecord, - deleteRecord, } diff --git a/lib/static/index.html b/lib/static/index.html index 81be266..397121d 100644 --- a/lib/static/index.html +++ b/lib/static/index.html @@ -56,23 +56,9 @@
-
- - - - - - - - - - - -
+
-
-