Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit ef84e80

Browse files
Make card icon solid.
1 parent e35387d commit ef84e80

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

components/ElementsForm.tsx

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import React, { useState } from 'react'
1+
import React, { useState } from 'react';
22

3-
import CustomDonationInput from '../components/CustomDonationInput'
4-
import PrintObject from '../components/PrintObject'
3+
import CustomDonationInput from '../components/CustomDonationInput';
4+
import PrintObject from '../components/PrintObject';
55

6-
import { fetchPostJSON } from '../utils/api-helpers'
7-
import { formatAmountForDisplay } from '../utils/stripe-helpers'
8-
import * as config from '../config'
6+
import { fetchPostJSON } from '../utils/api-helpers';
7+
import { formatAmountForDisplay } from '../utils/stripe-helpers';
8+
import * as config from '../config';
99

10-
import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js'
10+
import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js';
1111

1212
const CARD_OPTIONS = {
13+
iconStyle: 'solid' as const,
1314
style: {
1415
base: {
1516
iconColor: '#6772e5',
@@ -19,102 +20,102 @@ const CARD_OPTIONS = {
1920
fontSize: '16px',
2021
fontSmoothing: 'antialiased',
2122
':-webkit-autofill': {
22-
color: '#fce883',
23+
color: '#fce883'
2324
},
2425
'::placeholder': {
25-
color: '#6772e5',
26-
},
26+
color: '#6772e5'
27+
}
2728
},
2829
invalid: {
2930
iconColor: '#ef2961',
30-
color: '#ef2961',
31-
},
32-
},
33-
}
31+
color: '#ef2961'
32+
}
33+
}
34+
};
3435

3536
const ElementsForm: React.FunctionComponent = () => {
3637
const [input, setInput] = useState({
3738
customDonation: Math.round(config.MAX_AMOUNT / config.AMOUNT_STEP),
38-
cardholderName: '',
39-
})
40-
const [payment, setPayment] = useState({ status: 'initial' })
41-
const [errorMessage, setErrorMessage] = useState('')
42-
const stripe = useStripe()
43-
const elements = useElements()
39+
cardholderName: ''
40+
});
41+
const [payment, setPayment] = useState({ status: 'initial' });
42+
const [errorMessage, setErrorMessage] = useState('');
43+
const stripe = useStripe();
44+
const elements = useElements();
4445

4546
const PaymentStatus = ({ status }: { status: string }) => {
4647
switch (status) {
4748
case 'processing':
4849
case 'requires_payment_method':
4950
case 'requires_confirmation':
50-
return <h2>Processing...</h2>
51+
return <h2>Processing...</h2>;
5152

5253
case 'requires_action':
53-
return <h2>Authenticating...</h2>
54+
return <h2>Authenticating...</h2>;
5455

5556
case 'succeeded':
56-
return <h2>Payment Succeeded 🥳</h2>
57+
return <h2>Payment Succeeded 🥳</h2>;
5758

5859
case 'error':
5960
return (
6061
<>
6162
<h2>Error 😭</h2>
6263
<p className="error-message">{errorMessage}</p>
6364
</>
64-
)
65+
);
6566

6667
default:
67-
return null
68+
return null;
6869
}
69-
}
70+
};
7071

7172
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = e =>
7273
setInput({
7374
...input,
74-
[e.currentTarget.name]: e.currentTarget.value,
75-
})
75+
[e.currentTarget.name]: e.currentTarget.value
76+
});
7677

7778
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async e => {
78-
e.preventDefault()
79+
e.preventDefault();
7980
// Abort if form isn't valid
80-
if (!e.currentTarget.reportValidity()) return
81-
setPayment({ status: 'processing' })
81+
if (!e.currentTarget.reportValidity()) return;
82+
setPayment({ status: 'processing' });
8283

8384
// Create a PaymentIntent with the specified amount.
8485
const response = await fetchPostJSON('/api/payment_intents', {
85-
amount: input.customDonation,
86-
})
87-
setPayment(response)
86+
amount: input.customDonation
87+
});
88+
setPayment(response);
8889

8990
if (response.statusCode === 500) {
90-
setPayment({ status: 'error' })
91-
setErrorMessage(response.message)
92-
return
91+
setPayment({ status: 'error' });
92+
setErrorMessage(response.message);
93+
return;
9394
}
9495

9596
// Get a reference to a mounted CardElement. Elements knows how
9697
// to find your CardElement because there can only ever be one of
9798
// each type of element.
98-
const cardElement = elements!.getElement(CardElement)
99+
const cardElement = elements!.getElement(CardElement);
99100

100101
// Use your card Element with other Stripe.js APIs
101102
const { error, paymentIntent } = await stripe!.confirmCardPayment(
102103
response.client_secret,
103104
{
104105
payment_method: {
105106
card: cardElement!,
106-
billing_details: { name: input.cardholderName },
107-
},
107+
billing_details: { name: input.cardholderName }
108+
}
108109
}
109-
)
110+
);
110111

111112
if (error) {
112-
setPayment({ status: 'error' })
113-
setErrorMessage(error.message ?? 'An unknown error occured')
113+
setPayment({ status: 'error' });
114+
setErrorMessage(error.message ?? 'An unknown error occured');
114115
} else if (paymentIntent) {
115-
setPayment(paymentIntent)
116+
setPayment(paymentIntent);
116117
}
117-
}
118+
};
118119

119120
return (
120121
<>
@@ -144,8 +145,10 @@ const ElementsForm: React.FunctionComponent = () => {
144145
options={CARD_OPTIONS}
145146
onChange={e => {
146147
if (e.error) {
147-
setPayment({ status: 'error' })
148-
setErrorMessage(e.error.message ?? 'An unknown error occured')
148+
setPayment({ status: 'error' });
149+
setErrorMessage(
150+
e.error.message ?? 'An unknown error occured'
151+
);
149152
}
150153
}}
151154
/>
@@ -165,7 +168,7 @@ const ElementsForm: React.FunctionComponent = () => {
165168
<PaymentStatus status={payment.status} />
166169
<PrintObject content={payment} />
167170
</>
168-
)
169-
}
171+
);
172+
};
170173

171-
export default ElementsForm
174+
export default ElementsForm;

0 commit comments

Comments
 (0)