From 470184aec0ce2c4caf80dbca62cf55dedc0bce71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Syn=C3=A1=C4=8Dek?= Date: Tue, 15 Dec 2020 22:42:43 +0100 Subject: [PATCH] fixup! feature: update event store contents from existing code --- lib/frontend/stores/eventStore.js | 13 +++++++++- lib/static/app/storage.js | 42 +------------------------------ 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/lib/frontend/stores/eventStore.js b/lib/frontend/stores/eventStore.js index 72fa6ba..c4f21bd 100644 --- a/lib/frontend/stores/eventStore.js +++ b/lib/frontend/stores/eventStore.js @@ -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, } } diff --git a/lib/static/app/storage.js b/lib/static/app/storage.js index b3c96ec..9f6eba3 100644 --- a/lib/static/app/storage.js +++ b/lib/static/app/storage.js @@ -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,