| 1 |
import { Tab } from '@headlessui/react'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import { useEffect } from 'react'; |
| 4 |
import { useDispatch, useSelector } from 'react-redux'; |
| 5 |
import { useLocation } from 'react-router'; |
| 6 |
import ProIcon from '../../../components/ProIcon'; |
| 7 |
import { setTab } from '../../../features/discount/discountSlice'; |
| 8 |
import { |
| 9 |
setComponentToEdit, |
| 10 |
setShowEditor, |
| 11 |
} from '../../../features/interaction/interactionSlice'; |
| 12 |
import useIsPro from '../../../hooks/useIsPro'; |
| 13 |
import CampaignSetup from './CampaignSetup/CampaignSetup'; |
| 14 |
import DesignBlock from './DesignBlock/DesignBlock'; |
| 15 |
import Summary from './Summary/Summary'; |
| 16 |
import TabButton from './TabButton'; |
| 17 |
|
| 18 |
const MainTabs = () => { |
| 19 |
const isPro = useIsPro(); |
| 20 |
const location = useLocation(); |
| 21 |
const params = new URLSearchParams(location.search); |
| 22 |
const edit = params.get('edit'); |
| 23 |
|
| 24 |
const tabsButtons = [ |
| 25 |
{ |
| 26 |
id: 1, |
| 27 |
text: __('Setup', 'disco'), |
| 28 |
}, |
| 29 |
|
| 30 |
{ |
| 31 |
id: 2, |
| 32 |
text: __('Display', 'disco'), |
| 33 |
}, |
| 34 |
{ |
| 35 |
id: 3, |
| 36 |
text: __('Summary', 'disco'), |
| 37 |
}, |
| 38 |
]; |
| 39 |
const { ui } = useSelector((state) => state.discount); |
| 40 |
const dispatch = useDispatch(); |
| 41 |
|
| 42 |
// Reset editor state whenever tab changes |
| 43 |
useEffect(() => { |
| 44 |
dispatch(setShowEditor(false)); |
| 45 |
dispatch(setComponentToEdit('')); |
| 46 |
}, [ui[0], dispatch]); |
| 47 |
|
| 48 |
const handleTabChange = (index) => { |
| 49 |
// Tab disabled when creating a new campaign (no edit param) for index > 0. |
| 50 |
// Block the switch but keep the button enabled so ProIcon link stays clickable. |
| 51 |
if (!edit && index > 0) { |
| 52 |
return; |
| 53 |
} |
| 54 |
dispatch(setTab(index)); |
| 55 |
}; |
| 56 |
|
| 57 |
return ( |
| 58 |
<Tab.Group |
| 59 |
onChange={handleTabChange} |
| 60 |
// selectedIndex={ 1 } |
| 61 |
selectedIndex={ui[0]} |
| 62 |
manual |
| 63 |
> |
| 64 |
<Tab.List className="disco-flex disco-z-50 disco-sticky disco-top-8 disco-bg-gray-50 disco-p-2 disco-rounded-t-xl"> |
| 65 |
{tabsButtons.map((button, index) => ( |
| 66 |
<TabButton key={button.id} disabled={!edit && index > 0}> |
| 67 |
<span className="disco-flex disco-justify-center disco-items-center disco-gap-2"> |
| 68 |
{button.text} |
| 69 |
{!isPro && index === 1 ? <ProIcon /> : ''} |
| 70 |
</span> |
| 71 |
</TabButton> |
| 72 |
))} |
| 73 |
</Tab.List> |
| 74 |
<div className=""> |
| 75 |
<Tab.Panels> |
| 76 |
<Tab.Panel> |
| 77 |
<CampaignSetup /> |
| 78 |
</Tab.Panel> |
| 79 |
<Tab.Panel> |
| 80 |
<DesignBlock /> |
| 81 |
</Tab.Panel> |
| 82 |
<Tab.Panel> |
| 83 |
<Summary /> |
| 84 |
</Tab.Panel> |
| 85 |
</Tab.Panels> |
| 86 |
</div> |
| 87 |
</Tab.Group> |
| 88 |
); |
| 89 |
}; |
| 90 |
export default MainTabs; |
| 91 |
|