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

@ -5,7 +5,7 @@
import { configStore, eventStore } from '../stores' import { configStore, eventStore } from '../stores'
$: showTrackingPanel = $configStore.track === undefined $: showTrackingPanel = $configStore.track === undefined
$: showEventList = $eventStore.length > 0 $: showEventList = $eventStore.events.length > 0
</script> </script>
{#if showTrackingPanel} {#if showTrackingPanel}

View File

@ -55,7 +55,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{#each $eventStore as event (event.id)} {#each $eventStore.events as event (event.id)}
<tr> <tr>
<td> <td>
{event.startTime {event.startTime

View File

@ -36,7 +36,7 @@ class StorageListener {
configStore.set(JSON.parse(event.newValue)) configStore.set(JSON.parse(event.newValue))
break break
case STORAGE_KEYS.EVENTS: case STORAGE_KEYS.EVENTS:
eventStore.set(JSON.parse(event.newValue)) eventStore.set({ events: JSON.parse(event.newValue) })
break break
default: default:
return return
@ -48,7 +48,7 @@ class StorageListener {
} }
_handleEventStoreChange(value) { _handleEventStoreChange(value) {
localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value)) localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value.events || []))
} }
} }

View File

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

View File

@ -1,29 +1,6 @@
// TODO: this import should be removed once refactoring is finished // TODO: this import should be removed once refactoring is finished
import { eventStore } from '../../frontend/stores' import { eventStore } from '../../frontend/stores'
const migrateRecord = (record) => {
// NOTE: v3 records
const id = record.id || record.order
const startTime = record.startTime || null
return {
...record,
id,
startTime,
}
}
const getStorage = () => {
const storage = localStorage.getItem('fb-to-ical-events')
if (!storage) {
localStorage.setItem('fb-to-ical-events', JSON.stringify([]))
return "[]"
}
return storage
}
const getConfigStorage = () => { const getConfigStorage = () => {
const storage = localStorage.getItem('fb-to-ical-config') const storage = localStorage.getItem('fb-to-ical-config')
@ -39,36 +16,12 @@ const getStorageContents = (storage) => {
return JSON.parse(storage) return JSON.parse(storage)
} }
const updateStorage = (storageContents) => {
const encodedStorage = JSON.stringify(storageContents)
localStorage.setItem('fb-to-ical-events', encodedStorage)
}
const updateConfigStorage = (storageContents, key, value) => {
const encodedStorage = JSON.stringify({
...storageContents,
[key]: value,
})
localStorage.setItem('fb-to-ical-config', encodedStorage)
}
const saveRecord = ({ id, link, createdAt, startTime, title }) => { const saveRecord = ({ id, link, createdAt, startTime, title }) => {
eventStore.setCalculation({ id, link, createdAt, startTime, title }) eventStore.setCalculation({ id, link, createdAt, startTime, title })
} }
const deleteRecord = (id) => {
eventStore.clearCalculation(id)
}
export { export {
migrateRecord,
getStorage,
getConfigStorage, getConfigStorage,
getStorageContents, getStorageContents,
updateStorage,
updateConfigStorage,
saveRecord, saveRecord,
deleteRecord,
} }

View File

@ -56,22 +56,8 @@
<br /> <br />
<div class="list-wrapper">
<table id="list" class="hidden">
<thead>
<tr>
<td>Date</td>
<td>Name</td>
<td></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</article>
<div id="root"></div> <div id="root"></div>
</article>
<footer> <footer>
v.<%= htmlWebpackPlugin.options.version %> ◆ v.<%= htmlWebpackPlugin.options.version %> ◆

View File

@ -1,13 +1,8 @@
import { uuidv4, parseStartTimeFromiCalString } from './app/utils' import { uuidv4, parseStartTimeFromiCalString } from './app/utils'
import { import {
migrateRecord,
getStorage,
getConfigStorage, getConfigStorage,
getStorageContents, getStorageContents,
updateStorage,
updateConfigStorage,
saveRecord, saveRecord,
deleteRecord,
} from './app/storage' } from './app/storage'
import logger from './app/logger' import logger from './app/logger'
import { extractEventDataFromHTML } from '../../lib/services/ics-retriever' import { extractEventDataFromHTML } from '../../lib/services/ics-retriever'
@ -18,53 +13,6 @@ import boot from '../frontend'
(() => { (() => {
document.addEventListener('DOMContentLoaded', boot) document.addEventListener('DOMContentLoaded', boot)
const showTable = () => {
list.classList.remove('hidden')
}
const deleteTableRow = (id, row) => {
deleteRecord(id)
row.remove()
}
const insertTableRow = ({ id, link, createdAt, startTime, title }) => {
showTable()
const newRow = document.createElement('tr')
const startTimeCol = document.createElement('td')
startTimeCol.innerText = startTime ?
new Date(startTime).toLocaleString() :
'N/A\xa0\xa0\xa0\xa0\xa0'
const downloadEl = document.createElement('a')
downloadEl.setAttribute('href', link)
downloadEl.innerText = title
const titleCol = document.createElement('td')
titleCol.appendChild(downloadEl)
const deleteEl = document.createElement('a')
deleteEl.setAttribute('href', 'javascript:void(0)')
deleteEl.innerText = '✖︎'
deleteEl.classList.add('delete-record')
deleteEl.addEventListener('click', (event) => {
event.preventDefault()
deleteTableRow(id, newRow)
})
const actionCol = document.createElement('td')
actionCol.classList.add('actions')
actionCol.appendChild(deleteEl)
newRow.appendChild(startTimeCol)
newRow.appendChild(titleCol)
newRow.appendChild(actionCol)
tableBody.prepend(newRow)
}
const createRecord = (uri, summary, startTime) => { const createRecord = (uri, summary, startTime) => {
const id = uuidv4() const id = uuidv4()
const createdAt = new Date() const createdAt = new Date()
@ -76,43 +24,6 @@ import boot from '../frontend'
startTime, startTime,
title: summary, title: summary,
}) })
insertTableRow({
id,
link: uri,
createdAt,
title: summary,
startTime
})
}
const hydrateList = () => {
const prevStorage = getStorage()
const migratedStorageContents = getStorageContents(prevStorage).map((record) => {
return migrateRecord(record)
})
updateStorage(migratedStorageContents)
const storage = getStorage()
const storageContents = getStorageContents(storage)
if (storageContents.length > 0) {
showTable()
}
storageContents
.sort((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
})
.forEach((record) => {
insertTableRow(record)
})
} }
const configureLogger = (logger) => { const configureLogger = (logger) => {
@ -203,8 +114,6 @@ import boot from '../frontend'
const submitButton = document.querySelector("#submit") const submitButton = document.querySelector("#submit")
const input = document.querySelector("#url") const input = document.querySelector("#url")
const link = document.querySelector("#current-download") const link = document.querySelector("#current-download")
const table = document.querySelector('#list')
const tableBody = table.querySelector('tbody')
if (window.navigator && window.navigator.serviceWorker) { if (window.navigator && window.navigator.serviceWorker) {
const serviceWorker = window.navigator.serviceWorker const serviceWorker = window.navigator.serviceWorker
@ -240,7 +149,6 @@ import boot from '../frontend'
}) })
} }
hydrateList()
configureLogger(logger) configureLogger(logger)
const handleHTMLResponse = (html, url) => { const handleHTMLResponse = (html, url) => {