| 1 |
import React, { useEffect, useState } from 'react'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import CfIcon from '@WizardImages/cartflows-icon.svg'; |
| 4 |
import { getExitSetupWizard } from '@Utils/Helpers'; |
| 5 |
import apiFetch from '@wordpress/api-fetch'; |
| 6 |
import { useLocation, useHistory } from 'react-router-dom'; |
| 7 |
import { Disclosure } from '@headlessui/react'; |
| 8 |
import { XMarkIcon } from '@heroicons/react/24/outline'; |
| 9 |
|
| 10 |
function Index() { |
| 11 |
const search = useLocation().search; |
| 12 |
const history = useHistory(); |
| 13 |
const stepParam = new URLSearchParams( search ).get( 'step' ); |
| 14 |
const step = stepParam || 'welcome'; |
| 15 |
|
| 16 |
const [ lastVisitedStep ] = useState( step ); |
| 17 |
const woocommerce_status = window.cartflows_wizard?.woocommerce_status; |
| 18 |
const [ completedSteps ] = useState( () => { |
| 19 |
return JSON.parse( localStorage.getItem( 'completedSteps' ) ) || []; |
| 20 |
} ); |
| 21 |
|
| 22 |
// Add useEffect to synchronize completedSteps with navigation |
| 23 |
useEffect( () => { |
| 24 |
localStorage.setItem( |
| 25 |
'completedSteps', |
| 26 |
JSON.stringify( completedSteps ) |
| 27 |
); |
| 28 |
}, [ completedSteps ] ); |
| 29 |
|
| 30 |
const menus = [ |
| 31 |
{ name: __( 'Welcome', 'cartflows' ), id: 'welcome' }, |
| 32 |
{ name: __( 'Page Builder', 'cartflows' ), id: 'page-builder' }, |
| 33 |
{ name: __( 'Required Plugins', 'cartflows' ), id: 'plugin-install' }, |
| 34 |
{ name: __( 'Store Checkout', 'cartflows' ), id: 'store-checkout' }, |
| 35 |
{ name: __( 'Subscribe', 'cartflows' ), id: 'optin' }, |
| 36 |
{ name: __( 'Done', 'cartflows' ), id: 'ready' }, |
| 37 |
]; |
| 38 |
|
| 39 |
const handleExit = ( e ) => { |
| 40 |
e.preventDefault(); |
| 41 |
const ajaxData = new window.FormData(); |
| 42 |
ajaxData.append( 'action', 'cartflows_onboarding_exit' ); |
| 43 |
ajaxData.append( 'security', cartflows_wizard.onboarding_exit_nonce ); |
| 44 |
ajaxData.append( 'current_step', step ); |
| 45 |
|
| 46 |
apiFetch( { url: ajaxurl, method: 'POST', body: ajaxData } ).then( |
| 47 |
( response ) => { |
| 48 |
if ( response.success ) { |
| 49 |
window.location.href = getExitSetupWizard(); |
| 50 |
} |
| 51 |
} |
| 52 |
); |
| 53 |
}; |
| 54 |
|
| 55 |
const handleStepRedirection = ( e ) => { |
| 56 |
e.preventDefault(); |
| 57 |
const stepToRedirect = e.target.id; |
| 58 |
const stepIndex = menus.findIndex( |
| 59 |
( menu ) => menu.id === stepToRedirect |
| 60 |
); |
| 61 |
const currentStepIndex = menus.findIndex( |
| 62 |
( menu ) => menu.id === step |
| 63 |
); |
| 64 |
|
| 65 |
if ( |
| 66 |
stepIndex < currentStepIndex || |
| 67 |
lastVisitedStep === stepToRedirect |
| 68 |
) { |
| 69 |
history.push( { |
| 70 |
pathname: 'index.php', |
| 71 |
search: `?page=cartflow-setup&step=${ stepToRedirect }`, |
| 72 |
} ); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( |
| 77 |
stepToRedirect === 'store-checkout' && |
| 78 |
step === 'plugin-install' && |
| 79 |
woocommerce_status === 'active' |
| 80 |
) { |
| 81 |
history.push( { |
| 82 |
pathname: 'index.php', |
| 83 |
search: `?page=cartflow-setup&step=store-checkout`, |
| 84 |
} ); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! completedSteps.includes( menus[ stepIndex - 1 ]?.id ) ) { |
| 89 |
alert( |
| 90 |
__( |
| 91 |
'Please complete the previous step before proceeding.', |
| 92 |
'cartflows' |
| 93 |
) |
| 94 |
); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
history.push( { |
| 99 |
pathname: 'index.php', |
| 100 |
search: `?page=cartflow-setup&step=${ stepToRedirect }`, |
| 101 |
} ); |
| 102 |
}; |
| 103 |
|
| 104 |
return ( |
| 105 |
<Disclosure |
| 106 |
as="nav" |
| 107 |
className="bg-white fixed top-0 w-full z-30 border-b border-slate-200" |
| 108 |
> |
| 109 |
{ () => ( |
| 110 |
<div className="px-4 sm:px-6 lg:px-8"> |
| 111 |
<div className="flex h-16 justify-between"> |
| 112 |
<div className="flex flex-shrink-0 items-center"> |
| 113 |
<img |
| 114 |
className="block lg:hidden h-8 w-auto" |
| 115 |
src={ CfIcon } |
| 116 |
alt="CartFlows" |
| 117 |
/> |
| 118 |
<img |
| 119 |
className="hidden lg:block h-8 w-auto" |
| 120 |
src={ CfIcon } |
| 121 |
alt="CartFlows" |
| 122 |
/> |
| 123 |
</div> |
| 124 |
<MenuNavigation |
| 125 |
menus={ menus } |
| 126 |
step={ step } |
| 127 |
completedSteps={ completedSteps } |
| 128 |
handleStepRedirection={ handleStepRedirection } |
| 129 |
/> |
| 130 |
<button |
| 131 |
type="button" |
| 132 |
className="rounded-full bg-white p-1 text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2" |
| 133 |
onClick={ handleExit } |
| 134 |
title={ __( 'Exit setup wizard', 'cartflows' ) } |
| 135 |
> |
| 136 |
<span className="sr-only">Exit Wizard</span> |
| 137 |
<XMarkIcon className="h-6 w-6" aria-hidden="true" /> |
| 138 |
</button> |
| 139 |
</div> |
| 140 |
</div> |
| 141 |
) } |
| 142 |
</Disclosure> |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
const MenuNavigation = ( { |
| 147 |
menus, |
| 148 |
step, |
| 149 |
completedSteps, |
| 150 |
handleStepRedirection, |
| 151 |
} ) => { |
| 152 |
return ( |
| 153 |
<div className="wcf-wizard-menu--navbar hidden md:flex lg:space-x-8 space-x-4"> |
| 154 |
{ menus.map( ( menu ) => { |
| 155 |
const menuIndex = menus.findIndex( ( m ) => m.id === menu.id ); |
| 156 |
const stepIndex = menus.findIndex( ( m ) => m.id === step ); |
| 157 |
const isCompleted = |
| 158 |
menuIndex < stepIndex || completedSteps.includes( menu.id ); |
| 159 |
const isStoreCheckout = |
| 160 |
menu.id === 'store-checkout' && |
| 161 |
step === 'plugin-install' && |
| 162 |
window.cartflows_wizard?.woocommerce_status === 'active'; |
| 163 |
const isActive = step === menu.id; |
| 164 |
const isClickable = isCompleted || isStoreCheckout; |
| 165 |
let borderClass; |
| 166 |
let textClass; |
| 167 |
|
| 168 |
if ( isActive ) { |
| 169 |
borderClass = 'border-primary-500'; |
| 170 |
textClass = |
| 171 |
'text-gray-800 text-primary-500 hover:text-primary-500'; |
| 172 |
} else if ( isClickable ) { |
| 173 |
borderClass = 'border-transparent'; |
| 174 |
textClass = |
| 175 |
'text-gray-300 hover:border-primary-300 hover:text-primary-500'; |
| 176 |
} else { |
| 177 |
borderClass = 'border-transparent'; |
| 178 |
textClass = |
| 179 |
'text-gray-300 cursor-not-allowed hover:text-gray-300'; |
| 180 |
} |
| 181 |
|
| 182 |
const linkClasses = `inline-flex items-center border-b-2 px-1 pt-1 font-medium focus:outline-none focus:shadow-none text-sm lg:text-base ${ borderClass } ${ textClass }`; |
| 183 |
|
| 184 |
return ( |
| 185 |
<a |
| 186 |
href="#" |
| 187 |
className={ linkClasses } |
| 188 |
id={ menu.id } |
| 189 |
onClick={ |
| 190 |
isClickable |
| 191 |
? handleStepRedirection |
| 192 |
: ( e ) => e.preventDefault() |
| 193 |
} |
| 194 |
key={ menu.id } |
| 195 |
> |
| 196 |
{ menu.name } |
| 197 |
</a> |
| 198 |
); |
| 199 |
} ) } |
| 200 |
</div> |
| 201 |
); |
| 202 |
}; |
| 203 |
|
| 204 |
export default Index; |
| 205 |
|