Skip to content

Commit 4aaf2dc

Browse files
authored
Merge pull request #122 from scientist-softserv/25-error-component
Error component
2 parents f1d43f1 + 8fdd24e commit 4aaf2dc

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/components/Error/Error.jsx

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

src/components/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AdditionalInfo from './AdditionalInfo/AdditionalInfo'
77
import BlankRequestForm from './BlankRequestForm/BlankRequestForm'
88
import Button from './Button/Button'
99
import CollapsibleSection from './CollapsibleSection/CollapsibleSection'
10+
import Error from './Error/Error'
1011
import Image from './Image/Image'
1112
import Link from './Link/Link'
1213
import Loading from './Loading/Loading'
@@ -22,6 +23,7 @@ export {
2223
BlankRequestForm,
2324
Button,
2425
CollapsibleSection,
26+
Error,
2527
Image,
2628
Link,
2729
Loading,

src/compounds/ItemGroup/ItemGroup.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const ItemGroup = ({ buttonProps, items, isLoading, orientation, withButtonLink,
2929
)}
3030
</>
3131
) : (
32-
items.map((item) => (
32+
items?.map((item) => (
3333
<Col key={item.id}>
3434
<Item
3535
buttonProps={buttonProps}

0 commit comments

Comments
 (0)