UNPKG

stackpress

Version:

Incept is a content management framework.

74 lines (73 loc) 2.25 kB
import { useContext } from 'react'; import { getCookie, setCookie, deleteCookie } from 'cookies-next'; import { toast } from 'react-toastify'; import NotifyContext, { config } from './NotifyContext'; const cookieConfig = { path: '/' }; export function notify(type, message, autoClose) { if (!autoClose) { autoClose = config.autoClose || 5000; } const options = { ...config, autoClose }; switch (type) { case 'info': toast.info(message, options); break; case 'warn': toast.warn(message, options); break; case 'error': toast.error(message, options); break; case 'success': toast.success(message, options); break; } } export function flash(type, message, close = 5000) { setCookie('flash', JSON.stringify({ type, message, close }), cookieConfig); } ; export function unload() { const value = getCookie('flash'); if (value) { deleteCookie('flash', cookieConfig); const args = JSON.parse(value); notify(args.type, args.message, args.close); } } ; export function useNotify() { const { config } = useContext(NotifyContext); const handlers = { notify(type, message, autoClose) { if (!autoClose) { autoClose = config.autoClose || 5000; } const options = { ...config, autoClose }; switch (type) { case 'info': toast.info(message, options); break; case 'warn': toast.warn(message, options); break; case 'error': toast.error(message, options); break; case 'success': toast.success(message, options); break; } }, flash, unload() { const value = getCookie('flash'); if (value) { deleteCookie('flash', cookieConfig); const args = JSON.parse(value); handlers.notify(args.type, args.message, args.close); } } }; return handlers; }