From fa5eebf0ef41a1c3983a2a72e593a5b349484eac Mon Sep 17 00:00:00 2001 From: Saagar Takhi Date: Sun, 6 Oct 2019 03:20:17 +0530 Subject: [PATCH 1/2] Refactors PaymentInfo/index.tsx: replaces Cerebral with Overmind & converts class to FC with hooks --- .../PreferencesModal/PaymentInfo/index.tsx | 94 +++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx b/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx index 44a1c2313c8..7086724b333 100644 --- a/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx +++ b/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx @@ -1,65 +1,63 @@ -import React from 'react'; -import { inject, observer } from 'app/componentConnectors'; +import React, { FunctionComponent, useEffect, useCallback } from 'react'; +import { useOvermind } from 'app/overmind'; import { SubscribeForm } from 'app/components/SubscribeForm'; import { Card } from './Card'; import { Title, Subheading } from '../elements'; import { Container } from './elements'; -interface Props { - store: any; - signals: any; -} - -class PaymentInfoComponent extends React.Component { - componentDidMount() { - this.props.signals.preferences.paymentDetailsRequested(); - } - - updatePaymentDetails = ({ token }) => { - this.props.signals.preferences.paymentDetailsUpdated({ token }); - }; - - paymentDetails = () => { - const { preferences } = this.props.store; - - if (preferences.paymentDetailError) - return
An error occurred: {preferences.paymentDetailError}
; +export const PaymentInfo: FunctionComponent = () => { + const { + state: { + preferences: { + paymentDetailError, + paymentDetails: { last4, name, brand }, + isLoadingPaymentDetails, + }, + }, + actions: { + preferences: { paymentDetailsRequested, paymentDetailsUpdated }, + }, + } = useOvermind(); + + useEffect(() => { + paymentDetailsRequested(); + }, [paymentDetailsRequested]); + + const updatePaymentDetails = useCallback( + ({ token }) => { + paymentDetailsUpdated({ token }); + }, + [paymentDetailsUpdated] + ); + + const paymentDetails = useCallback(() => { + if (paymentDetailError) + return
An error occurred: {paymentDetailError}
; return (
Current card - + Update card info
); - }; - - render() { - const { preferences } = this.props.store; - return ( - - Payment Info - {preferences.isLoadingPaymentDetails ? ( -
Loading payment details...
- ) : ( - this.paymentDetails() - )} -
- ); - } -} - -export const PaymentInfo = inject('store', 'signals')( - observer(PaymentInfoComponent) -); + }, [paymentDetailError, last4, name, brand, updatePaymentDetails]); + + return ( + + Payment Info + {isLoadingPaymentDetails ? ( +
Loading payment details...
+ ) : ( + paymentDetails() + )} +
+ ); +}; From ffe6e12f9d108cb2f9925f60e890402be8a2e323 Mon Sep 17 00:00:00 2001 From: Saagar Takhi Date: Sun, 6 Oct 2019 04:16:09 +0530 Subject: [PATCH 2/2] Refactors PaymentInfo/index.tsx: removes dependency from useEffect to make it equivalent to componentDidMount --- .../pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx b/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx index 7086724b333..10a8c011f4a 100644 --- a/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx +++ b/packages/app/src/app/pages/common/Modals/PreferencesModal/PaymentInfo/index.tsx @@ -21,7 +21,7 @@ export const PaymentInfo: FunctionComponent = () => { useEffect(() => { paymentDetailsRequested(); - }, [paymentDetailsRequested]); + }, []); // eslint-disable-line react-hooks/exhaustive-deps const updatePaymentDetails = useCallback( ({ token }) => {