import * as uuid from 'uuid' import { Dispatch } from 'redux' import { NotificationLevel } from './interfaces' import { DEFAULT_AUTOCLOSE, NOTIFICATION_LEVELS } from './constants' export const SHOW_NOTIFICATION = 'SHOW_NOTIFICATION' export const HIDE_NOTIFICATION = 'HIDE_NOTIFICATION' // autoclose can be a boolean value // or you can set a custom autoclose duration in ms export type Autoclose = boolean | number export interface ShowNotificationAction { type: 'SHOW_NOTIFICATION' message: string, autoclose: number, // must be a number by the time it reaches here id: string, level: NotificationLevel, } export interface HideNotificationAction { type: 'HIDE_NOTIFICATION' id: string } export type NotificationAction = ShowNotificationAction | HideNotificationAction const autoCloseToTime = (autoclose: Autoclose): number => typeof autoclose === 'number' ? autoclose : (autoclose ? DEFAULT_AUTOCLOSE : -1) export const notificationError = (message: string, autoclose: Autoclose = DEFAULT_AUTOCLOSE) => showNotification(NOTIFICATION_LEVELS.ERROR, message, autoCloseToTime(autoclose)) export const notificationWarning = (message: string, autoclose: Autoclose = DEFAULT_AUTOCLOSE) => showNotification(NOTIFICATION_LEVELS.WARNING, message, autoCloseToTime(autoclose)) export const notificationDebug = (message: string, autoclose: Autoclose = DEFAULT_AUTOCLOSE) => showNotification(NOTIFICATION_LEVELS.DEBUG, message, autoCloseToTime(autoclose)) export const notificationInfo = (message: string, autoclose: Autoclose = DEFAULT_AUTOCLOSE) => showNotification(NOTIFICATION_LEVELS.INFO, message, autoCloseToTime(autoclose)) export const notificationSuccess = (message: string, autoclose: Autoclose = DEFAULT_AUTOCLOSE) => showNotification(NOTIFICATION_LEVELS.SUCCESS, message, autoCloseToTime(autoclose)) export const showNotification = ( level: NotificationLevel, message: string, autoclose: number = DEFAULT_AUTOCLOSE, ): ShowNotificationAction => ({ type: SHOW_NOTIFICATION, id: uuid(), level, message, autoclose, }) export const hideNotification = (id: string): HideNotificationAction => ({ type: HIDE_NOTIFICATION, id })