Total Complexity | 2 |
Complexity/F | 0 |
Lines of Code | 56 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | import { FC } from 'react'; |
||
2 | import { Card } from 'reactstrap'; |
||
3 | import classNames from 'classnames'; |
||
4 | import { faCircleNotch as preloader } from '@fortawesome/free-solid-svg-icons'; |
||
5 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
||
6 | |||
7 | type MessageType = 'default' | 'error'; |
||
8 | |||
9 | 6 | const getClassForType = (type: MessageType) => { |
|
10 | 7 | const map: Record<MessageType, string> = { |
|
11 | error: 'border-danger', |
||
12 | default: '', |
||
13 | }; |
||
14 | |||
15 | 7 | return map[type]; |
|
16 | }; |
||
17 | 6 | const getTextClassForType = (type: MessageType) => { |
|
18 | 7 | const map: Record<MessageType, string> = { |
|
19 | error: 'text-danger', |
||
20 | default: 'text-muted', |
||
21 | }; |
||
22 | |||
23 | 7 | return map[type]; |
|
24 | }; |
||
25 | |||
26 | interface MessageProps { |
||
27 | noMargin?: boolean; |
||
28 | loading?: boolean; |
||
29 | fullWidth?: boolean; |
||
30 | type?: MessageType; |
||
31 | } |
||
32 | |||
33 | 6 | const Message: FC<MessageProps> = ( |
|
34 | { children, loading = false, noMargin = false, type = 'default', fullWidth = false }, |
||
35 | ) => { |
||
36 | 7 | const cardClasses = classNames(getClassForType(type), { 'mt-4': !noMargin }); |
|
37 | 7 | const classes = classNames({ |
|
38 | 'col-md-12': fullWidth, |
||
39 | 'col-md-10 offset-md-1': !fullWidth, |
||
40 | }); |
||
41 | |||
42 | 7 | return ( |
|
43 | <div className={classes}> |
||
44 | <Card className={cardClasses} body> |
||
45 | <h3 className={classNames('text-center mb-0', getTextClassForType(type))}> |
||
46 | {loading && <FontAwesomeIcon icon={preloader} spin />} |
||
47 | {loading && <span className="ml-2">{children ?? 'Loading...'}</span>} |
||
48 | {!loading && children} |
||
49 | </h3> |
||
50 | </Card> |
||
51 | </div> |
||
52 | ); |
||
53 | }; |
||
54 | |||
55 | export default Message; |
||
56 |