Skip to content

Commit

Permalink
Replacing CustomCardContainer.tsx with CustomCardHelper.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Aug 28, 2024
1 parent 70daab2 commit 6a28354
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 103 deletions.
69 changes: 0 additions & 69 deletions packages/lib/storybook/stories/CustomCardContainer.tsx

This file was deleted.

21 changes: 5 additions & 16 deletions packages/lib/storybook/stories/cards/CustomCard.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import { MetaConfiguration, PaymentMethodStoryProps, StoryConfiguration } from '../types';
import { getStoryContextCheckout } from '../../utils/get-story-context-checkout';
import { MetaConfiguration, StoryConfiguration } from '../types';
import { CustomCardConfiguration } from '../../../src/components/CustomCard/types';
import { CustomCard } from '../../../src';
import { CustomCardContainer } from '../CustomCardContainer';
import './customCardHelpers/customCard.style.scss';
import { styles, setFocus, onBrand, onConfigSuccess, onBinLookup, onChange, setCCErrors } from './customCardHelpers/customCard.config';
import { CustomCardHelper } from './customCardHelpers/CustomCardHelper';

type customCardStory = StoryConfiguration<CustomCardConfiguration>;

const meta: MetaConfiguration<CustomCardConfiguration> = {
title: 'Cards/Custom Card'
};

const createComponent = (args: PaymentMethodStoryProps<CustomCardConfiguration>, context) => {
const { componentConfiguration } = args;
const checkout = getStoryContextCheckout(context);
const customCard = new CustomCard(checkout, componentConfiguration);

globalThis.customCard = customCard;
globalThis.parent.window['customCard'] = customCard; // expose to top level window, so a user can access window.customCard

return <CustomCardContainer element={customCard} contextArgs={args} />;
};

export const Default: customCardStory = {
render: createComponent,
render: args => {
return <CustomCardHelper contextArgs={args} />;
},
args: {
componentConfiguration: {
brands: ['mc', 'visa', 'amex', 'bcmc', 'maestro', 'cartebancaire', 'synchrony_plcc'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { useEffect, useRef, useState } from 'preact/hooks';
import { createAdvancedFlowCheckout } from '../../../helpers/create-advanced-checkout';
import { createSessionsCheckout } from '../../../helpers/create-sessions-checkout';
import { CustomCard } from '../../../../src';
import { setUpUtils, createPayButton } from './customCard.utils';

export const CustomCardHelper = ({ contextArgs }) => {
const container = useRef(null);
const checkout = useRef(null);
const [element, setElement] = useState(null);
const [errorMessage, setErrorMessage] = useState(null);

const createCheckout = async () => {
const { useSessions, showPayButton, countryCode, shopperLocale, amount } = contextArgs;

checkout.current = useSessions
? await createSessionsCheckout({ showPayButton, countryCode, shopperLocale, amount })
: await createAdvancedFlowCheckout({
showPayButton,
countryCode,
shopperLocale,
amount
});

const customCard = new CustomCard(checkout.current, { ...contextArgs.componentConfiguration });

setElement(customCard);

// Custom card specific
globalThis.customCard = customCard; // customCard..config & customCard.utils access the component through Storybook's globalThis
globalThis.parent.window['customCard'] = customCard; // expose to top level window, so a user can access window.customCard

setUpUtils(contextArgs, container);
globalThis.payBtn = createPayButton('.secured-fields', customCard, 'customcard');
};

useEffect(() => {
void createCheckout();
}, [contextArgs]);

useEffect(() => {
if (element?.isAvailable) {
element
.isAvailable()
.then(() => {
element.mount(container.current);
})
.catch(error => {
setErrorMessage(error.toString());
});
} else if (element) {
element.mount(container.current);
}
}, [element]);

return (
<div>
{errorMessage ? (
<div>{errorMessage}</div>
) : (
<div id={'topLevelHolder'}>
<div
ref={container}
id="component-root"
className="component-wrapper secured-fields"
// @ts-ignore just hiding for better UX experience
style={'display:none;'}
>
<span className="pm-image">
<img
className="pm-image-1"
width="40"
src="https://checkoutshopper-test.adyen.com/checkoutshopper/images/logos/nocard.svg"
alt="card"
/>
</span>
<span className="pm-image-dual">
<img className="pm-image-dual-1" width="40" alt="" />
<img className="pm-image-dual-2" width="40" alt="" />
</span>
<div className="pm-form-label">
<span className="pm-form-label__text">Card number:</span>
<span className="pm-input-field" data-cse="encryptedCardNumber" data-uid="adyen-checkout-encryptedCardNumber-1"></span>
<span className="pm-form-label__error-text">Please enter a valid credit card number</span>
</div>
<div className="pm-form-label pm-form-label--exp-month">
<span className="pm-form-label__text">Expiry month:</span>
<span className="pm-input-field" data-cse="encryptedExpiryMonth" data-uid="adyen-checkout-encryptedExpiryMonth-2"></span>
<span className="pm-form-label__error-text">Date error text</span>
</div>
<div className="pm-form-label pm-form-label--exp-year">
<span className="pm-form-label__text">Expiry year:</span>
<span className="pm-input-field" data-cse="encryptedExpiryYear" data-uid="adyen-checkout-encryptedExpiryYear-3"></span>
<span className="pm-form-label__error-text">Date error text</span>
</div>
<div className="pm-form-label pm-form-label--cvc">
<span className="pm-form-label__text">CVV/CVC:</span>
<span className="pm-input-field" data-cse="encryptedSecurityCode"></span>
<span className="pm-form-label__error-text">CVC Error text</span>
</div>
</div>
<div className="card-input__spinner__holder">
<div className="card-input__spinner card-input__spinner--active">
<div className="adyen-checkout__spinner__wrapper ">
<div className="adyen-checkout__spinner adyen-checkout__spinner--large"></div>
</div>
</div>
</div>
</div>
)}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ export function onConfigSuccess(pCallbackObj) {
globalThis.customCard.setFocusOn('encryptedCardNumber');
}, 100);

// window.customCard.updateStyles({
// base: {
// color: '#000',
//// fontSize: '18px',
//// lineHeight: '18px'
// },
// error: {
// color: 'orange'
// },
// validated: {
// color: 'blue',
// fontWeight: 'bold'
// },
// placeholder: {
// color: 'green'
// }
// });
// globalThis.customCard.updateStyles({
// base: {
// color: '#000'
// // fontSize: '18px',
// // lineHeight: '18px'
// },
// error: {
// color: 'orange'
// },
// validated: {
// color: 'blue',
// fontWeight: 'bold'
// },
// placeholder: {
// color: 'green'
// }
// });
}

export function setCCErrors(pCallbackObj) {
Expand Down Expand Up @@ -194,7 +194,7 @@ export function onBrand(pCallbackObj) {
}

function dualBrandListener(e) {
window.customCard.dualBrandingChangeHandler(e);
globalThis.customCard.dualBrandingChangeHandler(e);
}

function resetDualBranding(rootNode) {
Expand Down

0 comments on commit 6a28354

Please sign in to comment.