diff --git a/easycal/easycal.js b/easycal/easycal.js index d1c52f2..f084f66 100644 --- a/easycal/easycal.js +++ b/easycal/easycal.js @@ -1,8 +1,10 @@ +let events = []; + function handleClientLoad() { gapi.load('client:auth2', initClient); } -function findPrevious(events) { +function findPrevious() { const now = Date.now(); let max = null; let maxTime = 0; @@ -18,7 +20,7 @@ function findPrevious(events) { return max; } -function findCurrent(events) { +function findCurrent() { const now = Date.now(); for (const event of events) { const start = new Date(event.start.dateTime).getTime(); @@ -31,8 +33,8 @@ function findCurrent(events) { return null; } -function anyRemaining(events) { - const current = findCurrent(events); +function anyRemaining() { + const current = findCurrent(); const currentEnd = current ? new Date(current.end.dateTime).getTime() : null; const now = Date.now(); @@ -47,8 +49,8 @@ function anyRemaining(events) { return false; } -function findNext(events) { - const current = findCurrent(events); +function findNext() { + const current = findCurrent(); const currentEnd = current ? new Date(current.end.dateTime).getTime() : null; const now = Date.now(); @@ -107,16 +109,16 @@ function timezoneOffset() { return `${offset > 0 ? '-' : '+'}${Math.floor(offset / 60).toString().padStart(2, '0')}:${(offset % 60).toString().padStart(2, '0')}`; } -function render(events) { +function render() { const curElem = document.getElementById("current"); const curName = document.getElementById("currentName"); const curLeft = document.getElementById("currentLeft"); const nextElem = document.getElementById("next"); const nextName = document.getElementById("nextName"); - const current = findCurrent(events); - const prev = findPrevious(events); - const next = findNext(events); + const current = findCurrent(); + const prev = findPrevious(); + const next = findNext(); if (current) { // Currently in an event @@ -154,7 +156,7 @@ function render(events) { } else if (current) { nextElem.classList.remove("event"); nextElem.classList.add("break"); - if (anyRemaining(events)) { + if (anyRemaining()) { setText(nextName, "😎 Break"); } else { setText(nextName, "✅ Done!"); @@ -166,6 +168,20 @@ function render(events) { } } +function loadEvents() { + const tz = timezoneOffset(); + const date = new Date().toISOString().substring(0,10); + return gapi.client.calendar.events.list({ + 'calendarId': 'primary', + 'timeMin': `${date}T00:00:00${tz}`, + 'timeMax': `${date}T23:59:59${tz}`, + 'showDeleted': false, + 'singleEvents': true, + 'maxResults': 250, + 'orderBy': 'startTime', + }); +} + function initClient() { gapi.client.init({ apiKey: 'AIzaSyDzNMBbLqQoSCMiug_7UbUrgaAvnoyzYYU', @@ -175,23 +191,19 @@ function initClient() { ux_mode: 'redirect', }).then(() => { if (gapi.auth2.getAuthInstance().isSignedIn.get()) { - const tz = timezoneOffset(); - const date = new Date().toISOString().substring(0,10); - return gapi.client.calendar.events.list({ - 'calendarId': 'primary', - 'timeMin': `${date}T00:00:00${tz}`, - 'timeMax': `${date}T23:59:59${tz}`, - 'showDeleted': false, - 'singleEvents': true, - 'maxResults': 250, - 'orderBy': 'startTime', - }); + return loadEvents(); } else { gapi.auth2.getAuthInstance().signIn(); return; } }).then((response) => { - setInterval(() => render(response.result.items), 250); + events = response.result.items; + setInterval(() => render(), 250); + setInterval(() => { + loadEvents().then((response) => { + events = response.result.items; + }); + }, 30 * 60 * 1000); }); } diff --git a/easycal/index.html b/easycal/index.html index d340d53..3cc05e9 100644 --- a/easycal/index.html +++ b/easycal/index.html @@ -3,7 +3,7 @@ EasyCal - +