From 2b2b12175173feeee3396fda1d31272f113d4c3c Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Thu, 28 Jul 2022 23:51:47 +0200 Subject: [PATCH] Fix token problem --- src/components/Calendar.tsx | 57 +++++++------------------------ src/components/DVB.tsx | 2 +- src/components/News.tsx | 2 +- src/components/PlantState.tsx | 2 +- src/components/WeatherAndTime.tsx | 2 +- 5 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/components/Calendar.tsx b/src/components/Calendar.tsx index e99b41a..4ef4846 100644 --- a/src/components/Calendar.tsx +++ b/src/components/Calendar.tsx @@ -26,10 +26,11 @@ interface EventList { } const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { - const [token, setToken] = React.useState("") + const [calendarToken, setToken] = React.useState("") const [events, setEvents] = React.useState({}) const mergeEvents = (currentList: EventList, toAdd: Event[], ownerIdx: number = 0) => { + if (!toAdd || toAdd.length === 0) return; for(const event of toAdd) { const startDate = new Date(event.start.date ? event.start.date : event.start.dateTime) const startDateString = startDate.toDateString() @@ -103,45 +104,6 @@ const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { } } - /*let lastDate = ""; - let i = 0; - for (const event of events) { - const startDate = new Date(event.start.date ? event.start.date : event.start.dateTime) - const startDateString = `${startDate.getFullYear()}${startDate.getMonth()}${startDate.getDate()}` - if (startDateString !== lastDate) { - lastDate = startDateString; - const dayDiff = daysDifference(new Date(), startDate); - let dayDiffString: string; - switch (dayDiff) { - case 0: dayDiffString = "Heute" - break; - case 1: dayDiffString = "Morgen" - break; - default: dayDiffString = `${dayDiff} Tage`; - break; - } - eventTable.push( - - {dowToString(startDate.getDay())}, {startDate.getDate()}. {startDate.getMonth() + 1} - ({dayDiffString}) - - ); - } - - if (event.start.date) { - eventTable.push( - {event.summary} - ); - } else { - eventTable.push( - - {event.summary} - {startDate.getHours()}:{startDate.getMinutes().toString().padStart(2, "0")} - - ) - } - }*/ - return eventTable; } @@ -160,8 +122,8 @@ const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { return token.access_token; } - const pullCalendar = async (provToken?: string, calendarIndex: number = 0) => { - const correctToken = provToken || token; + const pullCalendar = async (calendarToken: string, calendarIndex: number = 0) => { + const correctToken = calendarToken; if (!correctToken || correctToken === "") return; const timeMin = new Date(); @@ -183,7 +145,7 @@ const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { //setEvents([...processEventData(events.items)]); } - const pullAll = async (provToken?: string) => { + const pullAll = async (provToken: string) => { const eventList: EventList = {} for(let i = 0; i < secrets.calendarIds.length; i++) { const events = await pullCalendar(provToken, i); @@ -194,8 +156,13 @@ const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { React.useEffect(() => { requestToken().then(pullAll) - const calendarInterval = setInterval(pullAll, CALENDAR_REFRESH_INTERVAL); - const calendarTokenInterval = setInterval(requestToken, CALENDAR_TOKEN_REFRESH_INTERVAL); + const calendarInterval = setInterval(() => { + setToken(token => { + pullAll(token); + return token; + }) + }, CALENDAR_REFRESH_INTERVAL); + const calendarTokenInterval = setInterval(requestToken.bind(this), CALENDAR_TOKEN_REFRESH_INTERVAL); return () => { clearInterval(calendarInterval); diff --git a/src/components/DVB.tsx b/src/components/DVB.tsx index 77b01ef..bdcd64d 100644 --- a/src/components/DVB.tsx +++ b/src/components/DVB.tsx @@ -54,7 +54,7 @@ const DVB = ({ stopId }: { stopId: number }) => { React.useEffect(() => { pullDepartures(); - const dvbInterval = setInterval(pullDepartures, DVB_REFRESH_INTERVAL); + const dvbInterval = setInterval(pullDepartures.bind(this), DVB_REFRESH_INTERVAL); return () => clearInterval(dvbInterval); }, []) diff --git a/src/components/News.tsx b/src/components/News.tsx index 3cc95ba..e6422e2 100644 --- a/src/components/News.tsx +++ b/src/components/News.tsx @@ -54,7 +54,7 @@ const News = () => { React.useEffect(() => { pullNews() - const newsInterval = setInterval(pullNews, NEWS_REFRESH_INTERVAL); + const newsInterval = setInterval(pullNews.bind(this), NEWS_REFRESH_INTERVAL); return () => clearInterval(newsInterval); }, []) diff --git a/src/components/PlantState.tsx b/src/components/PlantState.tsx index ab4b9e8..86d8434 100644 --- a/src/components/PlantState.tsx +++ b/src/components/PlantState.tsx @@ -29,7 +29,7 @@ const PlantState = ({ hassUrl, token, plants }: { hassUrl: string, token: string React.useEffect(() => { pullPlants() - const plantInterval = setInterval(pullPlants, PLANT_REFRESH_INTERVAL); + const plantInterval = setInterval(pullPlants.bind(this), PLANT_REFRESH_INTERVAL); return () => clearInterval(plantInterval); }, []) diff --git a/src/components/WeatherAndTime.tsx b/src/components/WeatherAndTime.tsx index f6114c0..a448e40 100644 --- a/src/components/WeatherAndTime.tsx +++ b/src/components/WeatherAndTime.tsx @@ -67,7 +67,7 @@ const WeatherAndTime = ({ secrets }: { secrets: SecretsWeather }) => { }, 1000); pullWeather() - const weatherInterval = setInterval(pullWeather, WEATHER_REFRESH_INTERVAL); + const weatherInterval = setInterval(pullWeather.bind(this), WEATHER_REFRESH_INTERVAL); return () => { clearInterval(dateInterval);