import { __ } from '@wordpress/i18n'; import { useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import SingleSelect from '../../../../../components/SingleSelect'; import { updateTextHighlight } from '../../../../../features/discount/discountSlice'; import textHighlightDesign from '../../../../../utilities/text-highlight-design'; import BadgeHeader from '../components/BadgeHeader'; import BadgeTextWithDynamicProperties from '../components/BadgeTextWithDynamicProperties'; import ColorPikerBox from '../components/ColorPikerBox'; import UploadBadgeComponent from '../components/UploadBadgeComponent'; import BadgeSelector from '../ProductBadges/components/SelectBadge'; import TextHighlightItems from './components/TextHighlightItems'; import TextHighlightProperties from './components/TextHighlightProperties'; import TextHighlightStyleBox from './components/TextHighlightStyleBox'; import TextHighlightView from './components/TextHighlightView'; const TextHighlightEdit = () => { const badgeInitialType = useSelector( (state) => state.discount.design_blocks.text_highlight?.badge_type || 'editable' ); const [selectedBadge, setSelectedBadge] = useState( badgeInitialType || 'editable' ); const textareaRef = useRef(null); const { text_highlight } = useSelector( (state) => state.discount.design_blocks ); const isValueEditable = selectedBadge === 'value_editable'; const isUpload = selectedBadge === 'upload'; const dispatch = useDispatch(); // handle title change const handleTextHighlightTitle = (key, value) => { dispatch( updateTextHighlight({ name: 'title', value: { ...text_highlight.title, [key]: value }, }) ); }; // handle container change const handleContainerChange = (key, value) => { dispatch( updateTextHighlight({ name: 'container', value: { ...text_highlight.container, [key]: value }, }) ); }; // handle position change const handlePositionChange = (value) => { dispatch(updateTextHighlight({ name: 'position', value })); }; // handle image badge select from uploaded images const handleImageTextHighlightSelect = (image) => { dispatch( updateTextHighlight({ name: 'selected_design', value: image.id, }) ); dispatch( updateTextHighlight({ name: 'image', value: { url: image.url }, }) ); dispatch( updateTextHighlight({ name: 'badge_type', value: 'upload', }) ); dispatch( updateTextHighlight({ name: 'container', value: textHighlightDesign?.image?.container || null, }) ); setSelectedBadge('upload'); // clear old badge data when new badge is selected dispatch( updateTextHighlight({ name: 'title', value: null, }) ); dispatch( updateTextHighlight({ name: 'design', value: null, }) ); }; const badgeOptions = [ { label: __('Editable', 'disco'), value: 'editable' }, { label: __('Value Editable', 'disco'), value: 'value_editable' }, { label: __('Upload', 'disco'), value: 'upload' }, ]; useEffect(() => { window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }); }, []); return (