fixup! feature: update event store contents from existing code
This commit is contained in:
parent
b86402a7ff
commit
470184aec0
|
@ -1,7 +1,7 @@
|
||||||
import { writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
|
|
||||||
import { STORAGE_KEYS } from '../services/storageListener'
|
import { STORAGE_KEYS } from '../services/storageListener'
|
||||||
import { migrateRecord, sortRecords } from '../../static/app/storage'
|
import { migrateRecord, sortRecord } from '../utils'
|
||||||
|
|
||||||
const createEventStore = () => {
|
const createEventStore = () => {
|
||||||
const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]')
|
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
|
const getState = () => state
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -31,6 +41,7 @@ const createEventStore = () => {
|
||||||
set,
|
set,
|
||||||
update,
|
update,
|
||||||
setCalculation,
|
setCalculation,
|
||||||
|
clearCalculation,
|
||||||
getState,
|
getState,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,55 +55,15 @@ const updateConfigStorage = (storageContents, key, value) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveRecord = ({ id, link, createdAt, startTime, title }) => {
|
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 })
|
eventStore.setCalculation({ id, link, createdAt, startTime, title })
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteRecord = (id) => {
|
const deleteRecord = (id) => {
|
||||||
const storage = getStorage()
|
eventStore.clearCalculation(id)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
migrateRecord,
|
migrateRecord,
|
||||||
sortRecord,
|
|
||||||
getStorage,
|
getStorage,
|
||||||
getConfigStorage,
|
getConfigStorage,
|
||||||
getStorageContents,
|
getStorageContents,
|
||||||
|
|
Loading…
Reference in New Issue