-
Notifications
You must be signed in to change notification settings - Fork 70
/
notificationToast.tsx
61 lines (56 loc) · 2.33 KB
/
notificationToast.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/***********************************************************
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import * as React from 'react';
import { IconButton } from 'office-ui-fabric-react/lib/Button';
import { toast, ToastType, ToastOptions, UpdateOptions } from 'react-toastify';
import { LocalizationContextConsumer, LocalizationContextInterface } from '../../shared/contexts/localizationContext';
import { NotificationListEntry } from '../../notifications/components/notificationListEntry';
import { Notification } from '../../api/models/notification';
import { ResourceKeys } from '../../../localization/resourceKeys';
import { CANCEL } from '../../constants/iconNames';
import '../../css/_notification.scss';
export interface CloseButtonProps {
// tslint:disable-next-line:no-any
closeToast?: any;
}
export const CloseButton = (props: CloseButtonProps): JSX.Element => {
return (
<LocalizationContextConsumer>
{(context: LocalizationContextInterface) => (
<IconButton
iconProps={{
iconName: CANCEL
}}
label={context.t(ResourceKeys.common.close)}
ariaLabel={context.t(ResourceKeys.common.close)}
style={{width: 50}}
onClick={props.closeToast}
/>
)}
</LocalizationContextConsumer>
);
};
const fetchComponent = (notification: Notification) => {
return <NotificationListEntry notification={notification} showAnnoucement={true}/>;
};
export const raiseNotificationToast = (notification: Notification) => {
const component = fetchComponent(notification);
const options: ToastOptions = {
bodyClassName: 'notification-toast-body',
closeButton: <CloseButton />,
position: toast.POSITION.TOP_RIGHT,
progressClassName: `notification-toast-progress-bar`,
toastId: notification.id,
type: ToastType.DEFAULT
};
if (notification.id && toast.isActive(notification.id)) {
const updateOptions = options as UpdateOptions;
updateOptions.render = component;
toast.update(notification.id, options);
}
else {
toast(component, options);
}
};