UNPKG

@appbuckets/react-ui-smart-components

Version:

UI Extended Components that work with @appbuckets/react-client and @appbuckets/react-ui

67 lines (64 loc) 1.69 kB
import * as React from 'react'; /* -------- * Hook Definition * -------- */ function useActionNotification(manager, notifications) { var onCanceled = notifications.onCanceled, onError = notifications.onError, onSubmitted = notifications.onSubmitted; // ---- // Build Base Raise Notification Function // ---- var raiseNotification = React.useCallback(function (raiser, content) { /** Assert raiser exists */ if (typeof raiser !== 'function') { return; } /** Assert content exists */ if (!content) { return; } /** Raise Notification */ raiser(content); }, []); // ---- // Build single raiser // ---- var _a = manager || {}, info = _a.info, error = _a.error, success = _a.success; var raiseOnCanceled = React.useCallback( function () { return raiseNotification(info, onCanceled); }, [info, onCanceled, raiseNotification] ); var raiseOnError = React.useCallback( function (thrownError) { if (onError === 'thrown') { raiseNotification(error, thrownError); } else if (typeof onError === 'function') { raiseNotification(error, onError(thrownError)); } else { raiseNotification(error, onError); } }, [error, onError, raiseNotification] ); var raiseOnSubmitted = React.useCallback( function () { return raiseNotification(success, onSubmitted); }, [onSubmitted, raiseNotification, success] ); // ---- // Return Utilities // ---- return { raiseOnCanceled: raiseOnCanceled, raiseOnError: raiseOnError, raiseOnSubmitted: raiseOnSubmitted, }; } export { useActionNotification as default };