Skip to content

Commit a1d9c5c

Browse files
committed
refactor the error component into a more reusable alert component
1 parent 51d3447 commit a1d9c5c

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

src/components/Error/Error.jsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ import React, { useState } from 'react'
33
import PropTypes from 'prop-types'
44
import { Alert, Button, Container } from 'react-bootstrap'
55

6-
const Error = ({ errors, showBackButton, canDismissAlert, router }) => {
7-
const { errorTitle, errorText, variant } = errors
6+
const Error = ({ alert, buttonProps, dismissible, withBackButton }) => {
87
const [show, setShow] = useState(true)
8+
const { title, body, variant } = alert
9+
let onClick, text
10+
if (withBackButton) ({ onClick, text } = buttonProps)
911

1012
return (
1113
show && (
1214
<Container>
13-
<Alert className='my-5 text-break' variant={variant} onClose={() => setShow(false)} dismissible={canDismissAlert}>
14-
{errorTitle && (
15-
<Alert.Heading>{errorTitle}</Alert.Heading>
15+
<Alert className='my-5 text-break' variant={variant} onClose={() => setShow(false)} dismissible={dismissible}>
16+
{title && (
17+
<Alert.Heading>{title}</Alert.Heading>
1618
)}
17-
{errorText.map((errorMessage, index) => (
18-
<p key={index}>{errorMessage}</p>
19+
{body.map((text, index) => (
20+
<p className='mb-0' key={index}>{text}</p>
1921
))}
20-
{showBackButton && (
22+
{withBackButton && (
2123
<>
2224
<hr />
2325
<div className='d-flex justify-content-end'>
24-
<Button onClick={() => router.back()} variant={`outline-${variant}`}>
26+
{/* <Button onClick={() => router.back()} variant={`outline-${variant}`}>
2527
Click to return to the previous page.
28+
</Button> */}
29+
<Button onClick={onClick} variant={`outline-${variant}`}>
30+
{text}
2631
</Button>
2732
</div>
2833
</>
@@ -34,19 +39,25 @@ const Error = ({ errors, showBackButton, canDismissAlert, router }) => {
3439
}
3540

3641
Error.propTypes = {
37-
canDismissAlert: PropTypes.bool,
38-
errors: PropTypes.shape({
39-
errorText: PropTypes.arrayOf(PropTypes.string).isRequired,
40-
errorTitle: PropTypes.string,
42+
alert: PropTypes.shape({
43+
body: PropTypes.arrayOf(PropTypes.string).isRequired,
44+
title: PropTypes.string,
4145
variant: PropTypes.string.isRequired,
4246
}).isRequired,
43-
showBackButton: PropTypes.bool,
44-
router: PropTypes.shape({}).isRequired,
47+
buttonProps: PropTypes.shape({
48+
onClick: PropTypes.func,
49+
text: PropTypes.string,
50+
}),
51+
dismissible: PropTypes.bool,
52+
withBackButton: PropTypes.bool,
4553
}
4654

4755
Error.defaultProps = {
48-
canDismissAlert: false,
49-
showBackButton: true,
56+
buttonProps: {
57+
text: 'Click to return to the previous page.'
58+
},
59+
dismissible: true,
60+
withBackButton: false,
5061
}
5162

5263
export default Error

src/components/Error/Error.stories.jsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,41 @@ const Template = (args) => <Error {...args} />
1010

1111
export const Default = Template.bind({})
1212
Default.args = {
13-
errors: {
14-
errorTitle: 'Errors:',
15-
errorText: ['this is an error in dev. error block would go here', 'this is a second error message', 'this is a third error message'],
16-
variant: 'danger'
17-
}
13+
alert: {
14+
title: '',
15+
body: ['A standard alert.'],
16+
variant: 'warning'
17+
},
18+
buttonProps: {
19+
text: 'Click to return to the previous page.'
20+
},
21+
dismissible: true,
22+
withBackButton: false,
23+
}
24+
25+
export const WithError = Template.bind({})
26+
WithError.args = {
27+
alert: {
28+
title: "We're sorry, something went wrong.",
29+
body: ['This is how an error would present in dev. There are instances where there may be several api errors on a single page. We would render them all.', 'This is a second error message.', 'This is a third error message.'],
30+
variant: 'warning'
31+
},
32+
buttonProps: {
33+
onClick: () => console.log('click me!'),
34+
text: 'Click to return to the previous page.'
35+
},
36+
dismissible: false,
37+
withBackButton: true,
1838
}
1939

20-
export const Alternate = Template.bind({})
21-
Alternate.args = {
22-
canDismissAlert: true,
23-
errors: {
24-
errorTitle: "We're sorry, something went wrong.",
25-
errorText: ['Please refresh the page and try again. this is an error in prod'],
26-
variant: 'danger'
40+
export const UnauthorizedUser = Template.bind({})
41+
UnauthorizedUser.args = {
42+
alert: {
43+
title: 'Unauthorized',
44+
body: ['Please sign in to access this page.'],
45+
variant: 'info'
2746
},
28-
showBackButton: false,
47+
buttonProps: {},
48+
dismissible: false,
49+
withBackButton: false,
2950
}

0 commit comments

Comments
 (0)