fixup! feature: update event store contents from existing code

This commit is contained in:
Ondřej Synáček 2020-12-15 22:42:43 +01:00
parent b86402a7ff
commit 470184aec0
2 changed files with 13 additions and 42 deletions

View File

@ -1,7 +1,7 @@
import { writable } from 'svelte/store'
import { STORAGE_KEYS } from '../services/storageListener'
import { migrateRecord, sortRecords } from '../../static/app/storage'
import { migrateRecord, sortRecord } from '../utils'
const createEventStore = () => {
const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]')
@ -23,6 +23,16 @@ const createEventStore = () => {
]))
}
const clearCalculation = (id) => {
const calculationIndex = state.findIndex((event) => event.id === id)
if (calculationIndex === -1) {
return
}
set(state.splice(calculationIndex, 1))
}
const getState = () => state
return {
@ -31,6 +41,7 @@ const createEventStore = () => {
set,
update,
setCalculation,
clearCalculation,
getState,
}
}

View File

@ -55,55 +55,15 @@ const updateConfigStorage = (storageContents, key, value) => {
}
const saveRecord = ({ id, link, createdAt, startTime, title }) => {
const storage = getStorage()
const storageContents = getStorageContents(storage)
const record = {
id,
link,
createdAt: createdAt.toString(),
startTime: startTime.toString(),
title,
}
updateStorage([ ...storageContents, record ])
eventStore.setCalculation({ id, link, createdAt, startTime, title })
}
const deleteRecord = (id) => {
const storage = getStorage()
const storageContents = getStorageContents(storage)
const index = storageContents.findIndex((record) => {
return record.id === id
})
if (!Number.isFinite(index)) {
return
}
const nextStorage = [ ...storageContents ]
nextStorage.splice(index, 1)
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
eventStore.clearCalculation(id)
}
export {
migrateRecord,
sortRecord,
getStorage,
getConfigStorage,
getStorageContents,