-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { SnackbarProvider } from 'notistack' | ||
|
||
export const NotificationProvider = ({ children }) => { | ||
return ( | ||
<SnackbarProvider | ||
autoHideDuration={NOTIFICATION_TIMEOUT_MS} | ||
maxSnack={NOTIFICATION_MAX_VISIBLE} | ||
anchorOrigin={{ | ||
vertical: NOTIFICATION_POSITION_VERTICAL, | ||
horizontal: NOTIFICATION_POSITION_HORIZONTAL, | ||
}}> | ||
{children} | ||
</SnackbarProvider> | ||
) | ||
} | ||
|
||
const NOTIFICATION_TIMEOUT_MS = 4 * 1000 // 4 seconds | ||
|
||
/** | ||
* How many notifications will be visible at the same time. | ||
* @constant | ||
* @type {number} | ||
*/ | ||
const NOTIFICATION_MAX_VISIBLE = 3 | ||
|
||
/** | ||
* Notifications vertical anchor origin. | ||
* @constant | ||
* @type {string} | ||
*/ | ||
const NOTIFICATION_POSITION_VERTICAL = 'top' | ||
|
||
/** | ||
* Notifications horizontal anchor origin. | ||
* @constant | ||
* @type {string} | ||
*/ | ||
const NOTIFICATION_POSITION_HORIZONTAL = 'center' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useFormContext } from 'react-hook-form' | ||
import { Button as MuiButton } from '@material-ui/core' | ||
|
||
export const Button = ({ | ||
children = 'Submit', | ||
variant = 'outlined', | ||
fullWidth = true, | ||
...props | ||
}) => { | ||
const { | ||
formState: { isSubmitting }, | ||
} = useFormContext() | ||
|
||
return ( | ||
<MuiButton | ||
{...props} | ||
disabled={isSubmitting} | ||
variant={variant} | ||
fullWidth={fullWidth}> | ||
{children} | ||
</MuiButton> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
FormProvider as ReactHookFormProvider, | ||
useFormContext, | ||
} from 'react-hook-form' | ||
import { DevTool } from '@hookform/devtools' | ||
|
||
export const FormProvider = ({ children, ...other }) => ( | ||
<ReactHookFormProvider {...other}> | ||
{children} | ||
<DevTools /> | ||
</ReactHookFormProvider> | ||
) | ||
|
||
const DevTools = () => { | ||
const { control } = useFormContext() | ||
return <DevTool control={control} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useFormContext, Controller } from 'react-hook-form' | ||
import { TextField } from '@material-ui/core' | ||
import { path } from 'ramda' | ||
|
||
export const Input = ({ name, label, defaultValue = '' }) => { | ||
const { | ||
register, | ||
formState: { isSubmitting }, | ||
errors, | ||
control, | ||
} = useFormContext() | ||
|
||
const disabled = isSubmitting | ||
const helperText = path([name, 'message'], errors) | ||
const error = Boolean(helperText) | ||
|
||
return ( | ||
<Controller | ||
as={ | ||
<TextField | ||
name={name} | ||
label={label} | ||
ref={register} | ||
disabled={disabled} | ||
helperText={helperText} | ||
error={error} | ||
variant='filled' | ||
fullWidth | ||
/> | ||
} | ||
name={name} | ||
control={control} | ||
defaultValue={defaultValue} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { Button } from './Button' | ||
export { Input } from './Input' | ||
export { FormProvider } from './FormProvider' | ||
export { useForm } from './useForm' | ||
export { setAsyncError } from './setAsyncError' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { __, compose, forEach, identity, ifElse, includes, path } from 'ramda' | ||
import { BAD_REQUEST, UNPROCESSABLE_ENTITY } from 'http-status' | ||
|
||
/** | ||
* A function that receives a `XMLHttpRequest` error object and sets the form | ||
* errors according to the error type. | ||
* | ||
* @callback SetErrorCallback | ||
* @param {Object} error - the raw `XMLHttpRequest` error object. | ||
*/ | ||
|
||
/** | ||
* Received | ||
* @param {Function} setErrorCallback - The current form `setError` function (returned from `useForm`). | ||
* @param {Function} setFormWideErrorCallback - The current form `setFormWideError` function (returned from `useForm`). | ||
* @returns {SetErrorCallback} callback | ||
*/ | ||
export const setAsyncError = ( | ||
setErrorCallback, | ||
setFormWideErrorCallback = identity | ||
) => | ||
ifElse( | ||
isValidationError, | ||
setValidationErrors(setErrorCallback), | ||
setFormWideError(setFormWideErrorCallback) | ||
) | ||
|
||
/** | ||
* Checks if the error type equals to "validation", which comprehends HTTP 400 (Bad Request) | ||
* and HTTP 422 (Unprocessable Entity). | ||
*/ | ||
const isValidationError = compose( | ||
includes(__, [BAD_REQUEST, UNPROCESSABLE_ENTITY]), | ||
path(['response', 'status']) | ||
) | ||
|
||
const setValidationErrors = setErrorCallback => | ||
compose( | ||
forEach(({ name, message }) => | ||
setErrorCallback(name, { type: 'manual', message }) | ||
), | ||
path(['response', 'data', 'errors']) | ||
) | ||
|
||
const setFormWideError = setFormWideErrorCallback => | ||
compose(setFormWideErrorCallback, path(['response', 'data', 'message'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useCallback } from 'react' | ||
import { useState } from 'react' | ||
import { useForm as useRHForm } from 'react-hook-form' | ||
|
||
export const useForm = (...args) => { | ||
const [formWideError, setFormWideError] = useState('') | ||
const { handleSubmit: RHFhandleSubmit, ...formProps } = useRHForm(...args) | ||
|
||
/** | ||
* We need this custom submit handler because, typically, we will use a | ||
* `mutateAsync` function from `react-query` to submit a form. These functions | ||
* will throw any errors (422, 500, for example). Since the error is already | ||
* handled by the mutation itself, we need to prevent this error from leaking. | ||
* | ||
* The original handler is exported as `RHFhandleSubmit`. | ||
* | ||
* @see {@link https://codesandbox.io/s/blissful-ride-wxxhl?file=/src/App.js} | ||
* @see {@link https://codesandbox.io/s/30xos?file=/src/App.js} | ||
*/ | ||
const handleSubmit = useCallback( | ||
onSubmit => RHFhandleSubmit(data => onSubmit(data).catch(() => {})), | ||
[RHFhandleSubmit] | ||
) | ||
|
||
return { | ||
...formProps, | ||
formWideError, | ||
setFormWideError, | ||
handleSubmit, | ||
RHFhandleSubmit, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { yupResolver } from '@hookform/resolvers/yup' | ||
import { Grid, Collapse } from '@material-ui/core' | ||
import { Alert, AlertTitle } from '@material-ui/lab' | ||
import { useCustomMutation } from './useCustomMutation' | ||
import { schema } from './schema' | ||
import { FormProvider, useForm, Input, Button, setAsyncError } from './Form' | ||
import { useSnackbar } from 'notistack' | ||
|
||
export default () => { | ||
const { enqueueSnackbar } = useSnackbar() | ||
|
||
const { formWideError, setFormWideError, ...formProps } = useForm({ | ||
resolver: yupResolver(schema), | ||
}) | ||
|
||
const { handleSubmit, setError } = formProps | ||
|
||
const options = { | ||
onSuccess: () => { | ||
enqueueSnackbar('Created successfully.', { | ||
variant: 'success', | ||
}) | ||
}, | ||
onMutate: () => setFormWideError(''), | ||
onError: error => { | ||
enqueueSnackbar('Something went wrong.', { | ||
variant: 'error', | ||
}) | ||
setAsyncError(setError, setFormWideError)(error) | ||
}, | ||
} | ||
|
||
const { mutateAsync: created } = useCustomMutation('/created', options) | ||
|
||
const { mutateAsync: unprocessableEntity } = useCustomMutation( | ||
'/unprocessable-entity', | ||
options | ||
) | ||
|
||
const { mutateAsync: conflict } = useCustomMutation('/conflict', options) | ||
|
||
return ( | ||
<FormProvider {...formProps}> | ||
<form onSubmit={handleSubmit(created)}> | ||
<Grid container spacing={2}> | ||
<Grid item xs={12}> | ||
<h1>Final version</h1> | ||
</Grid> | ||
<Grid item xs={12}> | ||
<Collapse in={Boolean(formWideError)}> | ||
<Alert severity='error' onClose={() => setFormWideError('')}> | ||
<AlertTitle>An error occurred</AlertTitle> | ||
{formWideError} | ||
</Alert> | ||
</Collapse> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Input name='firstName' defaultValue='John' label='First name' /> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Input name='lastName' defaultValue='Doe' label='Last name' /> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<Button onClick={handleSubmit(created)}>Submit (201)</Button> | ||
<Button onClick={handleSubmit(conflict)}>Submit (409)</Button> | ||
<Button onClick={handleSubmit(unprocessableEntity)}> | ||
Submit (422) | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
</form> | ||
</FormProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as yup from 'yup' | ||
|
||
export const schema = yup.object().shape({ | ||
firstName: yup.string().required(), | ||
lastName: yup.string().required(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { instance } from 'api' | ||
import { useMutation } from 'react-query' | ||
|
||
export const useCustomMutation = (url, options = {}) => | ||
useMutation(async () => await instance.post(url), options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1bea056
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: