improvement: add record migration and correct state normalization in new code

This commit is contained in:
Ondřej Synáček 2020-12-15 22:06:45 +01:00
parent e3917ee79a
commit b86402a7ff
2 changed files with 17 additions and 0 deletions

View File

@ -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)

View File

@ -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,