Skip to content

Commit

Permalink
worked on review comments. forced types in drop-in render fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeiroguilherme committed Dec 16, 2024
1 parent 4f6dc07 commit 3a92c58
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
42 changes: 21 additions & 21 deletions packages/lib/src/components/Dropin/components/DropinComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Component, Fragment, h } from 'preact';
import PaymentMethodList from './PaymentMethod/PaymentMethodList';
import Status from './status';
import getOrderStatus from '../../../core/Services/order-status';
import { DropinComponentProps, DropinComponentState, DropinStatusProps, onOrderCancelData } from '../types';
import './DropinComponent.scss';
import { UIElementStatus } from '../../internal/UIElement/types';
import { sanitizeOrder } from '../../internal/UIElement/utils';
import { PaymentAmount } from '../../../types/global-types';
import { ANALYTICS_RENDERED_STR } from '../../../core/Analytics/constants';
import AdyenCheckoutError from '../../../core/Errors/AdyenCheckoutError';
import Button from '../../internal/Button';
import type { DropinComponentProps, DropinComponentState, DropinStatus, DropinStatusProps, onOrderCancelData } from '../types';

export class DropinComponent extends Component<DropinComponentProps, DropinComponentState> {
public state: DropinComponentState = {
Expand Down Expand Up @@ -64,7 +63,7 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo
};
}

public setStatus = (status: UIElementStatus, props: DropinStatusProps = {}) => {
public setStatus = (status: DropinStatus['type'], props: DropinStatusProps = {}) => {
this.setState({ status: { type: status, props } });
};

Expand All @@ -77,6 +76,7 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo

componentDidUpdate(prevProps, prevState) {
if (prevState.status.type !== this.state.status.type && this.state.activePaymentMethod) {
// @ts-ignore TODO: Drop-in has its own 'status' values ('custom' for ex) which differs from regular UIElementStatus. Need to investigate best way to define/use this status variable
this.state.activePaymentMethod.setStatus(this.state.status.type);
}

Expand Down Expand Up @@ -154,9 +154,8 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo

private onOrderCancel: (data: onOrderCancelData) => void;

render(
props,
{
render() {
const {
elements,
fastlanePaymentElement,
instantPaymentElements,
Expand All @@ -165,17 +164,15 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo
activePaymentMethod,
cachedPaymentMethods,
showDefaultPaymentMethodList
}
) {
} = this.state;

const isLoading = status.type === 'loading';
const isRedirecting = status.type === 'redirect';
const hasPaymentMethodsToBeDisplayed = elements?.length || instantPaymentElements?.length || storedPaymentElements?.length;

const showFastlane = !!fastlanePaymentElement && !showDefaultPaymentMethodList;
const hasPaymentMethodsToBeDisplayed = !!(elements?.length || instantPaymentElements?.length || storedPaymentElements?.length);

switch (status.type) {
case 'success':
return <Status.Success message={props?.amount?.value === 0 ? 'resultMessages.preauthorized' : status.props?.message} />;
return <Status.Success message={this.props?.amount?.value === 0 ? 'resultMessages.preauthorized' : status.props?.message} />;

case 'error':
return <Status.Error message={status.props?.message} />;
Expand All @@ -189,7 +186,7 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo
{isRedirecting && status.props.component && status.props.component.render()}
{isLoading && status.props && status.props.component && status.props.component.render()}

{showFastlane && (
{!showDefaultPaymentMethodList && (
<Fragment>
<PaymentMethodList
isLoading={isLoading}
Expand All @@ -200,17 +197,20 @@ export class DropinComponent extends Component<DropinComponentProps, DropinCompo
openFirstPaymentMethod
showRadioButton={this.props.showRadioButton}
/>
<Button
classNameModifiers={['dropin-show-paymentmethods']}
variant="link"
inline
label="Other payment methods"
onClick={this.onShowDefaultPaymentMethodListClick}
/>

{hasPaymentMethodsToBeDisplayed && (
<Button
classNameModifiers={['dropin-show-paymentmethods']}
variant="link"
inline
label="Other payment methods"
onClick={this.onShowDefaultPaymentMethodListClick}
/>
)}
</Fragment>
)}

{!!hasPaymentMethodsToBeDisplayed && !showFastlane && (
{hasPaymentMethodsToBeDisplayed && showDefaultPaymentMethodList && (
<PaymentMethodList
isLoading={isLoading || isRedirecting}
isDisablingPaymentMethod={this.state.isDisabling}
Expand Down
11 changes: 6 additions & 5 deletions packages/lib/src/components/Dropin/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Order, OrderStatus, PaymentActionsType, PaymentAmount } from '../../types/global-types';
import type { UIElementProps, UIElementStatus } from '../internal/UIElement/types';
import { StatusFromAction, UIElementProps, UIElementStatus } from '../internal/UIElement/types';
import type { NewableComponent } from '../../core/core.registry';
import type { ICore } from '../../core/types';

Expand Down Expand Up @@ -150,23 +150,24 @@ export interface DropinComponentProps extends DropinConfiguration {
onOrderCancel?: onOrderCancelType;
}

interface DropinStatus {
type: UIElementStatus;
export interface DropinStatus {
type: UIElementStatus | StatusFromAction;
props?: DropinStatusProps;
}

export interface DropinStatusProps {
component?: UIElement;
message?: string;
}

export interface DropinComponentState {
elements: any[];
elements: UIElement[];
fastlanePaymentElement: UIElement[];
instantPaymentElements: UIElement[];
storedPaymentElements: UIElement[];
status: DropinStatus;
activePaymentMethod: UIElement;
cachedPaymentMethods: object;
cachedPaymentMethods: Record<string, boolean>;
showDefaultPaymentMethodList: boolean;
isDisabling: boolean;
orderStatus: OrderStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
justify-content: center;
margin-top: token(spacer-070);
}

.adyen-checkout-fastlane__brand img {
width: 95px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const FastlaneComponent = ({ lastFour, brand, payButton, setComponentRef, showPa
{showPayButton && payButton({ status, icon: getImage({ imageFolder: 'components/' })(`${PREFIX}lock`) })}

<div className="adyen-checkout-fastlane__brand">
<Img width="95px" src={getImage({ imageFolder: 'components/' })(`paypal_fastlane_gray`)} alt="Fastlane logo" />
<Img src={getImage({ imageFolder: 'components/' })(`paypal_fastlane_gray`)} alt="Fastlane logo" />
</div>
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion packages/lib/src/components/internal/UIElement/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type CoreCallbacks = Pick<
| 'onEnterKeyPressed'
>;

export type StatusFromAction = 'redirect' | 'loading' | 'custom';

export type UIElementProps = BaseElementProps &
CoreCallbacks & {
environment?: string;
Expand Down Expand Up @@ -57,7 +59,7 @@ export type UIElementProps = BaseElementProps &
* Status set when creating the Component from action
* @internal
*/
statusType?: 'redirect' | 'loading' | 'custom';
statusType?: StatusFromAction;

type?: string;
name?: string;
Expand Down

0 comments on commit 3a92c58

Please sign in to comment.