import { NotificationAction } from './constants' ; import { NewNotification , Notification , Status } from './types' ; import { isObject } from '../../utils' ; import { prepareNotification } from '../../services/notifications' ; export type UpsertNotificationAction = { type : NotificationAction . UpsertNotification ; payload : Notification ; } ; export type DismissNotificationAction = { type : NotificationAction . DismissNotification ; payload : string ; } ; export type DismissNotificationsAction = { type : NotificationAction . DismissNotifications ; } ; function upsertNotification ( notification ) ; function upsertNotification ( message , options ? ) ; function upsertNotification ( message , status , options ? ) ; function upsertNotification ( ... args ) { const lastArg = args [ args . length - 1 ] ; let notification = { } ; if ( typeof args [ 0 ] === 'string' ) { notification . message = args [ 0 ] as string ; } if ( typeof args [ 1 ] === 'string' ) { notification . status = args [ 1 ] as Status ; } if ( isObject ( lastArg ) ) { notification = { ... notification , ... ( lastArg as NewNotification ) } ; } return { type : NotificationAction . UpsertNotification , payload : prepareNotification ( notification as NewNotification | Notification ) , } ; } export const dismissNotification = ( notificationId ) : DismissNotificationAction => ( { type : NotificationAction . DismissNotification , payload : notificationId , } ) ; export const dismissNotifications = ( ) : DismissNotificationsAction => ( { type : NotificationAction . DismissNotifications , } ) ; export const notify = upsertNotification ; export type NotificationActions = UpsertNotificationAction | DismissNotificationAction | DismissNotificationsAction ;