Skip to content

Commit fa5eebf

Browse files
author
Saagar Takhi
committed
Refactors PaymentInfo/index.tsx: replaces Cerebral with Overmind & converts class to FC with hooks
1 parent 8a2bad7 commit fa5eebf

File tree

1 file changed

+46
-48
lines changed
  • packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo

1 file changed

+46
-48
lines changed
Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
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';
33
import { SubscribeForm } from 'app/components/SubscribeForm';
44
import { Card } from './Card';
55
import { Title, Subheading } from '../elements';
66
import { Container } from './elements';
77

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>;
2736

2837
return (
2938
<div>
3039
<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} />
3641

3742
<Subheading style={{ marginTop: '2rem' }}>Update card info</Subheading>
3843
<SubscribeForm
3944
buttonName="Update"
4045
loadingText="Updating Card Info..."
41-
name={preferences.paymentDetails.name}
42-
subscribe={this.updatePaymentDetails}
46+
name={name}
47+
subscribe={updatePaymentDetails}
4348
/>
4449
</div>
4550
);
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

Comments
 (0)