2020-10-05 13:09:35 +00:00
|
|
|
import React from "react";
|
|
|
|
import { toast } from "react-toastify";
|
|
|
|
import styled from "styled-components";
|
|
|
|
import { Icons } from "../icons";
|
|
|
|
import IconButton from "../icon-button";
|
|
|
|
import Text from "../text";
|
2019-07-17 11:20:02 +00:00
|
|
|
|
2020-09-01 13:55:28 +00:00
|
|
|
// eslint-disable-next-line react/prop-types
|
2020-10-05 13:09:35 +00:00
|
|
|
const Icon = ({ type }) =>
|
|
|
|
type === "success" ? (
|
|
|
|
<Icons.CheckToastIcon color="#333333" isfill={true} />
|
|
|
|
) : type === "error" || type === "warning" ? (
|
|
|
|
<Icons.DangerToastIcon color="#333333" isfill={true} />
|
|
|
|
) : (
|
|
|
|
<Icons.InfoToastIcon color="#333333" isfill={true} />
|
|
|
|
);
|
2019-07-17 11:20:02 +00:00
|
|
|
|
2020-09-25 06:01:05 +00:00
|
|
|
const IconWrapper = styled.div`
|
|
|
|
align-self: end;
|
|
|
|
display: flex;
|
2020-10-05 13:09:35 +00:00
|
|
|
svg {
|
2020-09-25 06:01:05 +00:00
|
|
|
width: 16px;
|
|
|
|
min-width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
min-height: 16px;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-07-17 11:20:02 +00:00
|
|
|
const StyledDiv = styled.div`
|
2020-09-25 06:42:56 +00:00
|
|
|
margin: 0 15px;
|
|
|
|
|
2020-10-05 13:09:35 +00:00
|
|
|
.toast-title {
|
2020-09-25 06:42:56 +00:00
|
|
|
font-weight: 600;
|
|
|
|
margin: 0;
|
|
|
|
margin-bottom: 5px;
|
|
|
|
line-height: 16px;
|
2020-10-05 13:09:35 +00:00
|
|
|
color: #000000;
|
|
|
|
font-size: 12px;
|
2020-09-25 06:42:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:09:35 +00:00
|
|
|
.toast-text {
|
2020-09-25 06:42:56 +00:00
|
|
|
line-height: 1.3;
|
|
|
|
align-self: center;
|
2020-10-05 13:09:35 +00:00
|
|
|
font-size: 12px;
|
|
|
|
color: #333333;
|
2020-09-25 06:42:56 +00:00
|
|
|
}
|
2020-09-25 06:01:05 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledCloseWrapper = styled.div`
|
2020-10-05 13:09:35 +00:00
|
|
|
.closeButton {
|
2020-09-25 06:01:05 +00:00
|
|
|
opacity: 0.5;
|
|
|
|
padding-top: 2px;
|
2020-10-05 13:09:35 +00:00
|
|
|
&:hover {
|
2020-09-25 06:01:05 +00:00
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 11:20:02 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const toastr = {
|
|
|
|
clear: clear,
|
|
|
|
error: error,
|
|
|
|
info: info,
|
|
|
|
success: success,
|
|
|
|
warning: warning
|
|
|
|
};
|
|
|
|
|
2020-10-05 13:09:35 +00:00
|
|
|
const CloseButton = ({ closeToast }) => (
|
2020-09-25 06:01:05 +00:00
|
|
|
<StyledCloseWrapper>
|
|
|
|
<IconButton
|
|
|
|
className="closeButton"
|
|
|
|
onClick={closeToast}
|
|
|
|
iconName="CrossIcon"
|
|
|
|
size={12}
|
|
|
|
color="#333333"
|
|
|
|
/>
|
|
|
|
</StyledCloseWrapper>
|
2020-10-05 13:09:35 +00:00
|
|
|
);
|
2019-07-17 11:20:02 +00:00
|
|
|
|
2020-10-05 13:09:35 +00:00
|
|
|
const notify = (
|
|
|
|
type,
|
|
|
|
data,
|
|
|
|
title,
|
|
|
|
timeout = 5000,
|
|
|
|
withCross = false,
|
|
|
|
centerPosition = false
|
|
|
|
) => {
|
2019-07-17 11:20:02 +00:00
|
|
|
return toast(
|
|
|
|
<>
|
2020-09-25 06:01:05 +00:00
|
|
|
<IconWrapper>
|
|
|
|
<Icon size="medium" type={type} />
|
|
|
|
</IconWrapper>
|
2019-07-17 11:20:02 +00:00
|
|
|
<StyledDiv>
|
2020-09-25 06:42:56 +00:00
|
|
|
{title && <Text className="toast-title">{title}</Text>}
|
|
|
|
{data && <Text className="toast-text">{data}</Text>}
|
2019-07-17 11:20:02 +00:00
|
|
|
</StyledDiv>
|
|
|
|
</>,
|
|
|
|
{
|
|
|
|
type: type,
|
2019-07-18 15:26:34 +00:00
|
|
|
closeOnClick: !withCross,
|
2020-10-05 13:09:35 +00:00
|
|
|
closeButton: withCross && <CloseButton />,
|
|
|
|
autoClose: timeout === 0 ? false : timeout < 750 ? 5000 : timeout || 5000,
|
2019-07-18 14:42:30 +00:00
|
|
|
position: centerPosition && toast.POSITION.TOP_CENTER
|
2019-07-17 11:20:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-24 08:19:09 +00:00
|
|
|
function success(data, title, timeout, withCross, centerPosition) {
|
2020-10-05 13:09:35 +00:00
|
|
|
return notify("success", data, title, timeout, withCross, centerPosition);
|
2019-07-17 11:20:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:19:09 +00:00
|
|
|
function error(data, title, timeout, withCross, centerPosition) {
|
2020-10-05 13:09:35 +00:00
|
|
|
const dataType = typeof data;
|
|
|
|
const message =
|
|
|
|
dataType === "string"
|
|
|
|
? data
|
|
|
|
: dataType === "object" && data.statusText
|
|
|
|
? data.statusText
|
|
|
|
: "";
|
|
|
|
|
|
|
|
return notify("error", message, title, timeout, withCross, centerPosition);
|
2019-07-17 11:20:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:19:09 +00:00
|
|
|
function warning(data, title, timeout, withCross, centerPosition) {
|
2020-10-05 13:09:35 +00:00
|
|
|
return notify("warning", data, title, timeout, withCross, centerPosition);
|
2019-07-17 11:20:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:19:09 +00:00
|
|
|
function info(data, title, timeout, withCross, centerPosition) {
|
2020-10-05 13:09:35 +00:00
|
|
|
return notify("info", data, title, timeout, withCross, centerPosition);
|
2019-07-17 11:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
return toast.dismiss();
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:09:35 +00:00
|
|
|
export default toastr;
|