Warning Header (Closes #205) #394
|
@ -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";
|
||||
|
||||
snedadah marked this conversation as resolved
|
||||
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;
|
||||
}
|
||||
snedadah marked this conversation as resolved
a258wang
commented
Do we really need this comment here? I personally think the comment on line 25, along with the general structure of the code, are already sufficient to convey what is happening and why. Do we really need this comment here? I personally think the comment on line 25, along with the general structure of the code, are already sufficient to convey what is happening and why.
|
||||
|
||||
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>;
|
||||
}
|
||||
|
|
|
@ -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
We should probably move these functions to utils in that case, and then make both events and this file import from there