Skip to content

Commit

Permalink
fix(project): show all dates in a localized format
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jun 1, 2023
1 parent dedcade commit 5022cdb
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/components/Payment/Payment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TextField from '#components/TextField/TextField';
import LoadingOverlay from '#components/LoadingOverlay/LoadingOverlay';
import Button from '#components/Button/Button';
import type { Customer } from '#types/account';
import { formatDate, formatPrice } from '#src/utils/formatting';
import { formatLocalizedDate, formatPrice } from '#src/utils/formatting';
import { addQueryParam } from '#src/utils/location';
import type { PaymentDetail, Subscription, Transaction } from '#types/subscription';
import type { AccessModel } from '#types/Config';
Expand Down Expand Up @@ -57,7 +57,7 @@ const Payment = ({
onUpgradeSubscriptionClick,
offerSwitchesAvailable,
}: Props): JSX.Element => {
const { t } = useTranslation(['user', 'account']);
const { t, i18n } = useTranslation(['user', 'account']);
const hiddenTransactionsCount = transactions ? transactions?.length - VISIBLE_TRANSACTIONS : 0;
const hasMoreTransactions = hiddenTransactionsCount > 0;
const navigate = useNavigate();
Expand Down Expand Up @@ -108,8 +108,8 @@ const Payment = ({
<p>
<strong>{getTitle(activeSubscription.period)}</strong> <br />
{activeSubscription.status === 'active' && !isGrantedSubscription
? t('user:payment.next_billing_date_on', { date: formatDate(activeSubscription.expiresAt) })
: t('user:payment.subscription_expires_on', { date: formatDate(activeSubscription.expiresAt) })}
? t('user:payment.next_billing_date_on', { date: formatLocalizedDate(new Date(activeSubscription.expiresAt * 1000), i18n.language) })
: t('user:payment.subscription_expires_on', { date: formatLocalizedDate(new Date(activeSubscription.expiresAt), i18n.language) })}
</p>
{!isGrantedSubscription && (
<p className={styles.price}>
Expand Down Expand Up @@ -193,7 +193,7 @@ const Payment = ({
<p>
{transaction.transactionId}
<br />
{formatDate(transaction.transactionDate)}
{formatLocalizedDate(new Date(transaction.transactionDate * 1000), i18n.language)}
</p>
{canShowReceipts && (
<IconButton aria-label={t('user:payment.show_receipt')} onClick={() => !isLoading && onShowReceiptClick(transaction.transactionId)}>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Payment/__snapshots__/Payment.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exports[`<Payment> > renders and matches snapshot 1`] = `
<p>
T712014024
<br />
5/5/2021
May 5, 2021
</p>
</div>
</div>
Expand All @@ -102,7 +102,7 @@ exports[`<Payment> > renders and matches snapshot 1`] = `
<p>
T177974068
<br />
5/5/2021
May 5, 2021
</p>
</div>
</div>
Expand All @@ -125,7 +125,7 @@ exports[`<Payment> > renders and matches snapshot 1`] = `
<p>
T996601696
<br />
5/5/2021
May 5, 2021
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import styles from './RenewSubscriptionForm.module.scss';

import Button from '#components/Button/Button';
import { formatDate, formatPrice } from '#src/utils/formatting';
import { formatLocalizedDate, formatPrice } from '#src/utils/formatting';
import FormFeedback from '#components/FormFeedback/FormFeedback';
import type { Customer } from '#types/account';
import type { Subscription } from '#types/subscription';
Expand All @@ -19,7 +19,7 @@ type Props = {
};

const RenewSubscriptionForm: React.FC<Props> = ({ subscription, customer, error, submitting, onConfirm, onClose }: Props) => {
const { t } = useTranslation('account');
const { t, i18n } = useTranslation('account');

return (
<div>
Expand All @@ -29,7 +29,7 @@ const RenewSubscriptionForm: React.FC<Props> = ({ subscription, customer, error,
<div className={styles.infoBox}>
<p>
<strong>{subscription.offerTitle}</strong> <br />
{t('renew_subscription.next_billing_date_on', { date: formatDate(subscription.expiresAt) })}
{t('renew_subscription.next_billing_date_on', { date: formatLocalizedDate(new Date(subscription.expiresAt * 1000), i18n.language) })}
</p>
<p className={styles.price}>
<strong>{formatPrice(subscription.nextPaymentPrice, subscription.nextPaymentCurrency, customer.country)}</strong>
Expand Down
6 changes: 3 additions & 3 deletions src/components/SubscriptionRenewed/SubscriptionRenewed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import styles from './SubscriptionRenewed.module.scss';

import Button from '#components/Button/Button';
import { formatDate, formatPrice } from '#src/utils/formatting';
import { formatLocalizedDate, formatPrice } from '#src/utils/formatting';
import type { Subscription } from '#types/subscription';
import type { Customer } from '#types/account';

Expand All @@ -15,8 +15,8 @@ type Props = {
};

const SubscriptionRenewed: React.FC<Props> = ({ onClose, customer, subscription }: Props) => {
const { t } = useTranslation('account');
const date = formatDate(subscription.expiresAt);
const { t, i18n } = useTranslation('account');
const date = formatLocalizedDate(new Date(subscription.expiresAt * 1000), i18n.language);
const price = formatPrice(subscription.nextPaymentPrice, subscription.nextPaymentCurrency, customer.country);

return (
Expand Down
6 changes: 3 additions & 3 deletions src/containers/AccountModal/forms/CancelSubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { useAccountStore } from '#src/stores/AccountStore';
import CancelSubscriptionForm from '#components/CancelSubscriptionForm/CancelSubscriptionForm';
import LoadingOverlay from '#components/LoadingOverlay/LoadingOverlay';
import SubscriptionCancelled from '#components/SubscriptionCancelled/SubscriptionCancelled';
import { formatDate } from '#src/utils/formatting';
import { formatLocalizedDate } from '#src/utils/formatting';
import { removeQueryParam } from '#src/utils/location';
import { updateSubscription } from '#src/stores/AccountController';

const CancelSubscription = () => {
const { t } = useTranslation('account');
const { t, i18n } = useTranslation('account');
const navigate = useNavigate();
const location = useLocation();
const subscription = useAccountStore((s) => s.subscription);
Expand Down Expand Up @@ -42,7 +42,7 @@ const CancelSubscription = () => {
return (
<React.Fragment>
{cancelled ? (
<SubscriptionCancelled expiresDate={formatDate(subscription.expiresAt)} onClose={closeHandler} />
<SubscriptionCancelled expiresDate={formatLocalizedDate(new Date(subscription.expiresAt * 1000), i18n.language)} onClose={closeHandler} />
) : (
<CancelSubscriptionForm onConfirm={cancelSubscriptionConfirmHandler} onCancel={closeHandler} submitting={submitting} error={error} />
)}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/User/__snapshots__/User.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ exports[`User Component tests > Payments Page 1`] = `
<p>
11232
<br />
7/16/2022
July 16, 2022
</p>
</div>
</div>
Expand All @@ -772,7 +772,7 @@ exports[`User Component tests > Payments Page 1`] = `
<p>
11234
<br />
11/9/2022
November 9, 2022
</p>
</div>
</div>
Expand Down
20 changes: 9 additions & 11 deletions src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ export const episodeURL = (episode: PlaylistItem, seriesId?: string, play: boole
seriesId,
});

export const formatDate = (dateString: number) => {
if (!dateString) return '';

return new Date(dateString * 1000).toLocaleDateString('en-US');
};

export const formatPrice = (price: number, currency: string, country: string) => {
return new Intl.NumberFormat(country || undefined, {
style: 'currency',
Expand Down Expand Up @@ -135,10 +129,14 @@ export const formatVideoSchedule = (locale: string, scheduledStart?: Date, sched
return `${formatLocalizedDateTime(scheduledStart, locale, ' • ')} - ${formatLocalizedTime(scheduledEnd, locale)}`;
};

export const formatLocalizedDate = (date: Date, locale: string) =>
new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric' }).format(date);
export const formatLocalizedDate = (date: Date, locale: string) => {
return new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric' }).format(date);
};

export const formatLocalizedTime = (date: Date, locale: string) => new Intl.DateTimeFormat(locale, { hour: 'numeric', minute: 'numeric' }).format(date);
export const formatLocalizedTime = (date: Date, locale: string) => {
return new Intl.DateTimeFormat(locale, { hour: 'numeric', minute: 'numeric' }).format(date);
};

export const formatLocalizedDateTime = (date: Date, locale: string, separator = ' ') =>
`${formatLocalizedDate(date, locale)}${separator}${formatLocalizedTime(date, locale)}`;
export const formatLocalizedDateTime = (date: Date, locale: string, separator = ' ') => {
return `${formatLocalizedDate(date, locale)}${separator}${formatLocalizedTime(date, locale)}`;
};
2 changes: 1 addition & 1 deletion test-e2e/utils/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function addYear(date: Date) {
}

export function formatDate(date: Date) {
return date.toLocaleDateString();
return date.toLocaleDateString('en-US', { day: 'numeric', month: 'long', hour: 'numeric' });
}

export async function finishAndCheckSubscription(I: CodeceptJS.I, billingDate: Date, today: Date, yearlyPrice: string) {
Expand Down

0 comments on commit 5022cdb

Please sign in to comment.