Warning Header (Closes #205) #394

Merged
snedadah merged 19 commits from warning-header into main 2022-02-22 23:29:55 -05:00
1 changed files with 16 additions and 22 deletions
Showing only changes of commit f34687d5cb - Show all commits

View File

@ -14,13 +14,10 @@ interface Warning {
function getCurrentWarning(): Warning | null {
const today = new Date();
let currentWarning: Warning | null = null;
warnings.forEach((warning) => {
const currentWarnings: Warning[] = warnings.filter((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, DATE_FORMAT, new Date());
let endDate = parse(warning.endDate, DATE_FORMAT, new Date());
@ -28,40 +25,37 @@ function getCurrentWarning(): Warning | null {
if (
!startDate ||
!endDate ||
isNaN(startDate.getTime()) || // this checks if the parsed date is not valid (eg. wrong format)
isNaN(startDate.getTime()) || // this checks if the parsed date is not valid (eg. wrong format), since getLocalDateFromEST fails with invalid dates
snedadah marked this conversation as resolved
Review

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.
isNaN(endDate.getTime())
) {
console.error("WARNING WITH INVALID DATES:", warning);
return; // skip to next warning in loop, this one is invalid
return false; // this warning is not valid, dont try to call getLocalDateFromEST
}
startDate = getLocalDateFromEST(startDate);
endDate = getLocalDateFromEST(endDate);
// check if current time is in warning range
if (
// check if current warning is in range
return (
startDate.getTime() < today.getTime() &&
endDate.getTime() > today.getTime()
) {
if (currentWarning) {
console.error(
"ERROR: TWO WARNINGS SCHEDULED AT SAME TIME! ",
currentWarning,
warning
);
}
currentWarning = warning;
}
);
});
return currentWarning;
if (currentWarnings.length > 1) {
console.error(
"ERROR: MORE THAN ONE WARNING SCHEDULED CURRENTLY! ",
currentWarnings
);
}
return currentWarnings.length == 0 ? null : currentWarnings[0];
}
export function WarningHeader() {
const warning = getCurrentWarning();
return !warning || warning.message == "" ? (
<></>
) : (
return warning == null ? null : (
<div className={styles.warning}>{warning.message}</div>
);
}