-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.tsx
143 lines (117 loc) · 4.45 KB
/
notifications.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, {SyntheticEvent} from 'react';
import ReactDOM from 'react-dom';
import uuid from 'uuid/v4';
import { Omit } from './historyRouter';
export interface _Notification {
id: string,
message: string,
className?: string,
component?: React.ComponentType<NotificationComponentProps>
}
export interface Notifications {
notify(message: string, className?: string, component?: React.ComponentClass<NotificationComponentProps>): void
dismissNotification(id): void
}
export interface NotificationComponentProps{
notification: _Notification,
dismissNotification(id): void
}
export const NotificationComponent = (props: NotificationComponentProps) => {
if (props.notification.component) {
return <props.notification.component {...props} />;
} else {
const baseClass = props.notification.className ? props.notification.className : 'axc__notification';
return (
<div className={baseClass}>
<div className={baseClass + '__message'}>
{props.notification.message}
</div>
<button className={baseClass +'__button'} onClick={props.dismissNotification.bind(undefined,props.notification.id)}>dismiss</button>
</div>
);
}
}
export const Notification: React.Context<Notifications> = React.createContext(undefined);
export interface NotificationSystemProps {
root?: Element
notificationComponent?: React.ComponentType<NotificationComponentProps>
}
export interface NotificationSystemState {
currentNotifications: _Notification[];
}
export class NotificationSystem extends React.Component<NotificationSystemProps, NotificationSystemState> {
constructor(props) {
super(props);
this.state = {
currentNotifications: []
};
}
onDismissNotification = (id:string):void=> {
this.setState((prevState)=>{
return{
currentNotifications:prevState.currentNotifications.filter((notification: _Notification)=>{
notification.id !== id;
})
}
});
}
onNotify = (message: string, className?: string, component?: React.ComponentClass<NotificationComponentProps>):void =>{
this.setState((prevState)=>{
return{
currentNotifications: [...prevState.currentNotifications, {
id: uuid(),
message,
className,
component
}]
}
});
}
getNotificationContext():Notifications{
return{
notify: this.onNotify,
dismissNotification: this.onDismissNotification
}
}
renderNotifications = () =>{
if(this.props.root && this.props.notificationComponent){
return ReactDOM.createPortal(
this.state.currentNotifications.map((notification: _Notification)=>{
if(notification.component){
return <notification.component notification={notification} dismissNotification={this.onDismissNotification}/>
}else{
return <this.props.notificationComponent notification={notification} dismissNotification={this.onDismissNotification}/>
}
}),
this.props.root
);
}else{
return this.state.currentNotifications.map((notification: _Notification)=>{
return(
<NotificationComponent notification={notification} dismissNotification={this.onDismissNotification}/>
);
})
}
}
render() {
return (
<Notification.Provider value={this.getNotificationContext()}>
{this.props.children}
{this.renderNotifications()}
</Notification.Provider>
)
}
}
type Diff<T, U> = T extends U ? never : T;
type ObjectDiff<T, U> = Pick<T, Diff<keyof T, keyof U>>;
export interface WithNotificationsHOC<T extends Notifications> {
(props: ObjectDiff<T, Notifications>): React.SFC<T>
}
export const WithNotifications =<T extends Notifications>(Component: React.ComponentType<T>) => {
return (props: ObjectDiff<T, Notifications>) => (
<Notification.Consumer>
{notification=> <Component {...notification}{...props} />}
</Notification.Consumer>
)
}
export default NotificationSystem;