-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashMessage.js
57 lines (50 loc) · 1.4 KB
/
FlashMessage.js
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
import React from "react";
import Store from "./Store";
import { connect } from "react-redux";
import { withRouter } from "react-router";
class FlashMessage extends React.Component {
startFlashMessageTimeout = () => {
setTimeout(() => {
if (this.props.flashMessage) {
setFlashMessage("");
}
}, 5000);
};
componentDidMount = () => {
this.startFlashMessageTimeout();
};
componentDidUpdate = (prevProps, prevState) => {
if (prevProps.flashMessage !== this.props.flashMessage) {
this.startFlashMessageTimeout();
}
};
render() {
return (
<React.Fragment>
<div>
{this.props.flashMessage && (
<div
className={
this.props.className
? "alert " + this.props.className
: "alert alert-" + this.props.flashMessageSeverity
}
>
{this.props.flashMessage}
</div>
)}
</div>
</React.Fragment>
);
}
}
function mapStateToProps(state) {
return {
flashMessage: state.flashMessage,
flashMessageSeverity: state.flashMessageSeverity
};
}
export default withRouter(connect(mapStateToProps)(FlashMessage));
export function setFlashMessage(message, severity) {
Store.dispatch({ type: "SET_FLASH_MESSAGE", value: message, severity });
}