Skip to content

Commit 1ec5793

Browse files
authored
Merge pull request #847 from paypal/feat/storybook-v6-use-clientid
refactor storybook to use clientId
2 parents 8663d7f + d73a191 commit 1ec5793

File tree

5 files changed

+14
-79
lines changed

5 files changed

+14
-79
lines changed

.github/workflows/build-and-deploy-docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ jobs:
5353

5454
- name: Build v6 react storybook
5555
run: npm run build-storybook:v6
56+
env:
57+
STORYBOOK_PAYPAL_SANDBOX_CLIENT_ID: ${{ secrets.STORYBOOK_PAYPAL_SANDBOX_CLIENT_ID }}
5658

5759
- name: Setup Pages
5860
uses: actions/configure-pages@v5

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ node_modules
1414
*.tgz
1515

1616
.vscode
17+
.env

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

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from "react";
1+
import React, { useEffect } from "react";
22
import type { Decorator } from "@storybook/react";
33
import { action } from "storybook/actions";
44

@@ -8,63 +8,7 @@ import {
88
useEligibleMethods,
99
INSTANCE_LOADING_STATE,
1010
} from "@paypal/react-paypal-js/sdk-v6";
11-
import { SAMPLE_INTEGRATION_API } from "../shared/utils";
12-
13-
async function fetchClientToken(): Promise<string> {
14-
const response = await fetch(
15-
`${SAMPLE_INTEGRATION_API}/paypal-api/auth/browser-safe-client-token`,
16-
);
17-
18-
if (!response.ok) {
19-
throw new Error(`Failed to fetch client token: ${response.status}`);
20-
}
21-
22-
const data = await response.json();
23-
return data.accessToken;
24-
}
25-
26-
function LoadingSpinner() {
27-
return (
28-
<div style={{ padding: "40px", textAlign: "center", color: "#0070ba" }}>
29-
<div
30-
style={{
31-
display: "inline-block",
32-
width: "32px",
33-
height: "32px",
34-
border: "3px solid rgba(0, 112, 186, 0.2)",
35-
borderTopColor: "#0070ba",
36-
borderRadius: "50%",
37-
animation: "v6-spin 0.8s linear infinite",
38-
}}
39-
/>
40-
<p style={{ marginTop: "12px", fontSize: "14px" }}>
41-
Loading PayPal SDK...
42-
</p>
43-
<style>{`@keyframes v6-spin { to { transform: rotate(360deg); } }`}</style>
44-
</div>
45-
);
46-
}
47-
48-
function ErrorDisplay({ message }: { message: string }) {
49-
return (
50-
<div
51-
style={{
52-
padding: "20px",
53-
backgroundColor: "#fef3cd",
54-
border: "1px solid #ffc107",
55-
borderRadius: "4px",
56-
color: "#856404",
57-
}}
58-
>
59-
<strong>Failed to load PayPal SDK</strong>
60-
<p style={{ margin: "8px 0", fontSize: "14px" }}>{message}</p>
61-
<p style={{ margin: 0, fontSize: "12px", color: "#666" }}>
62-
Ensure the sample integration server is running at{" "}
63-
{SAMPLE_INTEGRATION_API}
64-
</p>
65-
</div>
66-
);
67-
}
11+
import { PAYPAL_CLIENT_ID } from "../shared/utils";
6812

6913
// Logs SDK and button events to the Actions panel.
7014
function SdkStatusMonitor({ children }: { children: React.ReactNode }) {
@@ -96,24 +40,9 @@ function SdkStatusMonitor({ children }: { children: React.ReactNode }) {
9640
}
9741

9842
function ProviderWrapper({ children }: { children: React.ReactNode }) {
99-
const [clientToken, setClientToken] = useState<string>();
100-
const [error, setError] = useState<Error>();
101-
102-
useEffect(() => {
103-
fetchClientToken().then(setClientToken).catch(setError);
104-
}, []);
105-
106-
if (error) {
107-
return <ErrorDisplay message={error.message} />;
108-
}
109-
110-
if (!clientToken) {
111-
return <LoadingSpinner />;
112-
}
113-
11443
return (
11544
<PayPalProvider
116-
clientToken={clientToken}
45+
clientId={PAYPAL_CLIENT_ID}
11746
components={[
11847
"paypal-payments",
11948
"venmo-payments",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function onApprove(data) {
2828
export default function App() {
2929
return (
3030
<PayPalProvider
31-
clientToken="YOUR_CLIENT_TOKEN"
31+
clientId="YOUR_CLIENT_ID"
3232
components={["paypal-payments"]}
3333
pageType="checkout"
3434
>
@@ -60,7 +60,7 @@ async function createOrder() {
6060
export default function App() {
6161
return (
6262
<PayPalProvider
63-
clientToken="YOUR_CLIENT_TOKEN"
63+
clientId="YOUR_CLIENT_ID"
6464
components={["venmo-payments"]}
6565
pageType="checkout"
6666
>
@@ -92,7 +92,7 @@ async function createVaultToken() {
9292
export default function App() {
9393
return (
9494
<PayPalProvider
95-
clientToken="YOUR_CLIENT_TOKEN"
95+
clientId="YOUR_CLIENT_ID"
9696
components={["paypal-payments"]}
9797
pageType="checkout"
9898
>
@@ -147,7 +147,7 @@ function Checkout() {
147147
export default function App() {
148148
return (
149149
<PayPalProvider
150-
clientToken="YOUR_CLIENT_TOKEN"
150+
clientId="YOUR_CLIENT_ID"
151151
components={["paypal-payments"]}
152152
pageType="checkout"
153153
>
@@ -171,7 +171,7 @@ async function createOrder() {
171171
export default function App() {
172172
return (
173173
<PayPalProvider
174-
clientToken="YOUR_CLIENT_TOKEN"
174+
clientId="YOUR_CLIENT_ID"
175175
components={["paypal-guest-payments"]}
176176
pageType="checkout"
177177
>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export const SAMPLE_INTEGRATION_API =
2121
import.meta.env.STORYBOOK_PAYPAL_API_URL ||
2222
"https://v6-web-sdk-sample-integration-server.fly.dev";
2323

24+
export const PAYPAL_CLIENT_ID =
25+
import.meta.env.STORYBOOK_PAYPAL_SANDBOX_CLIENT_ID || "";
26+
2427
// One-Time Payment APIs
2528

2629
export async function createOrder(): Promise<{ orderId: string }> {

0 commit comments

Comments
 (0)