Skip to content

Commit

Permalink
ORV2-2991 - Fix application form/review incorrect datetime display (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zgong-gov authored Nov 8, 2024
1 parent c2c644b commit c57aedc
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 27 deletions.
28 changes: 17 additions & 11 deletions frontend/src/common/helpers/formatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ export const now = () => dayjs();
export const nowUtc = () => dayjs.utc();

/**
* Get local datetime string in a specified format for a given DayJS object.
* @param dayjsObj DayJS object that could be in any timezone
* Get local datetime string in a specified format for a given local DayJS object.
* @param localDayjs Local DayJS object
* @param formatStr datetime format to display the datetime in (default ISO-8601)
* @returns datetime string representing local datetime in the format specified
*/
export const dayjsToLocalStr = (dayjsObj: Dayjs, formatStr?: string) =>
dayjs(dayjsObj).local().format(formatStr);
export const dayjsToLocalStr = (localDayjs: Dayjs, formatStr?: string) =>
dayjs(localDayjs).format(formatStr);

/**
* Get UTC datetime string in a specified format for a given DayJS object.
* @param dayjsObj DayJS object that could be in any timezone
* Get UTC datetime string in a specified format for a given local DayJS object.
* @param localDayjs Local DayJS object
* @param formatStr datetime format to display the datetime in (default ISO-8601)
* @returns datetime string representing UTC datetime in the format specified
*/
export const dayjsToUtcStr = (dayjsObj: Dayjs, formatStr?: string) =>
dayjs(dayjsObj).utc().format(formatStr);
export const dayjsToUtcStr = (localDayjs: Dayjs, formatStr?: string) =>
dayjs(localDayjs).utc().format(formatStr);

/**
* Get UTC datetime string in a specified format for a given datetime string
Expand All @@ -65,10 +65,16 @@ export const toUtc = (dateTimeStr: string, formatStr?: string) =>
* Get local datetime string in a specified format for a given datetime string
* @param dateTimeStr datetime string that could be in any timezone
* @param formatStr datetime format to display in (default ISO-8601)
* @param isDateTimeStrLocal Whether or not the provided datetime string is already local
* @returns datetime string representing local datetime in the format specified
*/
export const toLocal = (dateTimeStr: string, formatStr?: string) =>
dayjs(dateTimeStr).local().format(formatStr);
export const toLocal = (
dateTimeStr: string,
formatStr?: string,
isDateTimeStrLocal?: boolean,
) => isDateTimeStrLocal
? dayjs(dateTimeStr).format(formatStr)
: dayjs(dateTimeStr).local().format(formatStr);

/**
* Get local DayJS object for a given UTC datetime string
Expand Down Expand Up @@ -106,7 +112,7 @@ export const toTimeZone = (
) =>
ianaId
? dayjs(datetimeStr).tz(ianaId).format(formatStr)
: toLocal(datetimeStr, formatStr);
: toLocal(datetimeStr, formatStr, true);

/**
* Gets the number of days between two datetimes (should both be in the same timezone).
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/common/helpers/tableHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import { PermitListItem } from "../../features/permits/types/permit";
import { Nullable } from "../types/common";

/**
* Format a given datetime string to a format that we can display
* @param rawDateTime
* Format a datetime string in a table cell to a given display format.
* @param rawDateTime Provided datetime string, if any
* @param isDateTimeLocal Whether or not the provided datetime is local
* @returns datetime string for display or "NA" if invalid date given
*/
export const formatCellValuetoDatetime = (rawDateTime: Nullable<string>) => {
export const formatCellValuetoDatetime = (
rawDateTime: Nullable<string>,
isDateTimeLocal?: boolean,
) => {
return applyWhenNotNullable(
(dt) => toLocal(dt, DATE_FORMATS.DATEONLY_ABBR_MONTH),
(dt) => toLocal(dt, DATE_FORMATS.DATEONLY_ABBR_MONTH, isDateTimeLocal),
rawDateTime,
"NA",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const PermitSearchResultColumnDef = (
enableSorting: true,
sortingFn: dateTimeStringSortingFn,
Cell: (props: { cell: any }) => {
const formattedDate = formatCellValuetoDatetime(props.cell.getValue());
const formattedDate = formatCellValuetoDatetime(props.cell.getValue(), true);
return formattedDate;
},
},
Expand All @@ -115,7 +115,7 @@ export const PermitSearchResultColumnDef = (
enableSorting: true,
sortingFn: dateTimeStringSortingFn,
Cell: (props: { cell: any }) => {
const formattedDate = formatCellValuetoDatetime(props.cell.getValue());
const formattedDate = formatCellValuetoDatetime(props.cell.getValue(), true);
return formattedDate;
},
},
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/features/permits/apiManager/permitsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export const getApplications = async (
startDate: toLocal(
application?.startDate,
DATE_FORMATS.DATEONLY_SHORT_NAME,
true,
),
} as ApplicationListItem;
});
Expand Down Expand Up @@ -494,10 +495,12 @@ export const getPermits = async (
startDate: toLocal(
permit.startDate,
DATE_FORMATS.DATEONLY_SHORT_NAME,
true,
),
expiryDate: toLocal(
permit.expiryDate,
DATE_FORMATS.DATEONLY_SHORT_NAME,
true,
),
} as PermitListItem;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const PermitsColumnDefinition = (
header: "Permit Start Date",
enableSorting: true,
Cell: (props: { cell: any }) => {
const formattedDate = formatCellValuetoDatetime(props.cell.getValue());
const formattedDate = formatCellValuetoDatetime(props.cell.getValue(), true);
return formattedDate;
},
},
Expand All @@ -81,7 +81,7 @@ export const PermitsColumnDefinition = (
id: "expiryDate",
enableSorting: true,
Cell: (props: { cell: any }) => {
const formattedDate = formatCellValuetoDatetime(props.cell.getValue());
const formattedDate = formatCellValuetoDatetime(props.cell.getValue(), true);
return formattedDate;
},
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/permits/helpers/equality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PermitVehicleDetails } from "../types/PermitVehicleDetails";
import { PermitData } from "../types/PermitData";
import { PermitCondition } from "../types/PermitCondition";
import { arePermitLOADetailsEqual, PermitLOA } from "../types/PermitLOA";
import { doUniqueArraysHaveSameObjects } from "../../../common/helpers/equality";
import {
DATE_FORMATS,
dayjsToLocalStr,
} from "../../../common/helpers/formatDate";
import { doUniqueArraysHaveSameObjects } from "../../../common/helpers/equality";

/**
* Compare whether or not two mailing addresses are equal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useFetchSpecialAuthorizations } from "../../../../settings/hooks/specia
import { filterLOAsForPermitType, filterNonExpiredLOAs } from "../../../helpers/permitLOA";
import {
dayjsToUtcStr,
nowUtc,
now,
} from "../../../../../common/helpers/formatDate";

import {
Expand Down Expand Up @@ -213,7 +213,7 @@ export const AmendPermitForm = () => {
comment: getDefaultRequiredVal("", history.comment),
name: history.commentUsername,
revisionDateTime: getDefaultRequiredVal(
dayjsToUtcStr(nowUtc()),
dayjsToUtcStr(now()),
history.transactionSubmitDate,
),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const LOATable = ({
scope="row"
>
{applyWhenNotNullable(
expiryDate => toLocal(expiryDate, DATE_FORMATS.DATEONLY_SLASH),
expiryDate => toLocal(expiryDate, DATE_FORMATS.DATEONLY_SLASH, true),
selectableLOA.loa.expiryDate,
"Never expires",
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const ShoppingCartItem = ({
<span
className="shopping-cart-item__info shopping-cart-item__info--start-date"
>
{toLocal(cartItemData.startDate, DATE_FORMATS.DATEONLY_ABBR_MONTH)}
{toLocal(cartItemData.startDate, DATE_FORMATS.DATEONLY_ABBR_MONTH, true)}
</span>
</div>
</div>
Expand Down Expand Up @@ -113,7 +113,7 @@ export const ShoppingCartItem = ({
<span
className="shopping-cart-item__info shopping-cart-item__info--end-date"
>
{toLocal(cartItemData.expiryDate, DATE_FORMATS.DATEONLY_ABBR_MONTH)}
{toLocal(cartItemData.expiryDate, DATE_FORMATS.DATEONLY_ABBR_MONTH, true)}
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const VoidPermitHeader = ({ permit }: { permit: Nullable<Permit> }) => {
{toLocal(
permit.permitData.startDate,
DATE_FORMATS.DATEONLY_ABBR_MONTH,
true,
)}
</Box>
</Box>
Expand All @@ -62,6 +63,7 @@ export const VoidPermitHeader = ({ permit }: { permit: Nullable<Permit> }) => {
{toLocal(
permit.permitData.expiryDate,
DATE_FORMATS.DATEONLY_ABBR_MONTH,
true,
)}
</Box>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const LOAListColumnDef = (
},
{
accessorFn: (originalRow) => {
return toLocal(originalRow.startDate, DATE_FORMATS.DATEONLY_SLASH);
return toLocal(originalRow.startDate, DATE_FORMATS.DATEONLY_SLASH, true);
},
id: "startDate",
header: "Start Date",
Expand All @@ -54,7 +54,7 @@ export const LOAListColumnDef = (
{
accessorFn: (originalRow) =>
applyWhenNotNullable(
(expiryDate) => toLocal(expiryDate, DATE_FORMATS.DATEONLY_SLASH),
(expiryDate) => toLocal(expiryDate, DATE_FORMATS.DATEONLY_SLASH, true),
originalRow.expiryDate,
"Never expires",
) as string,
Expand Down

0 comments on commit c57aedc

Please sign in to comment.