From de6e85853fe036f14dd2c9245e0c9c974e8b95fe Mon Sep 17 00:00:00 2001 From: Shaun Hamilton <51722130+Sky020@users.noreply.github.com> Date: Fri, 23 Oct 2020 11:15:56 +0100 Subject: [PATCH] refactor(client): make ShowCertification functional (#39735) --- .../client-only-routes/ShowCertification.js | 372 +++++++++--------- 1 file changed, 178 insertions(+), 194 deletions(-) diff --git a/client/src/client-only-routes/ShowCertification.js b/client/src/client-only-routes/ShowCertification.js index c3df1079bd..1d471d8f5a 100644 --- a/client/src/client-only-routes/ShowCertification.js +++ b/client/src/client-only-routes/ShowCertification.js @@ -1,5 +1,5 @@ /* eslint-disable react/jsx-sort-props */ -import React, { Component } from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; @@ -83,37 +83,27 @@ const mapStateToProps = (state, { certName }) => { const mapDispatchToProps = dispatch => bindActionCreators({ createFlashMessage, showCert, executeGA }, dispatch); -class ShowCertification extends Component { - constructor(...args) { - super(...args); +const ShowCertification = props => { + const [isDonationSubmitted, setIsDonationSubmitted] = useState(false); + const [isDonationDisplayed, setIsDonationDisplayed] = useState(false); + const [isDonationClosed, setIsDonationClosed] = useState(false); - this.state = { - isDonationSubmitted: false, - isDonationDisplayed: false, - isDonationClosed: false - }; - - this.hideDonationSection = this.hideDonationSection.bind(this); - this.handleProcessing = this.handleProcessing.bind(this); - } - - componentDidMount() { - const { username, certName, validCertName, showCert } = this.props; + useEffect(() => { + const { username, certName, validCertName, showCert } = props; if (validCertName) { - return showCert({ username, certName }); + showCert({ username, certName }); } - return null; - } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); - shouldComponentUpdate(nextProps) { + useEffect(() => { const { userFetchState: { complete: userComplete }, signedInUserName, isDonating, cert: { username = '' }, executeGA - } = nextProps; - const { isDonationDisplayed } = this.state; + } = props; if ( !isDonationDisplayed && @@ -122,9 +112,7 @@ class ShowCertification extends Component { signedInUserName === username && !isDonating ) { - this.setState({ - isDonationDisplayed: true - }); + setIsDonationDisplayed(true); executeGA({ type: 'event', @@ -135,15 +123,19 @@ class ShowCertification extends Component { } }); } - return true; - } + }, [isDonationDisplayed, props]); - hideDonationSection() { - this.setState({ isDonationDisplayed: false, isDonationClosed: true }); - } + const hideDonationSection = () => { + setIsDonationDisplayed(false); + setIsDonationClosed(true); + }; - handleProcessing(duration, amount, action = 'stripe form submission') { - this.props.executeGA({ + const handleProcessing = ( + duration, + amount, + action = 'stripe form submission' + ) => { + props.executeGA({ type: 'event', data: { category: 'donation', @@ -152,185 +144,177 @@ class ShowCertification extends Component { value: amount } }); - this.setState({ isDonationSubmitted: true }); + setIsDonationSubmitted(true); + }; + + const { + cert, + fetchState, + validCertName, + createFlashMessage, + signedInUserName, + location: { pathname } + } = props; + + if (!validCertName) { + createFlashMessage(standardErrorMessage); + return ; } - render() { - const { - cert, - fetchState, - validCertName, - createFlashMessage, - signedInUserName, - location: { pathname } - } = this.props; + const { pending, complete, errored } = fetchState; - const { - isDonationSubmitted, - isDonationDisplayed, - isDonationClosed - } = this.state; + if (pending) { + return ; + } - if (!validCertName) { - createFlashMessage(standardErrorMessage); - return ; - } + if (!pending && errored) { + createFlashMessage(standardErrorMessage); + return ; + } - const { pending, complete, errored } = fetchState; + if (!pending && !complete && !errored) { + createFlashMessage(reallyWeirdErrorMessage); + return ; + } - if (pending) { - return ; - } + const { + date, + name: userFullName, + username, + certTitle, + completionTime + } = cert; - if (!pending && errored) { - createFlashMessage(standardErrorMessage); - return ; - } + const certDate = new Date(date); + const certYear = certDate.getFullYear(); + const certMonth = certDate.getMonth(); + const certURL = `https://freecodecamp.org${pathname}`; - if (!pending && !complete && !errored) { - createFlashMessage(reallyWeirdErrorMessage); - return ; - } + const donationCloseBtn = ( +
+ +
+ ); - const { - date, - name: userFullName, - username, - certTitle, - completionTime - } = cert; - - const certDate = new Date(date); - const certYear = certDate.getFullYear(); - const certMonth = certDate.getMonth(); - const certURL = `https://freecodecamp.org${pathname}`; - - const donationCloseBtn = ( -
- -
- ); - - let donationSection = ( - - {!isDonationSubmitted && ( - - -

- Only you can see this message. Congratulations on earning this - certification. It’s no easy task. Running freeCodeCamp isn’t - easy either. Nor is it cheap. Help us help you and many other - people around the world. Make a tax-deductible supporting - donation to our nonprofit today. -

- -
- )} - + let donationSection = ( + + {!isDonationSubmitted && ( - - {isDonationSubmitted && donationCloseBtn} + +

+ Only you can see this message. Congratulations on earning this + certification. It’s no easy task. Running freeCodeCamp isn’t easy + either. Nor is it cheap. Help us help you and many other people + around the world. Make a tax-deductible supporting donation to our + nonprofit today. +

-
- ); - - const shareCertBtns = ( - - - - - + )} + + + + {isDonationSubmitted && donationCloseBtn} + - ); +
+ ); - return ( -
- {isDonationDisplayed && !isDonationClosed ? donationSection : ''} - - -
- -
- -
- - -
- Issued  - {format(certDate, 'MMMM d, y')} -
- -
+ const shareCertBtns = ( + + + + + + + ); -
-
-

This certifies that

-

- {userFullName} -

-

has successfully completed the freeCodeCamp.org

-

- {certTitle} -

-

- Developer Certification, representing approximately{' '} - {completionTime} hours of coursework -

+ return ( +
+ {isDonationDisplayed && !isDonationClosed ? donationSection : ''} + + +
+ +
+
-
-
-
- Quincy Larson's Signature -

- Quincy Larson -

-

Executive Director, freeCodeCamp.org

+ + +
+ Issued  + {format(certDate, 'MMMM d, y')}
- -

Verify this certification at {certURL}

-
-
-
-
- {signedInUserName === username ? shareCertBtns : ''} -
- ); - } -} + + + +
+
+

This certifies that

+

+ {userFullName} +

+

has successfully completed the freeCodeCamp.org

+

+ {certTitle} +

+

+ Developer Certification, representing approximately{' '} + {completionTime} hours of coursework +

+
+
+ + + + {signedInUserName === username ? shareCertBtns : ''} + + ); +}; ShowCertification.displayName = 'ShowCertification'; ShowCertification.propTypes = propTypes;