1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-06-05 22:09:25 +02:00

refactor: replace event list table with Svelte version

This commit is contained in:
Ondřej Synáček
2020-12-15 22:56:39 +01:00
parent 978662b55c
commit 0f21613e39
7 changed files with 34 additions and 174 deletions

View File

@@ -4,40 +4,53 @@ import { STORAGE_KEYS } from '../services/storageListener'
import { migrateRecord, sortRecord } from '../utils'
const createEventStore = () => {
const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]')
const storedState = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]')
.map(migrateRecord)
.sort(sortRecord)
let state = { events: storedState }
const getState = () => state
const { subscribe, set, update } = writable(state)
const setCalculation = ({ id, link, createdAt, startTime, title }) => {
update((prevState) => ([
...prevState,
{
id,
link,
createdAt: createdAt.toString(),
startTime: startTime.toString(),
title,
},
]))
update((prevState) => {
const nextState = {
...prevState,
events: [
...prevState.events,
{
id,
link,
createdAt: createdAt.toString(),
startTime: startTime.toString(),
title,
},
]
}
state = nextState
return nextState
})
}
const clearCalculation = (id) => {
const calculationIndex = state.findIndex((event) => event.id === id)
const calculationIndex = getState().events.findIndex((event) => event.id === id)
if (calculationIndex === -1) {
return
}
const nextState = [ ...state ]
nextState.splice(calculationIndex, 1)
const nextEvents = [ ...getState().events ]
nextEvents.splice(calculationIndex, 1)
const nextState = { ...getState(), events: nextEvents }
state = nextState
set(nextState)
}
const getState = () => state
return {
...state,
subscribe,