refactored WarningHeader

This commit is contained in:
Shahan Nedadahandeh 2022-02-16 03:08:06 -05:00
parent 83e7bc7e57
commit f34687d5cb
1 changed files with 16 additions and 22 deletions

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
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>
);
}