Skip to content

Commit 4ae175f

Browse files
authored
Merge pull request #814 from paypal/feature/v6-react-storybook-enhancement
Feature/v6 react storybook enhancement
2 parents 581bbb9 + d67ad66 commit 4ae175f

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

packages/react-paypal-js-storybook/v6/.storybook/preview.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { withPayPalProvider } from "../src/decorators";
33

44
const preview: Preview = {
55
parameters: {
6+
actions: {
7+
// Automatically create actions for props matching on* pattern
8+
argTypesRegex: "^on[A-Z].*",
9+
},
610
controls: {
711
matchers: {
812
color: /(background|color)$/i,

packages/react-paypal-js-storybook/v6/src/decorators/PayPalProviderDecorator.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { useState, useEffect } from "react";
22
import type { Decorator } from "@storybook/react";
3+
import { action } from "storybook/actions";
34

4-
import { PayPalProvider } from "@paypal/react-paypal-js/sdk-v6";
5+
import {
6+
PayPalProvider,
7+
usePayPal,
8+
INSTANCE_LOADING_STATE,
9+
} from "@paypal/react-paypal-js/sdk-v6";
510
import { SAMPLE_INTEGRATION_API } from "../shared/utils";
611

712
async function fetchClientToken(): Promise<string> {
@@ -60,6 +65,28 @@ function ErrorDisplay({ message }: { message: string }) {
6065
);
6166
}
6267

68+
// Logs SDK and button events to the Actions panel.
69+
function SdkStatusMonitor({ children }: { children: React.ReactNode }) {
70+
const { loadingStatus } = usePayPal();
71+
72+
useEffect(() => {
73+
if (loadingStatus === INSTANCE_LOADING_STATE.RESOLVED) {
74+
action("SDK")("Library initialized and rendered");
75+
}
76+
}, [loadingStatus]);
77+
78+
const handleClick = (e: React.MouseEvent) => {
79+
const tag = (e.target as HTMLElement).tagName.toLowerCase();
80+
if (tag.endsWith("-button")) {
81+
action("button")(
82+
"Click event dispatched from the PayPal payment button",
83+
);
84+
}
85+
};
86+
87+
return <div onClick={handleClick}>{children}</div>;
88+
}
89+
6390
function ProviderWrapper({ children }: { children: React.ReactNode }) {
6491
const [clientToken, setClientToken] = useState<string>();
6592
const [error, setError] = useState<Error>();
@@ -86,7 +113,7 @@ function ProviderWrapper({ children }: { children: React.ReactNode }) {
86113
]}
87114
pageType="checkout"
88115
>
89-
{children}
116+
<SdkStatusMonitor>{children}</SdkStatusMonitor>
90117
</PayPalProvider>
91118
);
92119
}

packages/react-paypal-js-storybook/v6/src/shared/utils.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
OnCancelDataSavePayments,
1616
OnErrorData,
1717
} from "@paypal/react-paypal-js/sdk-v6";
18+
import { action } from "storybook/actions";
1819

1920
export const SAMPLE_INTEGRATION_API =
2021
import.meta.env.STORYBOOK_PAYPAL_API_URL ||
@@ -31,11 +32,14 @@ export async function createOrder(): Promise<{ orderId: string }> {
3132
return { orderId: data.id };
3233
}
3334

34-
export async function captureOrder(orderId: string): Promise<void> {
35-
await fetch(
35+
export async function captureOrder(
36+
orderId: string,
37+
): Promise<Record<string, unknown>> {
38+
const response = await fetch(
3639
`${SAMPLE_INTEGRATION_API}/paypal-api/checkout/orders/${orderId}/capture`,
3740
{ method: "POST" },
3841
);
42+
return response.json();
3943
}
4044

4145
// Vault/Save Payment APIs
@@ -55,27 +59,29 @@ export async function createVaultToken(): Promise<{ vaultSetupToken: string }> {
5559

5660
export const oneTimePaymentCallbacks = {
5761
onApprove: async (data: OnApproveDataOneTimePayments) => {
58-
console.log("Payment approved:", data);
59-
await captureOrder(data.orderId);
60-
console.log("Order captured successfully");
62+
const orderData = await captureOrder(data.orderId);
63+
action("approve")({
64+
...orderData,
65+
orderID: data.orderId,
66+
});
6167
},
6268
onCancel: (data: OnCancelDataOneTimePayments) => {
63-
console.log("Payment cancelled:", data);
69+
action("cancel")(data);
6470
},
6571
onError: (error: unknown) => {
66-
console.error("Payment error:", error as OnErrorData);
72+
action("error")(error as OnErrorData);
6773
},
6874
};
6975

7076
export const savePaymentCallbacks = {
7177
onApprove: async (data: { vaultSetupToken: string }) => {
72-
console.log("Payment method saved:", data);
78+
action("approve")(data);
7379
},
7480
onCancel: (data: OnCancelDataSavePayments) => {
75-
console.log("Save payment cancelled:", data);
81+
action("cancel")(data);
7682
},
7783
onError: (error: unknown) => {
78-
console.error("Save payment error:", error as OnErrorData);
84+
action("error")(error as OnErrorData);
7985
},
8086
};
8187

0 commit comments

Comments
 (0)