2019-11-19 06:37:36 +07:00
|
|
|
import { Level } from "../components/ColoredSnackbarContent";
|
|
|
|
import { useCallback, createContext, useContext } from "react";
|
|
|
|
import { Notification } from "../models/Notifications";
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
timeout: 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NotificationContextProps {
|
|
|
|
notification: Notification | null;
|
|
|
|
setNotification: (n: Notification | null) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NotificationsContext = createContext<NotificationContextProps>(
|
|
|
|
{ notification: null, setNotification: () => { } });
|
|
|
|
|
|
|
|
export default NotificationsContext;
|
|
|
|
|
|
|
|
|
|
|
|
export function useNotifications() {
|
|
|
|
let useNotificationsProps = useContext(NotificationsContext);
|
|
|
|
|
|
|
|
const notificationBuilder = (level: Level) => {
|
|
|
|
return (message: string, timeout?: number) => {
|
|
|
|
useNotificationsProps.setNotification({
|
|
|
|
level, message,
|
|
|
|
timeout: timeout ? timeout : defaultOptions.timeout
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const resetNotification = () => useNotificationsProps.setNotification(null);
|
2020-11-07 09:06:18 +07:00
|
|
|
/* eslint-disable react-hooks/exhaustive-deps */
|
2019-11-19 06:37:36 +07:00
|
|
|
const createInfoNotification = useCallback(notificationBuilder("info"), []);
|
|
|
|
const createSuccessNotification = useCallback(notificationBuilder("success"), []);
|
|
|
|
const createWarnNotification = useCallback(notificationBuilder("warning"), []);
|
|
|
|
const createErrorNotification = useCallback(notificationBuilder("error"), []);
|
2020-11-07 09:06:18 +07:00
|
|
|
/* eslint-enable react-hooks/exhaustive-deps */
|
2019-11-19 06:37:36 +07:00
|
|
|
const isActive = useNotificationsProps.notification !== null;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
notification: useNotificationsProps.notification,
|
|
|
|
resetNotification,
|
|
|
|
createInfoNotification,
|
|
|
|
createSuccessNotification,
|
|
|
|
createWarnNotification,
|
|
|
|
createErrorNotification,
|
|
|
|
isActive
|
|
|
|
}
|
|
|
|
}
|