|
1 |
| -import { |
2 |
| - Elements, |
3 |
| - PaymentElement, |
4 |
| - useElements, |
5 |
| - useStripe, |
6 |
| -} from '@stripe/react-stripe-js' |
7 |
| -import { loadStripe } from '@stripe/stripe-js' |
8 | 1 | import React, { useState } from 'react'
|
9 | 2 |
|
10 |
| -import Button from 'ui/Button' |
11 |
| - |
12 | 3 | import AddressForm from '../Address/AddressForm'
|
13 | 4 |
|
14 |
| -// TODO - fetch from API |
15 |
| -const STRIPE_PUBLISHABLE_KEY = process.env.STRIPE_PUBLISHABLE_KEY || '' |
16 |
| -const MANUALLY_FETCHED_CLIENT_SECRET = process.env.STRIPE_CLIENT_SECRET || '' |
17 |
| - |
18 |
| -const stripePromise = loadStripe(STRIPE_PUBLISHABLE_KEY) |
19 |
| - |
20 |
| -interface PaymentFormProps { |
21 |
| - clientSecret: string |
22 |
| -} |
23 |
| - |
24 |
| -const PaymentForm: React.FC<PaymentFormProps> = () => { |
25 |
| - const stripe = useStripe() |
26 |
| - const elements = useElements() |
27 |
| - const [isSubmitting, setIsSubmitting] = useState(false) |
28 |
| - const [errorMessage, setErrorMessage] = useState<string | null>(null) |
29 |
| - |
30 |
| - const handleSubmit = async (event: React.FormEvent) => { |
31 |
| - event.preventDefault() |
32 |
| - setIsSubmitting(true) |
33 |
| - setErrorMessage(null) |
34 |
| - |
35 |
| - if (!stripe || !elements) { |
36 |
| - setErrorMessage('Stripe has not loaded yet. Please try again.') |
37 |
| - setIsSubmitting(false) |
38 |
| - return |
39 |
| - } |
40 |
| - |
41 |
| - const { error } = await stripe.confirmPayment({ |
42 |
| - elements, |
43 |
| - confirmParams: { |
44 |
| - // eslint-disable-next-line camelcase |
45 |
| - return_url: 'https://codecov.io', |
46 |
| - }, |
47 |
| - }) |
48 |
| - |
49 |
| - if (error) { |
50 |
| - setErrorMessage(error.message || 'An unexpected error occurred.') |
51 |
| - setIsSubmitting(false) |
52 |
| - } else { |
53 |
| - setIsSubmitting(false) |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - return ( |
58 |
| - <div> |
59 |
| - <PaymentElement |
60 |
| - options={{ |
61 |
| - layout: 'tabs', |
62 |
| - defaultValues: { |
63 |
| - billingDetails: { |
64 |
| - name: 'John Doe', |
65 |
| - }, |
66 |
| - }, |
67 |
| - }} |
68 |
| - /> |
69 |
| - <div className="mb-8 mt-4 flex gap-1"> |
70 |
| - <Button |
71 |
| - hook="submit-address-update" |
72 |
| - type="submit" |
73 |
| - variant="primary" |
74 |
| - disabled={isSubmitting} // TODO - handle |
75 |
| - onClick={handleSubmit} |
76 |
| - to={undefined} |
77 |
| - > |
78 |
| - Save |
79 |
| - </Button> |
80 |
| - <Button |
81 |
| - type="button" |
82 |
| - hook="cancel-address-update" |
83 |
| - variant="plain" |
84 |
| - // disabled={isLoading} |
85 |
| - onClick={() => console.log('TODO - implement me')} // TODO - implement me |
86 |
| - to={undefined} |
87 |
| - > |
88 |
| - Cancel |
89 |
| - </Button> |
90 |
| - </div> |
91 |
| - |
92 |
| - {errorMessage && <div className="text-red-500">{errorMessage}</div>} |
93 |
| - </div> |
94 |
| - ) |
95 |
| -} |
96 |
| - |
97 |
| -const PaymentPage: React.FC<{ clientSecret: string }> = ({ clientSecret }) => { |
98 |
| - const options = { |
99 |
| - clientSecret, |
100 |
| - appearance: { |
101 |
| - theme: 'stripe' as const, |
102 |
| - }, |
103 |
| - } |
| 5 | +import EditPaymentMethodForm from './EditPaymentMethodForm' |
104 | 6 |
|
105 |
| - return ( |
106 |
| - <Elements stripe={stripePromise} options={options}> |
107 |
| - <PaymentForm clientSecret={clientSecret} /> |
108 |
| - </Elements> |
109 |
| - ) |
| 7 | +interface EditPaymentMethodProps { |
| 8 | + isEditMode: boolean |
| 9 | + setEditMode: (isEditMode: boolean) => void |
| 10 | + provider: string |
| 11 | + owner: string |
110 | 12 | }
|
111 | 13 |
|
112 |
| -interface EditablePaymentMethodProps { |
113 |
| - clientSecret: string |
114 |
| -} |
115 |
| - |
116 |
| -const EditPaymentMethod: React.FC<EditablePaymentMethodProps> = () => { |
117 |
| - const clientSecret = MANUALLY_FETCHED_CLIENT_SECRET // TODO - fetch from API |
118 |
| - |
| 14 | +const EditPaymentMethod = ({ |
| 15 | + isEditMode, |
| 16 | + setEditMode, |
| 17 | + provider, |
| 18 | + owner, |
| 19 | +}: EditPaymentMethodProps) => { |
119 | 20 | const [activeTab, setActiveTab] = useState<'primary' | 'secondary'>('primary')
|
120 | 21 |
|
121 | 22 | return (
|
@@ -143,13 +44,23 @@ const EditPaymentMethod: React.FC<EditablePaymentMethodProps> = () => {
|
143 | 44 | <div className="m-4">
|
144 | 45 | {activeTab === 'primary' && (
|
145 | 46 | <div>
|
146 |
| - <PaymentPage clientSecret={clientSecret} /> |
| 47 | + <EditPaymentMethodForm |
| 48 | + isEditMode={isEditMode} |
| 49 | + setEditMode={setEditMode} |
| 50 | + provider={provider} |
| 51 | + owner={owner} |
| 52 | + /> |
147 | 53 | <AddressForm closeForm={() => {}} provider={''} owner={''} />
|
148 | 54 | </div>
|
149 | 55 | )}
|
150 | 56 | {activeTab === 'secondary' && (
|
151 | 57 | <div>
|
152 |
| - <PaymentPage clientSecret={clientSecret} /> |
| 58 | + <EditPaymentMethodForm |
| 59 | + isEditMode={isEditMode} |
| 60 | + setEditMode={setEditMode} |
| 61 | + provider={provider} |
| 62 | + owner={owner} |
| 63 | + /> |
153 | 64 | <AddressForm closeForm={() => {}} provider={''} owner={''} />
|
154 | 65 | </div>
|
155 | 66 | )}
|
|
0 commit comments