Warning Header (Closes #205) #394

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

View File

@ -1,21 +1,11 @@
import { parse } from "date-fns";
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
import React from "react";
import warnings from "../content/warnings/warnings.json";
import { DATE_FORMAT, getLocalDateFromEST } from "../utils";
import styles from "./WarningHeader.module.css";
// 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
);
}
interface Warning {
snedadah marked this conversation as resolved
Review

We should probably move these functions to utils in that case, and then make both events and this file import from there

We should probably move these functions to utils in that case, and then make both events and this file import from there
message: string;
startDate: string;
@ -32,8 +22,8 @@ function getCurrentWarning(): Warning | null {
}
// 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());
let startDate = parse(warning.startDate, DATE_FORMAT, new Date());
let endDate = parse(warning.endDate, DATE_FORMAT, new Date());
if (
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.
!startDate ||

View File

@ -2,14 +2,19 @@ import fs from "fs/promises";
import path from "path";
import { parse } from "date-fns";
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
import matter from "gray-matter";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import type { Props } from "../pages/events/[year]/[term]/index";
// do not use alias "@/utils" as generate-calendar imports a function from this file and ts-node is not compatible
import { Term, TERMS, isTerm } from "../utils";
import {
Term,
TERMS,
isTerm,
DATE_FORMAT,
getLocalDateFromEST,
} from "../utils";
const EVENTS_PATH = path.join("content", "events");
@ -55,8 +60,6 @@ export interface Event {
metadata: Metadata;
}
export const DATE_FORMAT = "MMMM dd yyyy HH:mm";
export async function getEventBySlug(
year: string,
term: Term,
@ -284,12 +287,3 @@ function getFutureTerm(year: string, term: Term): { year: string; term: Term } {
term: TERMS[index + 1],
};
}
// The date that's returned should be in local time
export function getLocalDateFromEST(date: Date) {
return utcToZonedTime(
// The parsed date is in EST
zonedTimeToUtc(date, "America/Toronto"),
Intl.DateTimeFormat().resolvedOptions().timeZone
);
}

View File

@ -8,7 +8,7 @@ import { serialize } from "next-mdx-remote/serialize";
import { isTerm, Term, TERMS } from "@/utils";
import { DATE_FORMAT, getLocalDateFromEST } from "./events";
import { DATE_FORMAT, getLocalDateFromEST } from "../utils";
export const NEWS_PATH = path.join("content", "news");

View File

@ -1,5 +1,8 @@
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
export const TERMS = ["winter", "spring", "fall"] as const;
export type Term = typeof TERMS[number];
export const DATE_FORMAT = "MMMM dd yyyy HH:mm";
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
export function isTerm(x: string): x is Term {
@ -9,3 +12,12 @@ export function isTerm(x: string): x is Term {
export function capitalize(str: string) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}
// Converts a date to local time
export function getLocalDateFromEST(date: Date): Date {
return utcToZonedTime(
// The date parameter is in EST
zonedTimeToUtc(date, "America/Toronto"),
Intl.DateTimeFormat().resolvedOptions().timeZone
);
}