migrated warning system to json file

This commit is contained in:
Shahan Nedadahandeh 2022-02-15 03:32:05 -05:00
parent 3d6073ab99
commit ae833a912c
2 changed files with 80 additions and 15 deletions

View File

@ -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<Warning | null>(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 (
<div
className={styles.warning}
@ -30,5 +83,5 @@ export function WarningHeader() {
);
}
return <div className={styles.warning}>{warning.content}</div>;
return <div className={styles.warning}>{warning.message}</div>;
}

View File

@ -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"
}
]