♻️ Greatly improve Navigation component

This commit is contained in:
Philipp Stracker 2024-12-02 17:46:07 +01:00
parent 2b87837345
commit f4cfabacf9
No known key found for this signature in database
3 changed files with 66 additions and 98 deletions

View file

@ -4,108 +4,60 @@ import { __ } from '@wordpress/i18n';
import { OnboardingHooks } from '../../../../data';
const Navigation = ( {
setStep,
setCompleted,
currentStep,
stepperOrder,
title,
} ) => {
const isLastStep = () => currentStep + 1 === stepperOrder.length;
const isFistStep = () => currentStep === 0;
const navigateBy = ( stepDirection ) => {
let newStep = currentStep + stepDirection;
const Navigation = ( { stepDetails, onNext, onPrev, onExit } ) => {
const { title, isFirst, percentage, showNext, canProceed } = stepDetails;
if ( isNaN( newStep ) || newStep < 0 ) {
console.warn( 'Invalid next step:', newStep );
newStep = 0;
}
if ( newStep >= stepperOrder.length ) {
setCompleted( true );
} else {
setStep( newStep );
}
};
const { products } = OnboardingHooks.useProducts();
const { isCasualSeller } = OnboardingHooks.useBusiness();
let disabled = false;
switch ( currentStep ) {
case 1:
disabled = isCasualSeller === null;
break;
case 2:
disabled = products.length < 1;
break;
}
const state = OnboardingHooks.useNavigationState();
const isDisabled = ! canProceed( state );
return (
<div className="ppcp-r-navigation-container">
<div className="ppcp-r-navigation">
<div className="ppcp-r-navigation--left">
<Icon icon={ chevronLeft } />
{ ! isFistStep() ? (
<Button
variant="tertiary"
onClick={ () => navigateBy( -1 ) }
onClick={ isFirst ? onExit : onPrev }
>
{ title }
</Button>
) : (
<a
className="ppcp-r-navigation--left__link"
href={ global.ppcpSettings.wcPaymentsTabUrl }
aria-label={ __(
'Return to payments',
'woocommerce-paypal-payments'
) }
>
{ title }
</a>
) }
</div>
{ ! isFistStep() && (
<div className="ppcp-r-navigation--right">
<a
href={ global.ppcpSettings.wcPaymentsTabUrl }
aria-label={ __(
'Return to payments',
'woocommerce-paypal-payments'
) }
>
{ __(
'Save and exit',
'woocommerce-paypal-payments'
) }
</a>
{ ! isLastStep() && (
<Button
variant="primary"
disabled={ disabled }
onClick={ () => navigateBy( 1 ) }
>
{ __(
'Continue',
'woocommerce-paypal-payments'
) }
</Button>
) }
</div>
) }
<div
className="ppcp-r-navigation--progress-bar"
style={ {
width: `${
( currentStep / ( stepperOrder.length - 1 ) ) * 90
}%`,
} }
></div>
{ ! isFirst &&
NextButton( { showNext, isDisabled, onNext, onExit } ) }
<ProgressBar percent={ percentage } />
</div>
</div>
);
};
const NextButton = ( { showNext, isDisabled, onNext, onExit } ) => {
return (
<div className="ppcp-r-navigation--right">
<Button onClick={ onExit }>
{ __( 'Save and exit', 'woocommerce-paypal-payments' ) }
</Button>
{ showNext && (
<Button
variant="primary"
disabled={ isDisabled }
onClick={ onNext }
>
{ __( 'Continue', 'woocommerce-paypal-payments' ) }
</Button>
) }
</div>
);
};
const ProgressBar = ( { percent } ) => {
percent = Math.min( Math.max( percent, 0 ), 100 );
return (
<div
className="ppcp-r-navigation--progress-bar"
style={ { width: `${ percent * 0.9 }%` } }
/>
);
};
export default Navigation;

View file

@ -8,20 +8,26 @@ const Onboarding = () => {
const { step, setStep, setCompleted, flags } = OnboardingHooks.useSteps();
const Steps = getSteps( flags );
const { StepComponent, title } = getCurrentStep( step, Steps );
const currentStep = getCurrentStep( step, Steps );
const handleNext = () => setStep( currentStep.nextStep );
const handlePrev = () => setStep( currentStep.prevStep );
const handleExit = () => {
window.location.href = window.ppcpSettings.wcPaymentsTabUrl;
};
return (
<>
<Navigation
setStep={ setStep }
currentStep={ step }
setCompleted={ setCompleted }
stepperOrder={ Steps }
title={ title }
stepDetails={ currentStep }
onNext={ handleNext }
onPrev={ handlePrev }
onExit={ handleExit }
/>
<Container page="onboarding">
<div className="ppcp-r-card">
<StepComponent
<currentStep.StepComponent
setStep={ setStep }
currentStep={ step }
setCompleted={ setCompleted }

View file

@ -113,3 +113,13 @@ export const useSteps = () => {
return { flags, isReady, step, setStep, completed, setCompleted };
};
export const useNavigationState = () => {
const products = useProducts();
const business = useBusiness();
return {
products,
business,
};
};