|
1 | 1 | /* eslint react/no-array-index-key: 0 */
|
2 |
| -import React from 'react' |
| 2 | +import React, { useState } from 'react' |
3 | 3 | import PropTypes from 'prop-types'
|
4 | 4 | import { Alert, Button, Container } from 'react-bootstrap'
|
5 | 5 |
|
6 |
| -const Error = ({ errors, showBackButton, router }) => { |
7 |
| - const { errorTitle, errorText, variant} = errors |
| 6 | +const Error = ({ errors, showBackButton, canDismissAlert, router }) => { |
| 7 | + const { errorTitle, errorText, variant } = errors |
| 8 | + const [show, setShow] = useState(true) |
8 | 9 | return (
|
9 |
| - <Container> |
10 |
| - <Alert className='my-5 text-break' variant={variant}> |
11 |
| - {errorTitle && ( |
12 |
| - <Alert.Heading>{errorTitle}</Alert.Heading> |
13 |
| - )} |
14 |
| - {errorText && (errorText)} |
15 |
| - {showBackButton && ( |
16 |
| - <> |
17 |
| - <hr /> |
18 |
| - <div className='d-flex justify-content-end'> |
19 |
| - <Button onClick={() => router.back()} variant={`outline-${variant}`}> |
20 |
| - Click to return to the previous page. |
21 |
| - </Button> |
22 |
| - </div> |
23 |
| - </> |
24 |
| - )} |
25 |
| - </Alert> |
26 |
| - </Container> |
| 10 | + show && ( |
| 11 | + <Container> |
| 12 | + <Alert className='my-5 text-break' variant={variant} onClose={() => setShow(false)} dismissible={canDismissAlert}> |
| 13 | + {errorTitle && ( |
| 14 | + <Alert.Heading>{errorTitle}</Alert.Heading> |
| 15 | + )} |
| 16 | + {errorText.map((errorMessage, index) => ( |
| 17 | + <p key={index}>{errorMessage}</p> |
| 18 | + ))} |
| 19 | + {showBackButton && ( |
| 20 | + <> |
| 21 | + <hr /> |
| 22 | + <div className='d-flex justify-content-end'> |
| 23 | + <Button onClick={() => router.back()} variant={`outline-${variant}`}> |
| 24 | + Click to return to the previous page. |
| 25 | + </Button> |
| 26 | + </div> |
| 27 | + </> |
| 28 | + )} |
| 29 | + </Alert> |
| 30 | + </Container> |
| 31 | + ) |
27 | 32 | )
|
28 | 33 | }
|
29 | 34 |
|
30 | 35 | Error.propTypes = {
|
31 |
| - errorText: PropTypes.string.isRequired, |
32 |
| - errorTitle: PropTypes.string, |
33 |
| - router: PropTypes.shape({}).isRequired, |
| 36 | + canDismissAlert: PropTypes.bool, |
| 37 | + errors: PropTypes.shape({ |
| 38 | + errorText: PropTypes.string.isRequired, |
| 39 | + errorTitle: PropTypes.string, |
| 40 | + variant: PropTypes.string.isRequired, |
| 41 | + }).isRequired, |
34 | 42 | showBackButton: PropTypes.bool,
|
35 |
| - variant: PropTypes.string.isRequired, |
| 43 | + router: PropTypes.shape({}).isRequired, |
36 | 44 | }
|
37 | 45 |
|
38 | 46 | Error.defaultProps = {
|
39 |
| - errorTitle: '', |
| 47 | + canDismissAlert: false, |
40 | 48 | showBackButton: true,
|
41 | 49 | }
|
42 | 50 |
|
|
0 commit comments