Skip to content

Commit

Permalink
GAP-2521 | remove react-moment from PreviewSearchResultCard due to de…
Browse files Browse the repository at this point in the history
…p issue
  • Loading branch information
ConorFayleAND committed Apr 18, 2024
1 parent b674b48 commit 899e4fa
Showing 1 changed file with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'moment-timezone';
import Link from 'next/link';
import Moment from 'react-moment';
import React from 'react';
import moment from 'moment';
import Link from 'next/link';
import moment from 'moment-timezone';

const PreviewSearchResultCard = ({ grant }: PreviewSearchResultCardProps) => {
const { adjustedOpenDate, adjustedCloseDate } = adjustDate(
Expand Down Expand Up @@ -113,13 +111,7 @@ const PreviewSearchResultCard = ({ grant }: PreviewSearchResultCardProps) => {
data-testid="ApplicationOpenDateValue"
>
{grant.grantApplicationOpenDate.length ? (
<Moment
format="D MMMM YYYY, h:mma"
tz="GMT"
data-testid="openDate"
>
{adjustedOpenDate}
</Moment>
<time data-testid="openDate">{adjustedOpenDate}</time>
) : (
''
)}
Expand All @@ -137,13 +129,7 @@ const PreviewSearchResultCard = ({ grant }: PreviewSearchResultCardProps) => {
data-testid="ApplicationCloseDateValue"
>
{grant.grantApplicationCloseDate.length ? (
<Moment
format="D MMMM YYYY, h:mma"
tz="GMT"
data-testid="closeDate"
>
{adjustedCloseDate}
</Moment>
<time data-testid="closeDate">{adjustedCloseDate}</time>
) : (
''
)}
Expand Down Expand Up @@ -171,19 +157,34 @@ const parseDate = (date: string[]) => {
};

function adjustDate(openDate: string[], closeDate: string[]) {
const openDateFormatted = moment(parseDate(openDate));
const closeDateFormatted = moment(parseDate(closeDate));
const openDateMoment = moment(parseDate(openDate));
const closeDateMoment = moment(parseDate(closeDate));

const adjustedOpenDate = moment.utc(openDateFormatted).startOf('day');
const adjustedCloseDate = moment.utc(closeDateFormatted).startOf('day');
const openDateMidnight = moment.utc(openDateMoment).startOf('day');
const closeDateMidnight = moment.utc(closeDateMoment).startOf('day');

const adjustForMidnightAndFormat = (
date: moment.Moment,
midnight: moment.Moment,
isOpeningDate: boolean
) => {
const newDate = midnight.isSame(moment.utc(date))
? midnight.add(isOpeningDate ? 1 : -1, 'minute')
: moment.utc(date);
return newDate.tz('GMT').format('D MMMM YYYY, h:mma');
};

return {
adjustedOpenDate: adjustedOpenDate.isSame(moment.utc(openDateFormatted))
? adjustedOpenDate.add(1, 'minute')
: moment.utc(openDateFormatted),
adjustedCloseDate: adjustedCloseDate.isSame(moment.utc(closeDateFormatted))
? adjustedCloseDate.subtract(1, 'minute')
: moment.utc(closeDateFormatted),
adjustedOpenDate: adjustForMidnightAndFormat(
openDateMoment,
openDateMidnight,
true
),
adjustedCloseDate: adjustForMidnightAndFormat(
closeDateMoment,
closeDateMidnight,
false
),
};
}

Expand Down

0 comments on commit 899e4fa

Please sign in to comment.