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
- 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. -
- -+ 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. +