2022-04-24 10:39:11 +02:00
|
|
|
import { FC, PropsWithChildren } from 'react';
|
2020-12-21 09:22:13 +01:00
|
|
|
import { Card, Row } from 'reactstrap';
|
2020-03-05 08:41:55 +01:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { faCircleNotch as preloader } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
|
2020-08-31 18:38:27 +02:00
|
|
|
type MessageType = 'default' | 'error';
|
|
|
|
|
|
|
|
const getClassForType = (type: MessageType) => {
|
|
|
|
const map: Record<MessageType, string> = {
|
2020-03-08 11:35:06 +01:00
|
|
|
error: 'border-danger',
|
2020-08-31 18:38:27 +02:00
|
|
|
default: '',
|
2020-03-08 11:16:57 +01:00
|
|
|
};
|
|
|
|
|
2020-08-31 18:38:27 +02:00
|
|
|
return map[type];
|
2020-03-08 11:16:57 +01:00
|
|
|
};
|
2020-08-31 18:38:27 +02:00
|
|
|
const getTextClassForType = (type: MessageType) => {
|
|
|
|
const map: Record<MessageType, string> = {
|
2020-03-08 11:35:06 +01:00
|
|
|
error: 'text-danger',
|
2020-08-31 18:38:27 +02:00
|
|
|
default: 'text-muted',
|
2020-03-08 11:16:57 +01:00
|
|
|
};
|
|
|
|
|
2020-08-31 18:38:27 +02:00
|
|
|
return map[type];
|
2020-03-08 11:16:57 +01:00
|
|
|
};
|
|
|
|
|
2022-04-24 10:39:11 +02:00
|
|
|
export type MessageProps = PropsWithChildren<{
|
2020-12-21 09:22:13 +01:00
|
|
|
className?: string;
|
2020-08-31 18:38:27 +02:00
|
|
|
loading?: boolean;
|
2020-12-12 12:04:20 +01:00
|
|
|
fullWidth?: boolean;
|
2020-08-31 18:38:27 +02:00
|
|
|
type?: MessageType;
|
2022-04-24 10:39:11 +02:00
|
|
|
}>;
|
2020-03-05 08:41:55 +01:00
|
|
|
|
2022-05-28 11:16:59 +02:00
|
|
|
export const Message: FC<MessageProps> = (
|
|
|
|
{ className, children, loading = false, type = 'default', fullWidth = false },
|
|
|
|
) => {
|
2020-12-12 12:04:20 +01:00
|
|
|
const classes = classNames({
|
|
|
|
'col-md-12': fullWidth,
|
|
|
|
'col-md-10 offset-md-1': !fullWidth,
|
|
|
|
});
|
2020-03-05 08:41:55 +01:00
|
|
|
|
|
|
|
return (
|
2022-03-11 16:37:41 +01:00
|
|
|
<Row className={classNames('g-0', className)}>
|
2020-12-21 09:22:13 +01:00
|
|
|
<div className={classes}>
|
|
|
|
<Card className={getClassForType(type)} body>
|
|
|
|
<h3 className={classNames('text-center mb-0', getTextClassForType(type))}>
|
|
|
|
{loading && <FontAwesomeIcon icon={preloader} spin />}
|
2022-03-05 13:26:28 +01:00
|
|
|
{loading && <span className="ms-2">{children ?? 'Loading...'}</span>}
|
2020-12-21 09:22:13 +01:00
|
|
|
{!loading && children}
|
|
|
|
</h3>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</Row>
|
2020-03-05 08:41:55 +01:00
|
|
|
);
|
|
|
|
};
|