| 1 |
import { useEffect } from 'react'; |
| 2 |
import { useDispatch, useSelector } from 'react-redux'; |
| 3 |
import { useSearchParams } from 'react-router'; |
| 4 |
import FooterButtons from '../../../../components/FooterButtons'; |
| 5 |
import { useUpdateCampaignMutation } from '../../../../features/campaigns/campaignsApi'; |
| 6 |
import { setTab } from '../../../../features/discount/discountSlice'; |
| 7 |
import { |
| 8 |
setComponentToEdit, |
| 9 |
setShowEditor, |
| 10 |
} from '../../../../features/interaction/interactionSlice'; |
| 11 |
import { |
| 12 |
cleanFilters, |
| 13 |
validateEmptyField, |
| 14 |
} from '../../../../utilities/utilities'; |
| 15 |
import RenderBlocks from './components/RenderBlocks'; |
| 16 |
import RenderEditor from './components/RenderEditor'; |
| 17 |
const DesignBlock = () => { |
| 18 |
const dispatch = useDispatch(); |
| 19 |
const { discount_intent } = useSelector((state) => state.discount); |
| 20 |
const { showEditor, componentToEdit } = useSelector( |
| 21 |
(state) => state.interaction |
| 22 |
); |
| 23 |
|
| 24 |
const discount = useSelector((state) => state.discount); |
| 25 |
|
| 26 |
const [updateCampaign] = useUpdateCampaignMutation(); |
| 27 |
const [searchParams] = useSearchParams(); |
| 28 |
|
| 29 |
const handleContinue = () => { |
| 30 |
if (validateEmptyField(discount) === false) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Reset editor state before navigating |
| 35 |
dispatch(setShowEditor(false)); |
| 36 |
dispatch(setComponentToEdit('')); |
| 37 |
|
| 38 |
if (searchParams.get('edit')) { |
| 39 |
const dataForRequest = { ...discount }; |
| 40 |
|
| 41 |
delete dataForRequest.created_by; |
| 42 |
delete dataForRequest.modified_by; |
| 43 |
|
| 44 |
dataForRequest.conditions = cleanFilters(discount.conditions); |
| 45 |
|
| 46 |
updateCampaign({ |
| 47 |
id: discount.id, |
| 48 |
data: { |
| 49 |
...dataForRequest, |
| 50 |
priority: String(dataForRequest.priority), |
| 51 |
ui: [discount.ui[0] + 1, 0], |
| 52 |
}, |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
dispatch(setTab(2)); |
| 57 |
}; |
| 58 |
const handleBack = () => { |
| 59 |
// Reset editor state before navigating back |
| 60 |
dispatch(setShowEditor(false)); |
| 61 |
dispatch(setComponentToEdit('')); |
| 62 |
dispatch(setTab(0)); |
| 63 |
}; |
| 64 |
|
| 65 |
useEffect(() => { |
| 66 |
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }); |
| 67 |
}, []); |
| 68 |
|
| 69 |
return ( |
| 70 |
<> |
| 71 |
<div className="disco-mx-3"> |
| 72 |
{!showEditor ? ( |
| 73 |
<RenderBlocks discount_intent={discount_intent} /> |
| 74 |
) : ( |
| 75 |
<RenderEditor componentName={componentToEdit} /> |
| 76 |
)} |
| 77 |
</div> |
| 78 |
|
| 79 |
<FooterButtons |
| 80 |
handleContinue={handleContinue} |
| 81 |
handleBack={handleBack} |
| 82 |
disabled={showEditor} |
| 83 |
/> |
| 84 |
</> |
| 85 |
); |
| 86 |
}; |
| 87 |
export default DesignBlock; |
| 88 |
|