| 1 |
import { registerCoreBlocks } from '@wordpress/block-library'; |
| 2 |
import { useEffect } from '@wordpress/element'; |
| 3 |
import { __, sprintf } from '@wordpress/i18n'; |
| 4 |
import { MediaUpload } from '@wordpress/media-utils'; |
| 5 |
import { walkAndUpdateImageDetails } from '@agent/lib/blocks'; |
| 6 |
|
| 7 |
const openButton = __('Open Media Library', 'extendify-local'); |
| 8 |
|
| 9 |
export const UpdateImageConfirm = ({ inputs, onConfirm, onCancel }) => { |
| 10 |
const handleConfirm = async (image) => { |
| 11 |
await onConfirm({ |
| 12 |
data: { |
| 13 |
previousContent: inputs.previousContent, |
| 14 |
newContent: walkAndUpdateImageDetails(inputs, image), |
| 15 |
}, |
| 16 |
}); |
| 17 |
setTimeout(() => window.location.reload(), 1000); |
| 18 |
}; |
| 19 |
|
| 20 |
useEffect(() => { |
| 21 |
// rawHandler does not work on the frontend, so we need to register the |
| 22 |
// core blocks again to get it working. |
| 23 |
registerCoreBlocks(); |
| 24 |
}, []); |
| 25 |
|
| 26 |
useEffect(() => { |
| 27 |
// Put modal above the Agent |
| 28 |
const style = document.createElement('style'); |
| 29 |
style.textContent = `.media-modal { |
| 30 |
z-index: 999999 !important; |
| 31 |
}`; |
| 32 |
document.head.appendChild(style); |
| 33 |
return () => style.remove(); |
| 34 |
}, []); |
| 35 |
|
| 36 |
return ( |
| 37 |
<Wrapper> |
| 38 |
<Content> |
| 39 |
<p className="m-0 p-0 text-sm text-gray-900"> |
| 40 |
{sprintf( |
| 41 |
__( |
| 42 |
'The agent has requested the media library. Press "%s" to upload or select an image.', |
| 43 |
'extendify-local', |
| 44 |
), |
| 45 |
openButton, |
| 46 |
)} |
| 47 |
</p> |
| 48 |
</Content> |
| 49 |
<div className="flex justify-start gap-2 p-3"> |
| 50 |
<button |
| 51 |
type="button" |
| 52 |
className="w-full rounded border border-gray-300 bg-white p-2 text-sm text-gray-700" |
| 53 |
onClick={onCancel}> |
| 54 |
{__('Cancel', 'extendify-local')} |
| 55 |
</button> |
| 56 |
<MediaUpload |
| 57 |
title={__('Select or Upload Image', 'extendify-local')} |
| 58 |
onSelect={handleConfirm} |
| 59 |
allowedTypes={['image']} |
| 60 |
modalClass="image__media-modal" |
| 61 |
render={({ open }) => ( |
| 62 |
<button |
| 63 |
type="button" |
| 64 |
className="w-full rounded border border-design-main bg-design-main p-2 text-sm text-white" |
| 65 |
onClick={open}> |
| 66 |
{openButton} |
| 67 |
</button> |
| 68 |
)} |
| 69 |
/> |
| 70 |
</div> |
| 71 |
</Wrapper> |
| 72 |
); |
| 73 |
}; |
| 74 |
|
| 75 |
const Wrapper = ({ children }) => ( |
| 76 |
<div className="mb-4 ml-10 mr-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50 rtl:ml-2 rtl:mr-10"> |
| 77 |
{children} |
| 78 |
</div> |
| 79 |
); |
| 80 |
|
| 81 |
const Content = ({ children }) => ( |
| 82 |
<div className="rounded-lg border-b border-gray-300 bg-white"> |
| 83 |
<div className="p-3">{children}</div> |
| 84 |
</div> |
| 85 |
); |
| 86 |
|