refactor: getErrorMsg() to pure functions
This commit is contained in:
parent
26a051ff07
commit
86a245f6ba
4 changed files with 64 additions and 47 deletions
|
|
@ -34,20 +34,20 @@ const initialValues = {
|
||||||
rememberMe: false
|
rememberMe: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getErrorMsg = (formikErrors, formikTouched, mutationError) => {
|
||||||
|
if (!formikErrors || !formikTouched) return null
|
||||||
|
if (mutationError) return 'Invalid login/password combination'
|
||||||
|
if (formikErrors.client && formikTouched.client) return formikErrors.client
|
||||||
|
if (formikErrors.password && formikTouched.password)
|
||||||
|
return formikErrors.password
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const LoginState = ({ state, dispatch }) => {
|
const LoginState = ({ state, dispatch }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
const [login, { error: mutationError }] = useMutation(LOGIN)
|
const [login, { error: mutationError }] = useMutation(LOGIN)
|
||||||
|
|
||||||
const getErrorMsg = (formikErrors, formikTouched) => {
|
|
||||||
if (!formikErrors || !formikTouched) return null
|
|
||||||
if (mutationError) return 'Invalid login/password combination'
|
|
||||||
if (formikErrors.client && formikTouched.client) return formikErrors.client
|
|
||||||
if (formikErrors.password && formikTouched.password)
|
|
||||||
return formikErrors.password
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const submitLogin = async (username, password, rememberMe) => {
|
const submitLogin = async (username, password, rememberMe) => {
|
||||||
const { data: loginResponse } = await login({
|
const { data: loginResponse } = await login({
|
||||||
variables: {
|
variables: {
|
||||||
|
|
@ -88,7 +88,7 @@ const LoginState = ({ state, dispatch }) => {
|
||||||
fullWidth
|
fullWidth
|
||||||
autoFocus
|
autoFocus
|
||||||
className={classes.input}
|
className={classes.input}
|
||||||
error={getErrorMsg(errors, touched)}
|
error={getErrorMsg(errors, touched, mutationError)}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
name="password"
|
name="password"
|
||||||
|
|
@ -96,7 +96,7 @@ const LoginState = ({ state, dispatch }) => {
|
||||||
component={SecretInput}
|
component={SecretInput}
|
||||||
label="Password"
|
label="Password"
|
||||||
fullWidth
|
fullWidth
|
||||||
error={getErrorMsg(errors, touched)}
|
error={getErrorMsg(errors, touched, mutationError)}
|
||||||
/>
|
/>
|
||||||
<div className={classes.rememberMeWrapper}>
|
<div className={classes.rememberMeWrapper}>
|
||||||
<Field
|
<Field
|
||||||
|
|
@ -107,9 +107,9 @@ const LoginState = ({ state, dispatch }) => {
|
||||||
<Label3>Keep me logged in</Label3>
|
<Label3>Keep me logged in</Label3>
|
||||||
</div>
|
</div>
|
||||||
<div className={classes.footer}>
|
<div className={classes.footer}>
|
||||||
{getErrorMsg(errors, touched) && (
|
{getErrorMsg(errors, touched, mutationError) && (
|
||||||
<P className={classes.errorMessage}>
|
<P className={classes.errorMessage}>
|
||||||
{getErrorMsg(errors, touched)}
|
{getErrorMsg(errors, touched, mutationError)}
|
||||||
</P>
|
</P>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,21 @@ const reducer = (state, action) => {
|
||||||
return { ...state, ...payload, result: type }
|
return { ...state, ...payload, result: type }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getErrorMsg = (
|
||||||
|
formikErrors,
|
||||||
|
formikTouched,
|
||||||
|
queryError,
|
||||||
|
mutationError
|
||||||
|
) => {
|
||||||
|
if (!formikErrors || !formikTouched) return null
|
||||||
|
if (queryError || mutationError) return 'Internal server error'
|
||||||
|
if (formikErrors.password && formikTouched.password)
|
||||||
|
return formikErrors.password
|
||||||
|
if (formikErrors.confirmPassword && formikTouched.confirmPassword)
|
||||||
|
return formikErrors.confirmPassword
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const Register = () => {
|
const Register = () => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
const history = useHistory()
|
const history = useHistory()
|
||||||
|
|
@ -107,16 +122,6 @@ const Register = () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const getErrorMsg = (formikErrors, formikTouched) => {
|
|
||||||
if (!formikErrors || !formikTouched) return null
|
|
||||||
if (queryError || mutationError) return 'Internal server error'
|
|
||||||
if (formikErrors.password && formikTouched.password)
|
|
||||||
return formikErrors.password
|
|
||||||
if (formikErrors.confirmPassword && formikTouched.confirmPassword)
|
|
||||||
return formikErrors.confirmPassword
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid
|
<Grid
|
||||||
container
|
container
|
||||||
|
|
@ -166,9 +171,19 @@ const Register = () => {
|
||||||
fullWidth
|
fullWidth
|
||||||
/>
|
/>
|
||||||
<div className={classes.footer}>
|
<div className={classes.footer}>
|
||||||
{getErrorMsg(errors, touched) && (
|
{getErrorMsg(
|
||||||
|
errors,
|
||||||
|
touched,
|
||||||
|
queryError,
|
||||||
|
mutationError
|
||||||
|
) && (
|
||||||
<P className={classes.errorMessage}>
|
<P className={classes.errorMessage}>
|
||||||
{getErrorMsg(errors, touched)}
|
{getErrorMsg(
|
||||||
|
errors,
|
||||||
|
touched,
|
||||||
|
queryError,
|
||||||
|
mutationError
|
||||||
|
)}
|
||||||
</P>
|
</P>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,16 @@ const initialValues = {
|
||||||
confirmPassword: ''
|
confirmPassword: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getErrorMsg = (formikErrors, formikTouched, mutationError) => {
|
||||||
|
if (!formikErrors || !formikTouched) return null
|
||||||
|
if (mutationError) return 'Internal server error'
|
||||||
|
if (formikErrors.password && formikTouched.password)
|
||||||
|
return formikErrors.password
|
||||||
|
if (formikErrors.confirmPassword && formikTouched.confirmPassword)
|
||||||
|
return formikErrors.confirmPassword
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const ResetPassword = () => {
|
const ResetPassword = () => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
const history = useHistory()
|
const history = useHistory()
|
||||||
|
|
@ -81,16 +91,6 @@ const ResetPassword = () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const getErrorMsg = (formikErrors, formikTouched) => {
|
|
||||||
if (!formikErrors || !formikTouched) return null
|
|
||||||
if (error) return 'Internal server error'
|
|
||||||
if (formikErrors.password && formikTouched.password)
|
|
||||||
return formikErrors.password
|
|
||||||
if (formikErrors.confirmPassword && formikTouched.confirmPassword)
|
|
||||||
return formikErrors.confirmPassword
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid
|
<Grid
|
||||||
container
|
container
|
||||||
|
|
@ -139,9 +139,9 @@ const ResetPassword = () => {
|
||||||
fullWidth
|
fullWidth
|
||||||
/>
|
/>
|
||||||
<div className={classes.footer}>
|
<div className={classes.footer}>
|
||||||
{getErrorMsg(errors, touched) && (
|
{getErrorMsg(errors, touched, error) && (
|
||||||
<P className={classes.errorMessage}>
|
<P className={classes.errorMessage}>
|
||||||
{getErrorMsg(errors, touched)}
|
{getErrorMsg(errors, touched, error)}
|
||||||
</P>
|
</P>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,14 @@ const radioOptions = [
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const getErrorMsg = (formikErrors, formikTouched, mutationError) => {
|
||||||
|
if (!formikErrors || !formikTouched) return null
|
||||||
|
if (mutationError) return 'Internal server error'
|
||||||
|
if (formikErrors.username && formikTouched.username)
|
||||||
|
return formikErrors.username
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const CreateUserModal = ({ state, dispatch }) => {
|
const CreateUserModal = ({ state, dispatch }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
|
|
@ -74,14 +82,6 @@ const CreateUserModal = ({ state, dispatch }) => {
|
||||||
[classes.error]: formikErrors.role && formikTouched.role
|
[classes.error]: formikErrors.role && formikTouched.role
|
||||||
})
|
})
|
||||||
|
|
||||||
const getErrorMsg = (formikErrors, formikTouched) => {
|
|
||||||
if (!formikErrors || !formikTouched) return null
|
|
||||||
if (error) return 'Internal server error'
|
|
||||||
if (formikErrors.username && formikTouched.username)
|
|
||||||
return formikErrors.username
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{state.showCreateUserModal && !createUserURL && (
|
{state.showCreateUserModal && !createUserURL && (
|
||||||
|
|
@ -125,8 +125,10 @@ const CreateUserModal = ({ state, dispatch }) => {
|
||||||
options={radioOptions}
|
options={radioOptions}
|
||||||
/>
|
/>
|
||||||
<div className={classes.footer}>
|
<div className={classes.footer}>
|
||||||
{getErrorMsg(errors, touched) && (
|
{getErrorMsg(errors, touched, error) && (
|
||||||
<ErrorMessage>{getErrorMsg(errors, touched)}</ErrorMessage>
|
<ErrorMessage>
|
||||||
|
{getErrorMsg(errors, touched, error)}
|
||||||
|
</ErrorMessage>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue