| 1 |
import { useCallback, useEffect, useRef } from '@wordpress/element'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
export const UpdateSettingConfirm = ({ inputs, onConfirm, onCancel }) => { |
| 5 |
const headerRef = useRef(null); |
| 6 |
const oldTitle = useRef(''); |
| 7 |
const undoSettingChange = useCallback(() => { |
| 8 |
if (headerRef.current) { |
| 9 |
headerRef.current.textContent = oldTitle.current; |
| 10 |
} |
| 11 |
}, []); |
| 12 |
|
| 13 |
const confirmed = useRef(false); |
| 14 |
useEffect(() => { |
| 15 |
return () => { |
| 16 |
if (!confirmed.current) undoSettingChange(); |
| 17 |
}; |
| 18 |
}, []); |
| 19 |
|
| 20 |
const handleConfirm = () => { |
| 21 |
confirmed.current = true; |
| 22 |
onConfirm({ data: inputs, shouldRefreshPage: true }); |
| 23 |
}; |
| 24 |
|
| 25 |
useEffect(() => { |
| 26 |
const header = document.querySelector('.wp-block-site-title'); |
| 27 |
if (!headerRef.current && header) { |
| 28 |
headerRef.current = header; |
| 29 |
oldTitle.current = header.textContent ?? ''; |
| 30 |
} |
| 31 |
|
| 32 |
if (!inputs) return; |
| 33 |
const { settingName, newSettingValue } = inputs; |
| 34 |
|
| 35 |
if (settingName === 'title' && headerRef.current) { |
| 36 |
headerRef.current.textContent = newSettingValue; |
| 37 |
} |
| 38 |
}, [inputs, headerRef]); |
| 39 |
|
| 40 |
return ( |
| 41 |
<div className="mb-4 ms-2 me-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50"> |
| 42 |
<div className="rounded-lg border-b border-gray-300 bg-white"> |
| 43 |
<div className="p-3"> |
| 44 |
<p className="m-0 p-0 text-sm text-gray-900"> |
| 45 |
{__('Apply this setting change?', 'extendify-local')} |
| 46 |
</p> |
| 47 |
</div> |
| 48 |
</div> |
| 49 |
<div className="flex justify-start gap-2 p-3"> |
| 50 |
<button |
| 51 |
type="button" |
| 52 |
className="w-full rounded-sm border border-gray-500 bg-white p-2 text-sm text-gray-900" |
| 53 |
onClick={onCancel} |
| 54 |
> |
| 55 |
{__('Cancel', 'extendify-local')} |
| 56 |
</button> |
| 57 |
<button |
| 58 |
type="button" |
| 59 |
className="w-full rounded-sm border border-design-main bg-design-main p-2 text-sm text-white" |
| 60 |
onClick={handleConfirm} |
| 61 |
> |
| 62 |
{__('Confirm', 'extendify-local')} |
| 63 |
</button> |
| 64 |
</div> |
| 65 |
</div> |
| 66 |
); |
| 67 |
}; |
| 68 |
|