diff --git a/src/applications/ask-va/config/helpers.jsx b/src/applications/ask-va/config/helpers.jsx index baeefa6c0427..c2f334bd5eb1 100644 --- a/src/applications/ask-va/config/helpers.jsx +++ b/src/applications/ask-va/config/helpers.jsx @@ -1,5 +1,6 @@ import { format, isValid, parse } from 'date-fns'; -import { utcToZonedTime } from 'date-fns-tz'; +import { formatInTimeZone } from 'date-fns-tz'; +import { enUS } from 'date-fns/locale'; import React from 'react'; import { @@ -691,15 +692,31 @@ export const getDescriptiveTextFromCRM = status => { // Function to convert date to Response Inbox format using date-fns export const convertDateForInquirySubheader = dateString => { // Parse the input date string as UTC - const utcDate = parse(dateString, 'MM/dd/yyyy h:mm:ss a', new Date()); + let utcDate; + try { + utcDate = parse(dateString, 'M/d/yyyy h:mm:ss a', new Date(0)); + utcDate.setUTCFullYear(utcDate.getFullYear()); + utcDate.setUTCMonth(utcDate.getMonth()); + utcDate.setUTCDate(utcDate.getDate()); + utcDate.setUTCHours(utcDate.getHours()); + utcDate.setUTCMinutes(utcDate.getMinutes()); + utcDate.setUTCSeconds(utcDate.getSeconds()); + } catch (error) { + return 'Invalid Date'; + } - // Convert UTC to Eastern Time - const easternTime = utcToZonedTime(utcDate, 'America/New_York'); + // Ensure the date is valid + if (isNaN(utcDate.getTime())) { + return 'Invalid Date'; + } - // Format the date in Eastern Time - return format(easternTime, "MMM. d, yyyy 'at' h:mm aaaa 'E.T'", { - timeZone: 'America/New_York', - }).replace(/AM|PM/, match => `${match.toLowerCase()}.`); + // Format the UTC date in Eastern Time + return formatInTimeZone( + utcDate, + 'America/New_York', + "MMM. d, yyyy 'at' h:mm aaaa 'E.T'", + { locale: enUS }, + ).replace(/AM|PM/, match => `${match.toLowerCase()}.`); }; export const formatDate = (dateString, formatType = 'short') => { diff --git a/src/applications/ask-va/containers/ResponseInboxPage.jsx b/src/applications/ask-va/containers/ResponseInboxPage.jsx index 481daa954d9a..b7d3ea641bb2 100644 --- a/src/applications/ask-va/containers/ResponseInboxPage.jsx +++ b/src/applications/ask-va/containers/ResponseInboxPage.jsx @@ -7,6 +7,7 @@ import { import { isLoggedIn } from '@department-of-veterans-affairs/platform-user/selectors'; import { apiRequest } from '@department-of-veterans-affairs/platform-utilities/api'; import { focusElement } from '@department-of-veterans-affairs/platform-utilities/ui'; +import { parse } from 'date-fns'; import DOMPurify from 'dompurify'; import PropTypes from 'prop-types'; import React, { useCallback, useEffect, useState } from 'react'; @@ -37,6 +38,7 @@ const emptyMessage = message => ( const getReplySubHeader = messageType => { if (!messageType) return 'No messageType'; if (messageType === 'ResponseFromVA') return 'Response from VA'; + if (messageType === 'ReplyToVA') return 'Reply to VA'; // Split the string at capital letters and join with spaces return messageType.split(/(?=[A-Z])/).join(' '); }; @@ -232,21 +234,39 @@ const ResponseInboxPage = ({ router }) => { ), messageType: 'Your question', description: inquiryData.attributes.submitterQuestion, + originalCreatedOn: inquiryData.attributes.createdOn, }, }, - ...inquiryData.attributes.correspondences.data - .filter(corr => corr.attributes.messageType !== 'Notification') - .map(corr => ({ - ...corr, - attributes: { - ...corr.attributes, - createdOn: convertDateForInquirySubheader(corr.attributes.createdOn), - modifiedOn: convertDateForInquirySubheader( - corr.attributes.modifiedOn, - ), - }, - })), - ]; + ...(inquiryData.attributes.correspondences.data + ? inquiryData.attributes.correspondences.data + .filter(corr => corr.attributes.messageType !== 'Notification') + .map(corr => ({ + ...corr, + attributes: { + ...corr.attributes, + createdOn: convertDateForInquirySubheader( + corr.attributes.createdOn, + ), + modifiedOn: convertDateForInquirySubheader( + corr.attributes.modifiedOn, + ), + originalCreatedOn: corr.attributes.createdOn, + }, + })) + : []), + ].sort((a, b) => { + const dateA = parse( + a.attributes.originalCreatedOn, + 'MM/dd/yyyy h:mm:ss a', + new Date(), + ); + const dateB = parse( + b.attributes.originalCreatedOn, + 'MM/dd/yyyy h:mm:ss a', + new Date(), + ); + return dateA.getTime() - dateB.getTime(); + }); return (