2020-08-31 19:38:27 +03:00
|
|
|
import React, { FC } from 'react';
|
2020-03-05 10:41:55 +03:00
|
|
|
import { Card } from 'reactstrap';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { faCircleNotch as preloader } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
type MessageType = 'default' | 'error';
|
|
|
|
|
|
|
|
const getClassForType = (type: MessageType) => {
|
|
|
|
const map: Record<MessageType, string> = {
|
2020-03-08 13:35:06 +03:00
|
|
|
error: 'border-danger',
|
2020-08-31 19:38:27 +03:00
|
|
|
default: '',
|
2020-03-08 13:16:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
return map[type];
|
2020-03-08 13:16:57 +03:00
|
|
|
};
|
2020-08-31 19:38:27 +03:00
|
|
|
const getTextClassForType = (type: MessageType) => {
|
|
|
|
const map: Record<MessageType, string> = {
|
2020-03-08 13:35:06 +03:00
|
|
|
error: 'text-danger',
|
2020-08-31 19:38:27 +03:00
|
|
|
default: 'text-muted',
|
2020-03-08 13:16:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
return map[type];
|
2020-03-08 13:16:57 +03:00
|
|
|
};
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
interface MessageProps {
|
|
|
|
noMargin?: boolean;
|
|
|
|
loading?: boolean;
|
|
|
|
type?: MessageType;
|
|
|
|
}
|
2020-03-05 10:41:55 +03:00
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
const Message: FC<MessageProps> = ({ children, loading = false, noMargin = false, type = 'default' }) => {
|
2020-03-08 13:35:06 +03:00
|
|
|
const cardClasses = classNames('bg-light', getClassForType(type), { 'mt-4': !noMargin });
|
2020-03-05 10:41:55 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="col-md-10 offset-md-1">
|
|
|
|
<Card className={cardClasses} body>
|
2020-03-08 13:16:57 +03:00
|
|
|
<h3 className={classNames('text-center mb-0', getTextClassForType(type))}>
|
2020-03-05 10:41:55 +03:00
|
|
|
{loading && <FontAwesomeIcon icon={preloader} spin />}
|
2020-08-31 19:38:27 +03:00
|
|
|
{loading && <span className="ml-2">{children ?? 'Loading...'}</span>}
|
2020-04-04 13:58:04 +03:00
|
|
|
{!loading && children}
|
2020-03-05 10:41:55 +03:00
|
|
|
</h3>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-08 13:16:57 +03:00
|
|
|
export default Message;
|