Skip to content

Commit 9b186c7

Browse files
authored
Merge pull request #137 from scientist-softserv/136-refactor-error
136 refactor error
2 parents 32e3ea6 + 3f7d5e7 commit 9b186c7

File tree

5 files changed

+143
-83
lines changed

5 files changed

+143
-83
lines changed

src/components/Error/Error.jsx

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/components/Error/Error.stories.jsx

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/components/Notice/Notice.jsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 Notice = ({ alert, buttonProps, dismissible, withBackButton }) => {
7+
const [show, setShow] = useState(true)
8+
const { title, body, variant } = alert
9+
let onClick
10+
let text
11+
if (withBackButton) ({ onClick, text } = buttonProps)
12+
13+
return (
14+
show && (
15+
<Container>
16+
<Alert className='my-5 text-break' variant={variant} onClose={() => setShow(false)} dismissible={dismissible}>
17+
{title && (
18+
<Alert.Heading>{title}</Alert.Heading>
19+
)}
20+
{body.map((message, index) => {
21+
// If "message" is a JSON string, return it as a formatted code block
22+
// Otherwise, return it as a regular string
23+
const isJSONParsable = () => {
24+
try {
25+
JSON.parse(message)
26+
return true
27+
} catch (error) {
28+
return false
29+
}
30+
}
31+
const isJavascriptObject = isJSONParsable(message)
32+
33+
return (
34+
isJavascriptObject ? (
35+
<pre className='mb-0' key={index}>{message}</pre>
36+
) : (
37+
<p className='mb-0' key={index}>{message}</p>
38+
)
39+
)
40+
})}
41+
{withBackButton && (
42+
<>
43+
<hr />
44+
<div className='d-flex justify-content-end'>
45+
<Button onClick={onClick} variant={`outline-${variant}`}>
46+
{text}
47+
</Button>
48+
</div>
49+
</>
50+
)}
51+
</Alert>
52+
</Container>
53+
)
54+
)
55+
}
56+
57+
Notice.propTypes = {
58+
alert: PropTypes.shape({
59+
body: PropTypes.arrayOf(PropTypes.string).isRequired,
60+
title: PropTypes.string,
61+
variant: PropTypes.string.isRequired,
62+
}).isRequired,
63+
buttonProps: PropTypes.shape({
64+
onClick: PropTypes.func,
65+
text: PropTypes.string,
66+
}),
67+
dismissible: PropTypes.bool,
68+
withBackButton: PropTypes.bool,
69+
}
70+
71+
Notice.defaultProps = {
72+
buttonProps: {},
73+
dismissible: true,
74+
withBackButton: false,
75+
}
76+
77+
export default Notice
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react'
2+
import Notice from './Notice'
3+
4+
export default {
5+
title: 'Components/Notice',
6+
component: Notice,
7+
}
8+
9+
const Template = (args) => <Notice {...args} />
10+
11+
export const Default = Template.bind({})
12+
Default.args = {
13+
alert: {
14+
title: '',
15+
body: ['A standard alert.'],
16+
variant: 'warning',
17+
},
18+
buttonProps: {},
19+
dismissible: true,
20+
withBackButton: false,
21+
}
22+
23+
export const Error = Template.bind({})
24+
Error.args = {
25+
alert: {
26+
title: "We're sorry, something went wrong.",
27+
body: [
28+
JSON.stringify({
29+
message: 'There are instances where there may be several api errors on a single page. We would render them all.',
30+
name: 'First',
31+
status: 422,
32+
}, null, 2),
33+
JSON.stringify({
34+
message: 'This is a second error message.',
35+
name: 'Second',
36+
status: 422,
37+
}, null, 2),
38+
JSON.stringify({
39+
message: 'This is a third error message.',
40+
name: 'Third',
41+
status: 422,
42+
}, null, 2),
43+
],
44+
variant: 'warning',
45+
},
46+
buttonProps: {
47+
onClick: () => console.log('click me!'),
48+
text: 'Click to return to the previous page.',
49+
},
50+
dismissible: false,
51+
withBackButton: true,
52+
}
53+
54+
export const UnauthorizedUser = Template.bind({})
55+
UnauthorizedUser.args = {
56+
alert: {
57+
title: 'Unauthorized',
58+
body: ['Please sign in to access this page.'],
59+
variant: 'info',
60+
},
61+
buttonProps: {},
62+
dismissible: false,
63+
withBackButton: false,
64+
}

src/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ 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'
1110
import Image from './Image/Image'
1211
import Link from './Link/Link'
1312
import Loading from './Loading/Loading'
13+
import Notice from './Notice/Notice'
1414
import SearchBar from './SearchBar/SearchBar'
1515
import ShippingDetails from './ShippingDetails/ShippingDetails'
1616
import SocialIcon from './SocialIcon/SocialIcon'
@@ -23,10 +23,10 @@ export {
2323
BlankRequestForm,
2424
Button,
2525
CollapsibleSection,
26-
Error,
2726
Image,
2827
Link,
2928
Loading,
29+
Notice,
3030
SearchBar,
3131
ShippingDetails,
3232
SocialIcon,

0 commit comments

Comments
 (0)