From ca14709309d0906084a50e1368fa94033852d378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Syn=C3=A1=C4=8Dek?= Date: Sun, 24 Nov 2019 22:22:02 +0100 Subject: [PATCH] display date of the event instead of date of the creation of iCal file --- lib/public/scripts.js | 52 ++++++++++++++++++++++++++++++++++--------- lib/public/style.css | 1 + lib/views/index.ejs | 2 +- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/public/scripts.js b/lib/public/scripts.js index 717815e..c41f41e 100644 --- a/lib/public/scripts.js +++ b/lib/public/scripts.js @@ -17,13 +17,31 @@ ) } + const parseStartTimeFromiCalString = (text = '') => { + const [ dateStr, timeStr ] = text.split('T') + const rawDate = dateStr || '' + const rawTime = timeStr || '' + + const year = Number(rawDate.slice(0, 4)) + const month = Number(Math.max(rawDate.slice(4, 6) - 1), 0) + const date = Number(rawDate.slice(6, 8)) + const hour = Number(rawTime.slice(0, 2)) + const minutes = Number(rawTime.slice(2, 4)) + const seconds = Number(rawTime.slice(4, 6)) + + const parsedDate = new Date(year, month, date, hour, minutes, seconds) + return parsedDate.toString() + } + const migrateRecord = (record) => { - // NOTE: v2 records + // NOTE: v3 records const id = record.id || record.order + const startTime = record.startTime || null return { ...record, id, + startTime, } } @@ -52,7 +70,7 @@ localStorage.setItem('fb-to-ical-events', encodedStorage) } - const saveRecord = (id, link, createdAt, title) => { + const saveRecord = ({ id, link, createdAt, startTime, title }) => { if (!useStorage) { return } @@ -64,6 +82,7 @@ id, link, createdAt: createdAt.toString(), + startTime: startTime.toString(), title, } @@ -105,14 +124,15 @@ row.remove() } - const insertTableRow = ({ id, link, createdAt, title }) => { + const insertTableRow = ({ id, link, createdAt, startTime, title }) => { showTable() const newRow = document.createElement('tr') - const createdAtCol = document.createElement('td') - const parsedCreatedAt = new Date(createdAt) - createdAtCol.innerText = (parsedCreatedAt || new Date()).toLocaleString() + const startTimeCol = document.createElement('td') + startTimeCol.innerText = startTime ? + new Date(startTime).toLocaleString() : + 'N/A\xa0\xa0\xa0\xa0\xa0' const titleCol = document.createElement('td') titleCol.innerText = title @@ -135,23 +155,30 @@ actionCol.appendChild(downloadEl) actionCol.appendChild(deleteEl) - newRow.appendChild(createdAtCol) + newRow.appendChild(startTimeCol) newRow.appendChild(titleCol) newRow.appendChild(actionCol) tableBody.prepend(newRow) } - const createRecord = (uri, summary) => { + const createRecord = (uri, summary, startTime) => { const id = uuidv4() const createdAt = new Date() - saveRecord(id, uri, createdAt, summary) + saveRecord({ + id, + link: uri, + createdAt, + startTime, + title: summary, + }) insertTableRow({ id, link: uri, createdAt, title: summary, + startTime }) } @@ -356,8 +383,13 @@ const summaryMatch = text.match(/SUMMARY:.*/)[0] const summary = summaryMatch ? summaryMatch.replace(/SUMMARY:/, '') : '' + const startTimeMatches = text.match(/DTSTART:.*/) + const startTimeMatch = text.length > 0 ? + (startTimeMatches[0] || '').replace(/DTSTART:/, '') : + '' + const startTime = parseStartTimeFromiCalString(startTimeMatch) - createRecord(uri, summary) + createRecord(uri, summary, startTime) clearStatuses() }) .catch((err) => { diff --git a/lib/public/style.css b/lib/public/style.css index 6ae1f1b..48aa384 100644 --- a/lib/public/style.css +++ b/lib/public/style.css @@ -87,6 +87,7 @@ input { } #list { + width: 100%; } .row { diff --git a/lib/views/index.ejs b/lib/views/index.ejs index cf8c981..9302971 100644 --- a/lib/views/index.ejs +++ b/lib/views/index.ejs @@ -62,7 +62,7 @@ - +