From b2679bface6372dc16b7edb1d92f9c8cb855689f Mon Sep 17 00:00:00 2001 From: John Oleksowicz Date: Thu, 19 Oct 2023 14:41:48 -0500 Subject: [PATCH] Show correct number of steps This ensures we display the correct number of total steps when skipping the tracking step for dotcom users. In order to achieve this I decided to refactor the step count calculation and step mapping functions in a way that I hope will be easier to update going forward. MAILPOET-5549 --- .../assets/js/src/wizard/steps-numbers.ts | 36 ++++++++----------- .../src/wizard/welcome-wizard-controller.tsx | 22 ++++-------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/mailpoet/assets/js/src/wizard/steps-numbers.ts b/mailpoet/assets/js/src/wizard/steps-numbers.ts index 174549297f..e03e2a8dd1 100644 --- a/mailpoet/assets/js/src/wizard/steps-numbers.ts +++ b/mailpoet/assets/js/src/wizard/steps-numbers.ts @@ -1,14 +1,22 @@ -export const getStepsCount = (): number => { - let stepsCount = 3; +const getSteps = (): string[] => { + const steps = ['WelcomeWizardSenderStep']; + if (!window.mailpoet_is_dotcom) { + steps.push('WelcomeWizardUsageTrackingStep'); + } if (window.mailpoet_woocommerce_active) { - stepsCount += 1; + steps.push('WizardWooCommerceStep'); } - if (window.mailpoet_has_valid_api_key) { - stepsCount -= 1; // skip the MSS step if user already set API key + if (!window.mailpoet_has_valid_api_key) { + steps.push('WelcomeWizardPitchMSSStep'); } - return stepsCount; + return steps; }; +export const getStepsCount = (): number => getSteps().length; + +export const mapStepNumberToStepName = (stepNumber: number): string | null => + getSteps()[stepNumber - 1] || null; + export const redirectToNextStep = ( history: { push: (string) => void }, finishWizard: () => void, @@ -21,19 +29,3 @@ export const redirectToNextStep = ( finishWizard(); } }; - -export const mapStepNumberToStepName = (stepNumber: number): string | null => { - if (stepNumber === 1) { - return 'WelcomeWizardSenderStep'; - } - if (stepNumber === 2) { - return 'WelcomeWizardUsageTrackingStep'; - } - if (window.mailpoet_woocommerce_active && stepNumber === 3) { - return 'WizardWooCommerceStep'; - } - if (!window.mailpoet_has_valid_api_key) { - return 'WelcomeWizardPitchMSSStep'; - } - return null; -}; diff --git a/mailpoet/assets/js/src/wizard/welcome-wizard-controller.tsx b/mailpoet/assets/js/src/wizard/welcome-wizard-controller.tsx index dcb0f609ce..e107bc8409 100644 --- a/mailpoet/assets/js/src/wizard/welcome-wizard-controller.tsx +++ b/mailpoet/assets/js/src/wizard/welcome-wizard-controller.tsx @@ -86,16 +86,11 @@ function WelcomeWizardStepsController({ const submitSender = useCallback(async () => { setLoading(true); - let currentStep = step; - if (window.mailpoet_is_dotcom) { - // Skip the next step - currentStep += 1; - if (!window.wizard_has_tracking_settings) { - await updateTracking(true, true); - } + if (window.mailpoet_is_dotcom && !window.wizard_has_tracking_settings) { + await updateTracking(true, true); } await updateSettings(createSenderSettings(sender)).then(() => - redirect(currentStep), + redirect(step), ); setLoading(false); }, [redirect, sender, step, updateTracking]); @@ -105,18 +100,13 @@ function WelcomeWizardStepsController({ e.preventDefault(); setLoading(true); const defaultSenderInfo = { address: window.admin_email, name: '' }; - let currentStep = step; - if (window.mailpoet_is_dotcom) { - // Skip the next step - currentStep += 1; - if (!window.wizard_has_tracking_settings) { - await updateTracking(true, true); - } + if (window.mailpoet_is_dotcom && !window.wizard_has_tracking_settings) { + await updateTracking(true, true); } await updateSettings(createSenderSettings(defaultSenderInfo)).then(() => { setSender(defaultSenderInfo); - redirect(currentStep); + redirect(step); }); setLoading(false); },