Source

adminjs/src/frontend/hooks/use-notice.ts

  1. import { useDispatch } from 'react-redux'
  2. import { addNotice } from '../store/actions/add-notice'
  3. import { NoticeMessage } from '../hoc/with-notice'
  4. /**
  5. * @memberof useNotice
  6. * @alias AddNotice
  7. */
  8. export type AddNotice = (notice: NoticeMessage) => any;
  9. /**
  10. * @classdesc
  11. * Hook which allows you to add notice message to the app.
  12. *
  13. * ```javascript
  14. * import { useNotice, Button } from 'adminjs'
  15. *
  16. * const myComponent = () => {
  17. * const sendNotice = useNotice()
  18. * render (
  19. * <Button onClick={() => sendNotice({ message: 'I am awesome' })}>I am awesome</Button>
  20. * )
  21. * }
  22. * ```
  23. *
  24. * @class
  25. * @subcategory Hooks
  26. * @bundle
  27. * @hideconstructor
  28. */
  29. export const useNotice = (): AddNotice => {
  30. const dispatch = useDispatch()
  31. return (notice): any => dispatch(addNotice(notice))
  32. }
  33. export default useNotice