Passed
Branch v1.2 (02af97)
by Rohit
01:53
created

Toast.toast   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
1
import React, { Component } from "react";
2
import "../scss/components/Toast.scss";
3
4
class Toast extends Component {
5
    state = {
6
        visible: false,
7
        type: "primary",
8
        message: "",
9
        transitionDirection: "right",
10
        position: "top-right",
11
        timeout: 5000
12
    }
13
    toastTimeout = null;
14
    throwToast = (message, type, options) => {
15
        const { transitionDirection, position, timeout } = options;
16
        const validPositions = ["top-right", "top-left", "top-center", "bottom-right", "bottom-left", "bottom-center"];
17
        const validTypes = ["warn", "success", "error", "info", "primary"];
18
        this.setState({ message, type: [...validTypes].includes(type) ? type : "primary", transitionDirection, position: [...validPositions].includes(position) ? position || "top-right" : "top-right", timeout: timeout || 5000 }, () => {
19
            setTimeout(() => {
20
                this.setState({ visible: true }, () => {
21
                    this.toastTimeout = setTimeout(() => {
22
                        if (this.state.visible) {
23
                            this.setState({ visible: false });
24
                        }
25
                    }, this.state.timeout);
26
                });
27
            }, 250);
28
        });
29
    }
30
    toast = (message, type, options = {}) => {
31
        if (this.state.visible) {
32
            clearTimeout(this.toastTimeout);
33
            this.setState({ visible: false }, () => {
34
                setTimeout(() => {
35
                    this.throwToast(message, type, options);
36
                }, 250);
37
            });
38
        } else {
39
            this.throwToast(message, type, options);
40
        }
41
    }
42
    closeToast = () => {
43
        clearTimeout(this.toastTimeout);
44
        this.setState({ visible: false });
45
    }
46
    renderToast = (message, visible, type) => {
47
        const { transitionDirection, position } = this.state;
48
        let shiftDirection = "";
49
        if (position === "top-right") {
50
            shiftDirection = (transitionDirection === "top" || transitionDirection === "right") ? transitionDirection || "right" : "right";
51
        }
52
        if (position === "top-left") {
53
            shiftDirection = (transitionDirection === "top" || transitionDirection === "left") ? transitionDirection || "left" : "left";
54
        }
55
        if (position === "top-center") {
56
            shiftDirection = "top";
57
        }
58
        if (position === "bottom-right") {
59
            shiftDirection = (transitionDirection === "bottom" || transitionDirection === "right") ? transitionDirection || "right" : "right";;
60
        }
61
        if (position === "bottom-left") {
62
            shiftDirection = (transitionDirection === "bottom" || transitionDirection === "left") ? transitionDirection || "left" : "left";;
63
        }
64
        if (position === "bottom-center") {
65
            shiftDirection = "bottom";
66
        }
67
68
        return <div onClick={this.closeToast} className={`${"toast"} ${visible ? "show" : "hide"}-${shiftDirection} ${type} ${position}`}>{message}</div>
69
    }
70
    render() {
71
        const { message, visible, type } = this.state;
72
        return (<>{this.renderToast(message, visible, type)}</>);
73
    }
74
}
75
76
export default Toast;