FILE / Unmei/Frontend

src/components/Notifications/NotificationMessage.tsx

Исходный файл и его история в репозитории.
FILE 6e40dfc1017e3bcd5aadab8a2d5e8bfc6e69ed29
Files
Frontend/src/components/Notifications/NotificationMessage.tsx
T
2020-06-11 05:08:58 +03:00

71 lines
1.7 KiB
TypeScript

import React from "react";
import './NotificationMessage.sass'
import {capitalize} from "../../utils";
type Props = {
children: React.ReactNode
level?: 'info' | 'success' | 'error'
}
type State = {
time: number
paused: boolean
};
class NotificationMessage extends React.Component<Props, State> {
timer: NodeJS.Timeout | undefined;
state = {
time: 5000, paused: false
};
onMouseOver = () => {
this.setState({ paused: true });
};
onMouseOut = () => {
this.setState({ paused: false });
};
onClick = () => {
this.timer = setInterval(() => this.interval(400), 50);
this.setState({ paused: false });
};
interval = (step=50) => {
const { time, paused } = this.state;
if(time <= 0 && this.timer) clearInterval(this.timer);
if(!paused) this.setState({ time: time-step });
};
componentDidMount(): void {
this.timer = setInterval(() => this.interval(), 50)
};
componentWillUnmount(): void {
if(this.timer) clearTimeout(this.timer);
}
render() {
const { time } = this.state;
const { children, level } = this.props;
if(time <= 0)
return null;
const styleClass = level ? `Notification--${capitalize(level)}` : '';
return (
<div
className={`Notification ${styleClass}`}
onMouseOver={this.onMouseOver}
onMouseOut={this.onMouseOut}
onClick={this.onClick}
>
{children}
<div className="Progress" style={{ width: `${time / 5000 * 100}%` }}/>
</div>
);
}
}
export default NotificationMessage;