From ae833a912c39f9c4c97581cbf159414fc8fb36ff Mon Sep 17 00:00:00 2001 From: shahanneda Date: Tue, 15 Feb 2022 03:32:05 -0500 Subject: [PATCH] migrated warning system to json file --- components/WarningHeader.tsx | 83 ++++++++++++++++++++++++++++------ content/warnings/warnings.json | 12 +++++ 2 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 content/warnings/warnings.json diff --git a/components/WarningHeader.tsx b/components/WarningHeader.tsx index 8f7d03e0..752e376a 100644 --- a/components/WarningHeader.tsx +++ b/components/WarningHeader.tsx @@ -1,27 +1,80 @@ +import { start } from "repl"; + +import { parse } from "date-fns"; +import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz"; import { GetStaticProps } from "next"; -import { Warning } from "pages/api/currentWarning"; import React, { useState, useEffect } from "react"; import { getCurrentTerm } from "@/lib/events"; +import warnings from "../content/warnings/warnings.json"; + import styles from "./WarningHeader.module.css"; -export function WarningHeader() { - // We can't use getStaticProps since its a component, and we can't put it getStaticProps in its parents because nextjs doesnt support it in the _app.js - const [warning, setWarning] = useState(null); +// Had to add these functions here, since we cannot use import the file from ./lib/events since that uses libraries only on the serverside +const WARNING_DATE_FORMAT = "MMMM dd yyyy HH:mm"; +function getLocalDateFromEST(date: Date) { + return utcToZonedTime( + // The parsed date is in EST + zonedTimeToUtc(date, "America/Toronto"), + Intl.DateTimeFormat().resolvedOptions().timeZone + ); +} - useEffect(() => { - fetch("/api/currentWarning/") - .then((res) => res.json()) - .then((data) => { - setWarning(data); - }) - .catch((err) => { - console.error(err); - }); +interface Warning { + message: string; + startDate: string; + endDate: string; +} + +function getCurrentWarning(): Warning | null { + const today = new Date(); + let currentWarning: Warning | null = null; + + warnings.forEach((warning) => { + if (!warning.startDate || !warning.endDate) { + console.error("INVALID WARNING:", warning); + } + + // convert dates to date objects in EST time zone + let startDate = parse(warning.startDate, WARNING_DATE_FORMAT, new Date()); + let endDate = parse(warning.endDate, WARNING_DATE_FORMAT, new Date()); + + if ( + !startDate || + !endDate || + isNaN(startDate.getTime()) || // this checks if the parsed date is not valid (eg. wrong format) + isNaN(endDate.getTime()) + ) { + console.error("WARNING WITH INVALID DATES:", warning); + return; // skip to next warning in loop, this one is invalid + } + startDate = getLocalDateFromEST(startDate); + endDate = getLocalDateFromEST(endDate); + + // check if current time is in warning range + if ( + startDate.getTime() < today.getTime() && + endDate.getTime() > today.getTime() + ) { + if (currentWarning) { + console.error( + "ERROR: TWO WARNINGS SCHEDULED AT SAME TIME! ", + currentWarning, + warning + ); + } + currentWarning = warning; + } }); - if (!warning || !warning.content || warning.content == "") { + return currentWarning; +} + +export function WarningHeader() { + const warning = getCurrentWarning(); + + if (!warning || warning.message == "") { return (
{warning.content}
; + return
{warning.message}
; } diff --git a/content/warnings/warnings.json b/content/warnings/warnings.json new file mode 100644 index 00000000..abf818a1 --- /dev/null +++ b/content/warnings/warnings.json @@ -0,0 +1,12 @@ +[ + { + "startDate": "February 15 2022 00:00", + "endDate": "March 25 2022 18:00", + "message": "This is test warning in FEB!" + }, + { + "startDate": "January 29 2022 21:00", + "endDate": "January 30 2022 18:00", + "message": "This is a sample warning" + } +] \ No newline at end of file