diff --git a/lib/frontend/stores/eventStore.js b/lib/frontend/stores/eventStore.js index 89d966f..72fa6ba 100644 --- a/lib/frontend/stores/eventStore.js +++ b/lib/frontend/stores/eventStore.js @@ -1,9 +1,12 @@ import { writable } from 'svelte/store' import { STORAGE_KEYS } from '../services/storageListener' +import { migrateRecord, sortRecords } from '../../static/app/storage' const createEventStore = () => { const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]') + .map(migrateRecord) + .sort(sortRecord) const { subscribe, set, update } = writable(state) diff --git a/lib/static/app/storage.js b/lib/static/app/storage.js index d7e3847..b3c96ec 100644 --- a/lib/static/app/storage.js +++ b/lib/static/app/storage.js @@ -88,8 +88,22 @@ const deleteRecord = (id) => { updateStorage(nextStorage) } +const sortRecord = (a, b) => { + const aDate = new Date(a.createdAt) + const bDate = new Date(b.createdAt) + + if (aDate < bDate) { + return -1 + } + if (aDate > bDate) { + return 1 + } + return 0 +} + export { migrateRecord, + sortRecord, getStorage, getConfigStorage, getStorageContents,