ConfirmPopup.js
117 lines
| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | import { __ } from "@wordpress/i18n"; |
| 3 | import { CircleX } from "lucide-react"; |
| 4 | |
| 5 | import { Dialog, Button, Badge } from "@bsf/force-ui"; |
| 6 | |
| 7 | const ConfirmPopup = ({ |
| 8 | openConfirmPopup: openPopup, |
| 9 | setOpenConfirmPopup: setOpenPopup, |
| 10 | title = __("Popup Title", "presto-player"), |
| 11 | description = "", |
| 12 | confirmText = __("Confirm", "presto-player"), |
| 13 | cancelText = __("Cancel", "presto-player"), |
| 14 | confirmCallback = null, |
| 15 | cancelCallback = null, |
| 16 | ErrorMessage = "", |
| 17 | postsDeletionMessageList = false, |
| 18 | destructive = false, |
| 19 | }) => { |
| 20 | const cancelButtonRef = useRef(null); |
| 21 | const [loading, setLoading] = useState(false); |
| 22 | const onCancelClick = () => { |
| 23 | if (cancelCallback) { |
| 24 | cancelCallback(); |
| 25 | return; |
| 26 | } |
| 27 | setOpenPopup(false); |
| 28 | }; |
| 29 | |
| 30 | const onConfirmClick = async () => { |
| 31 | if (!confirmCallback) { |
| 32 | return; |
| 33 | } |
| 34 | setLoading(true); |
| 35 | try { |
| 36 | // Awaits the callback's promise (if any). Synchronous callbacks |
| 37 | // resolve immediately. After success the popup self-closes; on |
| 38 | // error the popup stays open so the caller can show ErrorMessage. |
| 39 | await confirmCallback(); |
| 40 | setOpenPopup(false); |
| 41 | } finally { |
| 42 | setLoading(false); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | useEffect(() => { |
| 47 | if (ErrorMessage) { |
| 48 | setLoading(false); |
| 49 | } |
| 50 | }, [ErrorMessage]); |
| 51 | |
| 52 | return ( |
| 53 | <Dialog |
| 54 | design="simple" |
| 55 | exitOnClickOutside |
| 56 | exitOnEsc |
| 57 | scrollLock |
| 58 | open={openPopup} |
| 59 | setOpen={setOpenPopup} |
| 60 | > |
| 61 | <Dialog.Backdrop /> |
| 62 | <Dialog.Panel> |
| 63 | <Dialog.Header> |
| 64 | {ErrorMessage && ( |
| 65 | <Badge |
| 66 | icon={<CircleX />} |
| 67 | label={ErrorMessage} |
| 68 | size="md" |
| 69 | type="rounded" |
| 70 | variant="red" |
| 71 | className="py-6 px-4 justify-start" |
| 72 | /> |
| 73 | )} |
| 74 | <Dialog.Title>{title}</Dialog.Title> |
| 75 | </Dialog.Header> |
| 76 | |
| 77 | {(description || postsDeletionMessageList) && ( |
| 78 | // py-3 balances the vertical rhythm: Dialog.Header has pb-1 but |
| 79 | // Dialog.Body has no default vertical padding, so without this the |
| 80 | // description sits visually closer to the title than to the footer. |
| 81 | <Dialog.Body className="py-3"> |
| 82 | <Dialog.Description> |
| 83 | {description} |
| 84 | {postsDeletionMessageList && ( |
| 85 | <ul className="list-disc pl-5"> |
| 86 | <li className="text-sm"> |
| 87 | {__( |
| 88 | "Posts content under this space will be deleted permanently.", |
| 89 | "presto-player" |
| 90 | )} |
| 91 | </li> |
| 92 | </ul> |
| 93 | )} |
| 94 | </Dialog.Description> |
| 95 | </Dialog.Body> |
| 96 | )} |
| 97 | |
| 98 | <Dialog.Footer> |
| 99 | <Button variant="ghost" onClick={onCancelClick} ref={cancelButtonRef}> |
| 100 | {cancelText} |
| 101 | </Button> |
| 102 | <Button |
| 103 | onClick={onConfirmClick} |
| 104 | disabled={loading} |
| 105 | loading={loading} |
| 106 | destructive={destructive} |
| 107 | > |
| 108 | {confirmText} |
| 109 | </Button> |
| 110 | </Dialog.Footer> |
| 111 | </Dialog.Panel> |
| 112 | </Dialog> |
| 113 | ); |
| 114 | }; |
| 115 | |
| 116 | export default ConfirmPopup; |
| 117 |