Skip to content

Commit

Permalink
feat(payment): STRIPE-444 New Stripe UPE (PoC)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlenkoM committed Oct 2, 2024
1 parent 1c1eae6 commit dd71610
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { FunctionComponent, memo, useCallback, useMemo } from 'react';

import { connectFormik, ConnectFormikProps } from '../../common/form';
import { isMobile } from '../../common/utility';
import { Checklist, ChecklistItem } from '../../ui/form';
import { Checklist, ChecklistItem, CustomChecklistItem } from '../../ui/form';

import getUniquePaymentMethodId, { parseUniquePaymentMethodId } from './getUniquePaymentMethodId';
import PaymentMethodTitle from './PaymentMethodTitle';
Expand Down Expand Up @@ -116,6 +116,15 @@ const PaymentMethodListItem: FunctionComponent<PaymentMethodListItemProps> = ({
[method],
);

if (method.initializationData?.accordion) { // TODO: accordion key will be changed to more relevant after changes on BCapp side
return (
<CustomChecklistItem
content={renderPaymentMethod}
htmlId={`radio-${value}`}
/>
);
}

return (
<ChecklistItem
content={renderPaymentMethod}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/app/ui/form/CustomChecklistItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { FunctionComponent, memo, ReactNode } from 'react';

export interface CustomChecklistItemProps {
content?: ReactNode;
htmlId?: string;
}

const CustomChecklistItem: FunctionComponent<CustomChecklistItemProps> = ({
content,
htmlId,
}) => {
return (
<li
className="form-checklist-item optimizedCheckout-form-checklist-item custom-checklist-item"
id={htmlId}
>
{content}
</li>
);
};

export default memo(CustomChecklistItem);
1 change: 1 addition & 0 deletions packages/core/src/app/ui/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export { default as CheckboxInput } from './CheckboxInput';
export { default as Label, LabelProps } from './Label';
export { default as Legend, LegendProps } from './Legend';
export { default as ChecklistItemInput, ChecklistItemInputProps } from './ChecklistItemInput';
export { default as CustomChecklistItem, CustomChecklistItemProps } from './CustomChecklistItem';
export { default as DynamicFormField } from './DynamicFormField';
export { default as DynamicFormFieldType } from './DynamicFormFieldType';
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,14 @@
will-change: $loading-skeleton-will-change;
}
}

.custom-checklist-item {
margin: 0;
overflow: hidden;
border-bottom: 0;

.paymentMethod--hosted,
.widget {
padding: 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { PaymentInitializeOptions } from '@bigcommerce/checkout-sdk';
import { noop, some } from 'lodash';
import React, { FunctionComponent, useCallback, useMemo } from 'react';
import React, {
FunctionComponent,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
} from 'react';

import { getAppliedStyles } from '@bigcommerce/checkout/dom-utils';
import { HostedWidgetPaymentComponent } from '@bigcommerce/checkout/hosted-widget-integration';
Expand All @@ -13,6 +20,7 @@ import {
PaymentMethodResolveId,
toResolvableComponent,
} from '@bigcommerce/checkout/payment-integration-api';
import { AccordionContext } from '@bigcommerce/checkout/ui';

const StripeUPEPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
paymentForm,
Expand All @@ -22,9 +30,19 @@ const StripeUPEPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
onUnhandledError = noop,
...rest
}) => {
const collapseStripeElement = useRef<() => void>();
const { onToggle, selectedItemId } = useContext(AccordionContext);
const containerId = `stripe-${method.id}-component-field`;
const paymentContext = paymentForm;

useEffect(() => {
if (selectedItemId?.includes('stripeupe-')) {
return;
}

collapseStripeElement.current?.();
}, [selectedItemId]);

const renderSubmitButton = useCallback(() => {
paymentContext.hidePaymentSubmitButton(method, false);
}, [paymentContext, method]);
Expand Down Expand Up @@ -69,6 +87,10 @@ const StripeUPEPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
return getAppliedStyles(parentContainer, properties);
};

// const accordionCollapseListener = (collapseElement: () => void) => {
// collapseStripeElement.current = collapseElement;
// };

const initializeStripePayment = useCallback(
async (options: PaymentInitializeOptions) => {
const formInput = getStylesFromElement(`${containerId}--input`, [
Expand Down Expand Up @@ -97,6 +119,10 @@ const StripeUPEPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
},
onError: onUnhandledError,
render: renderSubmitButton,
paymentMethodSelect: onToggle,
handleClosePaymentMethod: (collapseElement: () => void) => {
collapseStripeElement.current = collapseElement;
},
},
});
},
Expand All @@ -107,6 +133,7 @@ const StripeUPEPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
method,
paymentContext,
renderSubmitButton,
onToggle,
],
);

Expand Down

0 comments on commit dd71610

Please sign in to comment.