From 606ca74deb886f584cc11d1292c3193757f409a3 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Mon, 2 Oct 2023 13:50:15 +0300 Subject: [PATCH] feat: turn donation modal element into react components (#51670) Co-authored-by: Oliver Eyton-Williams --- .../components/Donation/donation-modal.tsx | 421 +++++++++++++----- 1 file changed, 303 insertions(+), 118 deletions(-) diff --git a/client/src/components/Donation/donation-modal.tsx b/client/src/components/Donation/donation-modal.tsx index c3144727fc5..56ef429314a 100644 --- a/client/src/components/Donation/donation-modal.tsx +++ b/client/src/components/Donation/donation-modal.tsx @@ -12,7 +12,8 @@ import { PaymentContext, subscriptionAmounts, defaultDonation, - defaultTierAmount + defaultTierAmount, + type DonationAmount } from '../../../../shared/config/donation-settings'; import BearProgressModal from '../../assets/images/components/bear-progress-modal'; import BearBlockCompletion from '../../assets/images/components/bear-block-completion-modal'; @@ -56,7 +57,7 @@ type DonateModalProps = { show: boolean; }; -const RenderIlustration = ({ +const Ilustration = ({ recentlyClaimedBlock }: { recentlyClaimedBlock: RecentlyClaimedBlock; @@ -74,78 +75,20 @@ function getctaNumberBetween1To10() { return Math.floor(Math.random() * (max - min + 1)) + min; } -function DonateModal({ - show, - closeDonationModal, - executeGA, - location, +function ModalHeader({ + closeLabel, + ctaNumber, + showMultiTier, recentlyClaimedBlock -}: DonateModalProps): JSX.Element { - const [closeLabel, setCloseLabel] = useState(false); - const [ctaNumber, setCtaNumber] = useState(0); - const [isDisabled, setIsDisabled] = useState(true); - const [showSkipButton, setShowSkipButton] = useState(false); - const [showDonateForm, setShowDonateForm] = useState(true); - const [donationAmount, setDonationAmount] = useState( - defaultDonation.donationAmount - ); - const loadElementsIndividually = useFeature('load_elements_individually').on; - const showMultiTier = useFeature('multi-tier').on; +}: { + closeLabel: boolean; + ctaNumber: number; + showMultiTier: boolean; + recentlyClaimedBlock: RecentlyClaimedBlock; +}) { const { t } = useTranslation(); - - // test wheather the conversions are being distributed properly - useFeature('aa-test-in-component'); - - const handleProcessing = () => { - setCloseLabel(true); - }; - - useEffect(() => { - if (loadElementsIndividually) { - const timer = setTimeout(() => { - setIsDisabled(false); - setShowSkipButton(true); - }, 4000); - return () => clearTimeout(timer); - } else { - setIsDisabled(false); - setShowSkipButton(true); - } - }, [loadElementsIndividually]); - - useEffect(() => { - if (show) { - void playTone('donation'); - executeGA({ event: 'pageview', pagePath: '/donation-modal' }); - executeGA({ - event: 'donation_view', - action: `Displayed ${ - recentlyClaimedBlock !== null ? 'Block' : 'Progress' - } Donation Modal` - }); - } - }, [show, recentlyClaimedBlock, executeGA]); - - useEffect(() => { - if (show) setCtaNumber(getctaNumberBetween1To10()); - }, [show]); - - useEffect(() => { - if (showMultiTier) { - setShowDonateForm(false); - setDonationAmount(defaultTierAmount); - } - }, [showMultiTier]); - - const handleModalHide = () => { - // If modal is open on a SuperBlock page - if (isLocationSuperBlock(location)) { - goToAnchor('claim-cert-block'); - } - }; - - const modalHeader = ( -
+ return ( +
{!closeLabel && ( @@ -169,37 +112,27 @@ function DonateModal({
); +} - const closeButtonRow = ( - <> - - - - - - - ); - - const selectionTabs = ( +function SelectionTabs({ + loadElementsIndividually, + donationAmount, + setDonationAmount, + setShowDonateForm +}: { + loadElementsIndividually: boolean; + donationAmount: DonationAmount; + setDonationAmount: React.Dispatch>; + setShowDonateForm: React.Dispatch>; +}) { + const { t } = useTranslation(); + return ( {t('donate.confirm-monthly', { @@ -255,12 +188,114 @@ function DonateModal({ ); +} - const donationFormRow = ( +function CloseButtonRow({ + showSkipButton, + isDisabled, + closeLabel, + closeDonationModal +}: { + showSkipButton: boolean; + isDisabled: boolean; + closeLabel: boolean; + closeDonationModal: () => void; +}) { + const { t } = useTranslation(); + return ( + + + + + + ); +} + +function DonationBody({ + closeLabel, + ctaNumber, + showMultiTier, + recentlyClaimedBlock, + donationAmount, + loadElementsIndividually, + handleProcessing, + setShowDonateForm, + closeDonationModal, + isDisabled, + showSkipButton +}: { + closeLabel: boolean; + ctaNumber: number; + showMultiTier: boolean; + recentlyClaimedBlock: RecentlyClaimedBlock; + donationAmount: DonationAmount; + loadElementsIndividually: boolean; + handleProcessing: () => void; + setShowDonateForm: React.Dispatch>; + closeDonationModal: () => void; + isDisabled: boolean; + showSkipButton: boolean; +}) { + return ( + <> + + + + + ); +} + +function DonationFormRow({ + handleProcessing, + loadElementsIndividually, + showMultiTier, + setShowDonateForm, + donationAmount +}: { + handleProcessing: () => void; + loadElementsIndividually: boolean; + showMultiTier: boolean; + setShowDonateForm: React.Dispatch>; + donationAmount: DonationAmount; +}) { + return ( ); +} - const multiTierModalBody = ( +function MultiTierDonationBody({ + closeLabel, + ctaNumber, + showMultiTier, + recentlyClaimedBlock, + donationAmount, + loadElementsIndividually, + setDonationAmount, + setShowDonateForm, + closeDonationModal, + isDisabled, + showSkipButton, + showDonateForm, + handleProcessing +}: { + closeLabel: boolean; + ctaNumber: number; + showMultiTier: boolean; + recentlyClaimedBlock: RecentlyClaimedBlock; + donationAmount: DonationAmount; + loadElementsIndividually: boolean; + setDonationAmount: React.Dispatch>; + setShowDonateForm: React.Dispatch>; + closeDonationModal: () => void; + isDisabled: boolean; + showSkipButton: boolean; + showDonateForm: boolean; + handleProcessing: () => void; +}) { + return ( <> -
- {modalHeader} - {selectionTabs} - {closeButtonRow} +
+ + +
-
- {donationFormRow} - {closeLabel && closeButtonRow} +
+ + {closeLabel && ( + + )}
); +} - const defaultModalBody = ( - <> - {modalHeader} - {donationFormRow} - {closeButtonRow} - +function DonateModal({ + show, + closeDonationModal, + executeGA, + location, + recentlyClaimedBlock +}: DonateModalProps): JSX.Element { + const [closeLabel, setCloseLabel] = useState(false); + const [ctaNumber, setCtaNumber] = useState(0); + const [isDisabled, setIsDisabled] = useState(true); + const [showSkipButton, setShowSkipButton] = useState(false); + const [showDonateForm, setShowDonateForm] = useState(true); + const [donationAmount, setDonationAmount] = useState( + defaultDonation.donationAmount ); + const loadElementsIndividually = useFeature('load_elements_individually').on; + const showMultiTier = useFeature('multi-tier').on; + + // test wheather the conversions are being distributed properly + useFeature('aa-test-in-component'); + + const handleProcessing = () => { + setCloseLabel(true); + }; + + useEffect(() => { + if (loadElementsIndividually) { + const timer = setTimeout(() => { + setIsDisabled(false); + setShowSkipButton(true); + }, 4000); + return () => clearTimeout(timer); + } else { + setIsDisabled(false); + setShowSkipButton(true); + } + }, [loadElementsIndividually]); + + useEffect(() => { + if (show) { + void playTone('donation'); + executeGA({ event: 'pageview', pagePath: '/donation-modal' }); + executeGA({ + event: 'donation_view', + action: `Displayed ${ + recentlyClaimedBlock !== null ? 'Block' : 'Progress' + } Donation Modal` + }); + } + }, [show, recentlyClaimedBlock, executeGA]); + + useEffect(() => { + if (show) setCtaNumber(getctaNumberBetween1To10()); + }, [show]); + + useEffect(() => { + if (showMultiTier) { + setShowDonateForm(false); + setDonationAmount(defaultTierAmount); + } + }, [showMultiTier]); + + const handleModalHide = () => { + // If modal is open on a SuperBlock page + if (isLocationSuperBlock(location)) { + goToAnchor('claim-cert-block'); + } + }; return ( - +
- +
- {showMultiTier ? multiTierModalBody : defaultModalBody} + {showMultiTier ? ( + + ) : ( + + )}
);