refactor(client): make ShowCertification functional (#39735)

This commit is contained in:
Shaun Hamilton
2020-10-23 11:15:56 +01:00
committed by GitHub
parent 0ae1df30c7
commit de6e85853f

View File

@ -1,5 +1,5 @@
/* eslint-disable react/jsx-sort-props */ /* eslint-disable react/jsx-sort-props */
import React, { Component } from 'react'; import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -83,37 +83,27 @@ const mapStateToProps = (state, { certName }) => {
const mapDispatchToProps = dispatch => const mapDispatchToProps = dispatch =>
bindActionCreators({ createFlashMessage, showCert, executeGA }, dispatch); bindActionCreators({ createFlashMessage, showCert, executeGA }, dispatch);
class ShowCertification extends Component { const ShowCertification = props => {
constructor(...args) { const [isDonationSubmitted, setIsDonationSubmitted] = useState(false);
super(...args); const [isDonationDisplayed, setIsDonationDisplayed] = useState(false);
const [isDonationClosed, setIsDonationClosed] = useState(false);
this.state = { useEffect(() => {
isDonationSubmitted: false, const { username, certName, validCertName, showCert } = props;
isDonationDisplayed: false,
isDonationClosed: false
};
this.hideDonationSection = this.hideDonationSection.bind(this);
this.handleProcessing = this.handleProcessing.bind(this);
}
componentDidMount() {
const { username, certName, validCertName, showCert } = this.props;
if (validCertName) { if (validCertName) {
return showCert({ username, certName }); showCert({ username, certName });
}
return null;
} }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
shouldComponentUpdate(nextProps) { useEffect(() => {
const { const {
userFetchState: { complete: userComplete }, userFetchState: { complete: userComplete },
signedInUserName, signedInUserName,
isDonating, isDonating,
cert: { username = '' }, cert: { username = '' },
executeGA executeGA
} = nextProps; } = props;
const { isDonationDisplayed } = this.state;
if ( if (
!isDonationDisplayed && !isDonationDisplayed &&
@ -122,9 +112,7 @@ class ShowCertification extends Component {
signedInUserName === username && signedInUserName === username &&
!isDonating !isDonating
) { ) {
this.setState({ setIsDonationDisplayed(true);
isDonationDisplayed: true
});
executeGA({ executeGA({
type: 'event', type: 'event',
@ -135,15 +123,19 @@ class ShowCertification extends Component {
} }
}); });
} }
return true; }, [isDonationDisplayed, props]);
}
hideDonationSection() { const hideDonationSection = () => {
this.setState({ isDonationDisplayed: false, isDonationClosed: true }); setIsDonationDisplayed(false);
} setIsDonationClosed(true);
};
handleProcessing(duration, amount, action = 'stripe form submission') { const handleProcessing = (
this.props.executeGA({ duration,
amount,
action = 'stripe form submission'
) => {
props.executeGA({
type: 'event', type: 'event',
data: { data: {
category: 'donation', category: 'donation',
@ -152,10 +144,9 @@ class ShowCertification extends Component {
value: amount value: amount
} }
}); });
this.setState({ isDonationSubmitted: true }); setIsDonationSubmitted(true);
} };
render() {
const { const {
cert, cert,
fetchState, fetchState,
@ -163,13 +154,7 @@ class ShowCertification extends Component {
createFlashMessage, createFlashMessage,
signedInUserName, signedInUserName,
location: { pathname } location: { pathname }
} = this.props; } = props;
const {
isDonationSubmitted,
isDonationDisplayed,
isDonationClosed
} = this.state;
if (!validCertName) { if (!validCertName) {
createFlashMessage(standardErrorMessage); createFlashMessage(standardErrorMessage);
@ -211,7 +196,7 @@ class ShowCertification extends Component {
block={true} block={true}
bsSize='sm' bsSize='sm'
bsStyle='primary' bsStyle='primary'
onClick={this.hideDonationSection} onClick={hideDonationSection}
> >
Close Close
</Button> </Button>
@ -225,16 +210,16 @@ class ShowCertification extends Component {
<Col lg={8} lgOffset={2} sm={10} smOffset={1} xs={12}> <Col lg={8} lgOffset={2} sm={10} smOffset={1} xs={12}>
<p> <p>
Only you can see this message. Congratulations on earning this Only you can see this message. Congratulations on earning this
certification. Its no easy task. Running freeCodeCamp isnt certification. Its no easy task. Running freeCodeCamp isnt easy
easy either. Nor is it cheap. Help us help you and many other either. Nor is it cheap. Help us help you and many other people
people around the world. Make a tax-deductible supporting around the world. Make a tax-deductible supporting donation to our
donation to our nonprofit today. nonprofit today.
</p> </p>
</Col> </Col>
</Row> </Row>
)} )}
<DonateForm <DonateForm
handleProcessing={this.handleProcessing} handleProcessing={handleProcessing}
defaultTheme='light' defaultTheme='light'
isMinimalForm={true} isMinimalForm={true}
/> />
@ -329,8 +314,7 @@ class ShowCertification extends Component {
{signedInUserName === username ? shareCertBtns : ''} {signedInUserName === username ? shareCertBtns : ''}
</div> </div>
); );
} };
}
ShowCertification.displayName = 'ShowCertification'; ShowCertification.displayName = 'ShowCertification';
ShowCertification.propTypes = propTypes; ShowCertification.propTypes = propTypes;