Skip to content

Commit

Permalink
Show errors in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr committed Jul 31, 2020
1 parent aa6b0c9 commit 35ed20a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/gatsby-admin/src/components/manage-plugin-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TextAreaField,
TextAreaFieldLabel,
TextAreaFieldControl,
Text,
} from "gatsby-interface"

interface IProps {
Expand Down Expand Up @@ -67,6 +68,7 @@ export default function ManagePluginModal({
const [options, setOptions] = useState(() =>
JSON.stringify(plugin.options || {}, null, 2)
)
const [error, setError] = useState<Error | null>(null)

return (
<Modal isOpen={isOpen} onDismiss={onDismiss}>
Expand All @@ -77,6 +79,7 @@ export default function ManagePluginModal({
<form
onSubmit={(evt: React.FormEvent): void => {
evt.preventDefault()
setError(null)
let json
try {
// NOTE(@mxstbr): I use eval() to support JS object notation (`{ bla: true }`) and
Expand All @@ -85,11 +88,14 @@ export default function ManagePluginModal({
// Validate that options isn't any JavaScript but an object
json = JSON.parse(JSON.stringify(js))
} catch (err) {
setError(err)
return
}
updateGatsbyPlugin({
name: plugin.name,
options: json,
}).catch(err => {
setError(err)
})
}}
>
Expand All @@ -102,6 +108,11 @@ export default function ManagePluginModal({
value={options}
onChange={(evt): void => setOptions(evt.target.value)}
/>
{error !== null && (
<Text sx={{ color: `red.60`, fontSize: 0 }}>
{error.message}
</Text>
)}
</TextAreaField>
</div>
<StyledModalActions>
Expand Down

0 comments on commit 35ed20a

Please sign in to comment.