migrated warning system to json file
This commit is contained in:
parent
3d6073ab99
commit
ae833a912c
|
@ -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 { GetStaticProps } from "next";
|
||||||
import { Warning } from "pages/api/currentWarning";
|
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
|
|
||||||
import { getCurrentTerm } from "@/lib/events";
|
import { getCurrentTerm } from "@/lib/events";
|
||||||
|
|
||||||
|
import warnings from "../content/warnings/warnings.json";
|
||||||
|
|
||||||
import styles from "./WarningHeader.module.css";
|
import styles from "./WarningHeader.module.css";
|
||||||
|
|
||||||
export function WarningHeader() {
|
// Had to add these functions here, since we cannot use import the file from ./lib/events since that uses libraries only on the serverside
|
||||||
// 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_DATE_FORMAT = "MMMM dd yyyy HH:mm";
|
||||||
const [warning, setWarning] = useState<Warning | null>(null);
|
function getLocalDateFromEST(date: Date) {
|
||||||
|
return utcToZonedTime(
|
||||||
|
// The parsed date is in EST
|
||||||
|
zonedTimeToUtc(date, "America/Toronto"),
|
||||||
|
Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
interface Warning {
|
||||||
fetch("/api/currentWarning/")
|
message: string;
|
||||||
.then((res) => res.json())
|
startDate: string;
|
||||||
.then((data) => {
|
endDate: string;
|
||||||
setWarning(data);
|
}
|
||||||
})
|
|
||||||
.catch((err) => {
|
function getCurrentWarning(): Warning | null {
|
||||||
console.error(err);
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
className={styles.warning}
|
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>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in New Issue