|
1 | | -import React from 'react'; |
2 | | -import { inject, observer } from 'app/componentConnectors'; |
| 1 | +import React, { FunctionComponent, useEffect, useCallback } from 'react'; |
| 2 | +import { useOvermind } from 'app/overmind'; |
3 | 3 | import { SubscribeForm } from 'app/components/SubscribeForm'; |
4 | 4 | import { Card } from './Card'; |
5 | 5 | import { Title, Subheading } from '../elements'; |
6 | 6 | import { Container } from './elements'; |
7 | 7 |
|
8 | | -interface Props { |
9 | | - store: any; |
10 | | - signals: any; |
11 | | -} |
12 | | - |
13 | | -class PaymentInfoComponent extends React.Component<Props> { |
14 | | - componentDidMount() { |
15 | | - this.props.signals.preferences.paymentDetailsRequested(); |
16 | | - } |
17 | | - |
18 | | - updatePaymentDetails = ({ token }) => { |
19 | | - this.props.signals.preferences.paymentDetailsUpdated({ token }); |
20 | | - }; |
21 | | - |
22 | | - paymentDetails = () => { |
23 | | - const { preferences } = this.props.store; |
24 | | - |
25 | | - if (preferences.paymentDetailError) |
26 | | - return <div>An error occurred: {preferences.paymentDetailError}</div>; |
| 8 | +export const PaymentInfo: FunctionComponent = () => { |
| 9 | + const { |
| 10 | + state: { |
| 11 | + preferences: { |
| 12 | + paymentDetailError, |
| 13 | + paymentDetails: { last4, name, brand }, |
| 14 | + isLoadingPaymentDetails, |
| 15 | + }, |
| 16 | + }, |
| 17 | + actions: { |
| 18 | + preferences: { paymentDetailsRequested, paymentDetailsUpdated }, |
| 19 | + }, |
| 20 | + } = useOvermind(); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + paymentDetailsRequested(); |
| 24 | + }, [paymentDetailsRequested]); |
| 25 | + |
| 26 | + const updatePaymentDetails = useCallback( |
| 27 | + ({ token }) => { |
| 28 | + paymentDetailsUpdated({ token }); |
| 29 | + }, |
| 30 | + [paymentDetailsUpdated] |
| 31 | + ); |
| 32 | + |
| 33 | + const paymentDetails = useCallback(() => { |
| 34 | + if (paymentDetailError) |
| 35 | + return <div>An error occurred: {paymentDetailError}</div>; |
27 | 36 |
|
28 | 37 | return ( |
29 | 38 | <div> |
30 | 39 | <Subheading>Current card</Subheading> |
31 | | - <Card |
32 | | - last4={preferences.paymentDetails.last4} |
33 | | - name={preferences.paymentDetails.name} |
34 | | - brand={preferences.paymentDetails.brand} |
35 | | - /> |
| 40 | + <Card last4={last4} name={name} brand={brand} /> |
36 | 41 |
|
37 | 42 | <Subheading style={{ marginTop: '2rem' }}>Update card info</Subheading> |
38 | 43 | <SubscribeForm |
39 | 44 | buttonName="Update" |
40 | 45 | loadingText="Updating Card Info..." |
41 | | - name={preferences.paymentDetails.name} |
42 | | - subscribe={this.updatePaymentDetails} |
| 46 | + name={name} |
| 47 | + subscribe={updatePaymentDetails} |
43 | 48 | /> |
44 | 49 | </div> |
45 | 50 | ); |
46 | | - }; |
47 | | - |
48 | | - render() { |
49 | | - const { preferences } = this.props.store; |
50 | | - return ( |
51 | | - <Container> |
52 | | - <Title>Payment Info</Title> |
53 | | - {preferences.isLoadingPaymentDetails ? ( |
54 | | - <div>Loading payment details...</div> |
55 | | - ) : ( |
56 | | - this.paymentDetails() |
57 | | - )} |
58 | | - </Container> |
59 | | - ); |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -export const PaymentInfo = inject('store', 'signals')( |
64 | | - observer(PaymentInfoComponent) |
65 | | -); |
| 51 | + }, [paymentDetailError, last4, name, brand, updatePaymentDetails]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <Container> |
| 55 | + <Title>Payment Info</Title> |
| 56 | + {isLoadingPaymentDetails ? ( |
| 57 | + <div>Loading payment details...</div> |
| 58 | + ) : ( |
| 59 | + paymentDetails() |
| 60 | + )} |
| 61 | + </Container> |
| 62 | + ); |
| 63 | +}; |
0 commit comments