Skip to content

Commit

Permalink
refactor: add strict util for modal urls from window location
Browse files Browse the repository at this point in the history
  • Loading branch information
royschut authored and ChristiaanScheermeijer committed Feb 20, 2024
1 parent cf1532a commit f787a60
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { useEffect, useState } from 'react';
import { useLocation, useNavigate } from 'react-router';
import useCheckout from '@jwp/ott-hooks-react/src/useCheckout';
import { modalURLFromLocation } from '@jwp/ott-ui-react/src/utils/location';
import { modalURLFromLocation, modalURLFromWindowLocation } from '@jwp/ott-ui-react/src/utils/location';
import useForm from '@jwp/ott-hooks-react/src/useForm';
import { createURL } from '@jwp/ott-common/src/utils/urlFormatting';
import { FormValidationError } from '@jwp/ott-common/src/errors/FormValidationError';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -101,9 +100,9 @@ const Checkout = () => {
);
}

const cancelUrl = createURL(window.location.href, { u: 'payment-cancelled' });
const waitingUrl = createURL(window.location.href, { u: 'waiting-for-payment', offerId: selectedOffer?.offerId });
const errorUrl = createURL(window.location.href, { u: 'payment-error' });
const cancelUrl = modalURLFromWindowLocation('payment-cancelled');
const waitingUrl = modalURLFromWindowLocation('waiting-for-payment', { offerId: selectedOffer?.offerId });
const errorUrl = modalURLFromWindowLocation('payment-error');
const successUrlPaypal = offerType === 'svod' ? waitingUrl : closeModalUrl;
const referrer = window.location.href;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import type { AdyenPaymentSession } from '@jwp/ott-common/types/checkout';
import { getModule } from '@jwp/ott-common/src/modules/container';
import AccountController from '@jwp/ott-common/src/controllers/AccountController';
import CheckoutController from '@jwp/ott-common/src/controllers/CheckoutController';
import { modalURLFromLocation } from '@jwp/ott-ui-react/src/utils/location';
import { modalURLFromLocation, modalURLFromWindowLocation } from '@jwp/ott-ui-react/src/utils/location';
import { ADYEN_LIVE_CLIENT_KEY, ADYEN_TEST_CLIENT_KEY } from '@jwp/ott-common/src/constants';
import useQueryParam from '@jwp/ott-ui-react/src/hooks/useQueryParam';
import useEventCallback from '@jwp/ott-hooks-react/src/useEventCallback';
import { createURL } from '@jwp/ott-common/src/utils/urlFormatting';
import { useTranslation } from 'react-i18next';

import Adyen from '../../components/Adyen/Adyen';
Expand Down Expand Up @@ -96,7 +95,7 @@ export default function AdyenPaymentDetails({ setProcessing, type, setPaymentErr
setProcessing(true);
setPaymentError(undefined);

const returnUrl = createURL(window.location.href, { u: 'payment-method', paymentMethodId: `${paymentMethodId}` });
const returnUrl = modalURLFromWindowLocation('payment-method', { paymentMethodId: `${paymentMethodId}` });
const result = await checkoutController.addAdyenPaymentDetails(state.data.paymentMethod, paymentMethodId, returnUrl);

if ('action' in result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { getModule } from '@jwp/ott-common/src/modules/container';
import { useCheckoutStore } from '@jwp/ott-common/src/stores/CheckoutStore';
import { useAccountStore } from '@jwp/ott-common/src/stores/AccountStore';
import CheckoutController from '@jwp/ott-common/src/controllers/CheckoutController';
import { createURL } from '@jwp/ott-common/src/utils/urlFormatting';
import useQueryParam from '@jwp/ott-ui-react/src/hooks/useQueryParam';

import LoadingOverlay from '../../components/LoadingOverlay/LoadingOverlay';
import PaymentMethodForm from '../../components/PaymentMethodForm/PaymentMethodForm';
import PayPal from '../../components/PayPal/PayPal';
import AdyenPaymentDetails from '../AdyenPaymentDetails/AdyenPaymentDetails';
import { modalURLFromWindowLocation } from '../../utils/location';

type Props = {
onCloseButtonClick: () => void;
Expand Down Expand Up @@ -48,10 +48,10 @@ const UpdatePaymentMethod = ({ onCloseButtonClick }: Props) => {
try {
setPaymentError(undefined);

const successUrl = createURL(window.location.href, { u: 'payment-method-success' });
const waitingUrl = createURL(window.location.href, { u: 'waiting-for-payment' });
const cancelUrl = createURL(window.location.href, { u: 'paypal-cancelled' }); // @todo: This route doesn't exist
const errorUrl = createURL(window.location.href, { u: 'paypal-error' }); // @todo: This route doesn't exist
const successUrl = modalURLFromWindowLocation('payment-method-success');
const waitingUrl = modalURLFromWindowLocation('waiting-for-payment');
const cancelUrl = modalURLFromWindowLocation('payment-cancelled');
const errorUrl = modalURLFromWindowLocation('payment-error');

const response = await checkoutController.updatePayPalPaymentMethod(
successUrl,
Expand Down
15 changes: 14 additions & 1 deletion packages/ui-react/src/utils/location.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Location } from 'react-router-dom';

import { createURLFromLocation, modalURLFromLocation } from './location';
import { createURLFromLocation, modalURLFromLocation, modalURLFromWindowLocation } from './location';

describe('url from location', () => {
test('valid url from a location', async () => {
Expand Down Expand Up @@ -115,4 +115,17 @@ describe('modal urls from location', () => {

expect(url).toEqual('/test?test-param=1&u=login&foo=bar');
});

describe('modal urls from window.location', () => {
test('valid modal url from window.location', async () => {
const url = modalURLFromWindowLocation('login');

expect(url).toEqual('http://localhost:3000/?u=login');
});
test('valid modal url from window.location with query params', async () => {
const url = modalURLFromWindowLocation('waiting-for-payment', { offerId: '123' });

expect(url).toEqual('http://localhost:3000/?u=waiting-for-payment&offerId=123');
});
});
});
5 changes: 5 additions & 0 deletions packages/ui-react/src/utils/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export const createURLFromLocation = (location: Location, queryParams: QueryPara
export const modalURLFromLocation = (location: Location, u: keyof AccountModals | null, queryParams?: QueryParamsArg) => {
return createURL(`${location.pathname}${location.search || ''}`, { u, ...queryParams });
};

// Create a full modal url including hostname, mostly for external use (e.g paypal successUrl)
export const modalURLFromWindowLocation = (u: keyof AccountModals, queryParams?: QueryParamsArg) => {
return createURL(window.location.href, { u, ...queryParams });
};

0 comments on commit f787a60

Please sign in to comment.