store
1 year ago
AddonRow.jsx
1 year ago
App.jsx
1 year ago
Metabox.jsx
1 year ago
Modal.jsx
1 year ago
Notices.jsx
1 year ago
Select2.jsx
1 year ago
Settings.jsx
1 year ago
Step1.jsx
1 year ago
Step2.jsx
1 year ago
Step3.jsx
1 year ago
StepWrapper.jsx
1 year ago
main.js
1 year ago
Notices.jsx
94 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import classnames from 'classnames'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress dependencies |
| 8 | */ |
| 9 | import { useEffect } from '@wordpress/element'; |
| 10 | import { store as noticesStore } from '@wordpress/notices'; |
| 11 | import { dispatch, useSelect, useDispatch } from '@wordpress/data'; |
| 12 | |
| 13 | export function removeAllNotices() { |
| 14 | dispatch(noticesStore).removeAllNotices(); |
| 15 | } |
| 16 | |
| 17 | export function noticeSuccess(message, options) { |
| 18 | dispatch(noticesStore).createSuccessNotice(message, options); |
| 19 | } |
| 20 | |
| 21 | export function noticeError(message, options) { |
| 22 | dispatch(noticesStore).createErrorNotice(message, options); |
| 23 | } |
| 24 | |
| 25 | function Notice({ status, isDismissible, actions, onRemove, children, type }) { |
| 26 | const wrapperClasses = classnames( |
| 27 | 'flex items-center justify-between notice notice-alt !px-3', |
| 28 | `notice-${status}`, |
| 29 | { 'is-dismissible': isDismissible } |
| 30 | ); |
| 31 | |
| 32 | useEffect(() => { |
| 33 | if ('timeout' !== type) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | const timeId = setTimeout(() => { |
| 38 | onRemove(); |
| 39 | }, 5000); |
| 40 | |
| 41 | // Cleanup function to clear the timeout |
| 42 | return () => clearTimeout(timeId); |
| 43 | }, []); // Empty array means it will only run on mount |
| 44 | |
| 45 | return ( |
| 46 | <div className={wrapperClasses}> |
| 47 | <div className="py-3" dangerouslySetInnerHTML={{ __html: children }} /> |
| 48 | {actions.map((action, index) => { |
| 49 | return ( |
| 50 | <button |
| 51 | key={index} |
| 52 | className="button button-primary !ml-auto !mr-2" |
| 53 | onClick={(event) => action.onClick(event, onRemove)} |
| 54 | > |
| 55 | {action.label} |
| 56 | </button> |
| 57 | ) |
| 58 | })} |
| 59 | { isDismissible && ( |
| 60 | <button |
| 61 | className="button-link !no-underline" |
| 62 | onClick={ onRemove } |
| 63 | > |
| 64 | <span className="dashicons dashicons-no-alt"></span> |
| 65 | </button> |
| 66 | ) } |
| 67 | </div> |
| 68 | ) |
| 69 | } |
| 70 | /** |
| 71 | * Renders the notices |
| 72 | * |
| 73 | * @return {React.ReactNode} The rendered component. |
| 74 | */ |
| 75 | export default function Notices() { |
| 76 | const notices = useSelect( |
| 77 | ( select ) => select( noticesStore ).getNotices(), |
| 78 | [] |
| 79 | ); |
| 80 | const { removeNotice } = useDispatch( noticesStore ); |
| 81 | |
| 82 | return ( |
| 83 | <> |
| 84 | {notices.map( ( notice ) => { |
| 85 | return ( |
| 86 | <Notice key={notice.id} onRemove={() => removeNotice(notice.id)} {...notice}> |
| 87 | {notice.content} |
| 88 | </Notice> |
| 89 | ) |
| 90 | })} |
| 91 | </> |
| 92 | ); |
| 93 | } |
| 94 |