From fe57dd2d2ffc952dd669d7c4d5b76d2c2aa878a0 Mon Sep 17 00:00:00 2001 From: Jovert Lota Palonpon Date: Tue, 9 Apr 2019 23:10:33 +0800 Subject: [PATCH] Validate only when submitCount > 0, resolve #22 --- .eslintrc | 2 +- .../views/__backoffice/users/Forms/Account.js | 55 ++- .../views/__backoffice/users/Forms/Profile.js | 382 +++++++++--------- resources/js/views/auth/passwords/Request.js | 31 +- resources/js/views/auth/passwords/Reset.js | 45 ++- resources/js/views/layouts/Auth.js | 6 +- 6 files changed, 284 insertions(+), 237 deletions(-) diff --git a/.eslintrc b/.eslintrc index a7d0e6c..b5c6548 100755 --- a/.eslintrc +++ b/.eslintrc @@ -17,7 +17,7 @@ }, "plugins": ["react"], "rules": { - "indent": ["warn", "4"], + "indent": ["warn", 4], "linebreak-style": ["warn", "unix"], "quotes": ["warn", "single"], "semi": ["warn", "always"], diff --git a/resources/js/views/__backoffice/users/Forms/Account.js b/resources/js/views/__backoffice/users/Forms/Account.js index 6ace5f6..a2b1d46 100644 --- a/resources/js/views/__backoffice/users/Forms/Account.js +++ b/resources/js/views/__backoffice/users/Forms/Account.js @@ -32,7 +32,7 @@ const Account = props => { onSubmit={handleSubmit} validateOnBlur={false} > - {({ values, handleChange, errors, isSubmitting }) => ( + {({ values, handleChange, errors, submitCount, isSubmitting }) => (
Account Settings @@ -42,7 +42,10 @@ const Account = props => { 0 && + errors.hasOwnProperty('type') + } > Type{' '} @@ -68,11 +71,12 @@ const Account = props => { User - {errors.hasOwnProperty('type') && ( - - {errors.type} - - )} + {submitCount > 0 && + errors.hasOwnProperty('type') && ( + + {errors.type} + + )} @@ -81,7 +85,10 @@ const Account = props => { 0 && + errors.hasOwnProperty('email') + } > Email{' '} @@ -95,18 +102,23 @@ const Account = props => { onChange={handleChange} fullWidth /> - {errors.hasOwnProperty('email') && ( - - {errors.email} - - )} + + {submitCount > 0 && + errors.hasOwnProperty('email') && ( + + {errors.email} + + )} 0 && + errors.hasOwnProperty('username') + } > Username @@ -119,11 +131,13 @@ const Account = props => { onChange={handleChange} fullWidth /> - {errors.hasOwnProperty('username') && ( - - {errors.username} - - )} + + {submitCount > 0 && + errors.hasOwnProperty('username') && ( + + {errors.username} + + )} @@ -145,7 +159,8 @@ const Account = props => { color="primary" disabled={ (errors && - Object.keys(errors).length > 0) || + Object.keys(errors).length > 0 && + submitCount > 0) || isSubmitting } > diff --git a/resources/js/views/__backoffice/users/Forms/Profile.js b/resources/js/views/__backoffice/users/Forms/Profile.js index 4fc5b4d..47a6668 100644 --- a/resources/js/views/__backoffice/users/Forms/Profile.js +++ b/resources/js/views/__backoffice/users/Forms/Profile.js @@ -65,226 +65,236 @@ const Profile = props => { > {({ values, - handleChange, - setFieldValue, errors, + submitCount, isSubmitting, - }) => { - if (apiErrors && Object.keys(apiErrors).length > 0) { - errors = apiErrors; - } - - return ( - - - Personal Information - - - - - - - Firstname{' '} - - * - - - - - - {errors.hasOwnProperty('firstname') && ( + handleChange, + setFieldValue, + }) => ( + + + Personal Information + + + + + 0 && + errors.hasOwnProperty('firstname') + } + > + + Firstname{' '} + * + + + + + {submitCount > 0 && + errors.hasOwnProperty('firstname') && ( {errors.firstname} )} - - - - - - - Middlename - - - + + - {errors.hasOwnProperty('middlename') && ( + + 0 && + errors.hasOwnProperty('middlename') + } + > + + Middlename + + + + + {submitCount > 0 && + errors.hasOwnProperty('middlename') && ( {errors.middlename} )} - - - - - - - Lastname{' '} - - * - - - - + + - {errors.hasOwnProperty('lastname') && ( + + 0 && + errors.hasOwnProperty('lastname') + } + > + + Lastname{' '} + * + + + + + {submitCount > 0 && + errors.hasOwnProperty('lastname') && ( {errors.lastname} )} - - + - - - - + + + + 0 && + errors.hasOwnProperty('gender') + } + > + Gender + + } - autoWidth - > - - Please select the gender - - - - Female - - - Male - - - {errors.hasOwnProperty('gender') && ( + + Please select the gender + + + Female + + Male + + + {submitCount > 0 && + errors.hasOwnProperty('gender') && ( {errors.gender} )} - - + + - - - - - setFieldValue('birthdate', date) - } - format="YYYY-MM-DD" - maxDate={moment() - .subtract(10, 'y') - .subtract(1, 'd') - .format('YYYY-MM-DD')} - keyboard - clearable - disableFuture - /> - - - {errors.hasOwnProperty('birthdate') && ( + + 0 && + errors.hasOwnProperty('birthdate') + } + > + + + setFieldValue('birthdate', date) + } + format="YYYY-MM-DD" + maxDate={moment() + .subtract(10, 'y') + .subtract(1, 'd') + .format('YYYY-MM-DD')} + keyboard + clearable + disableFuture + /> + + + {submitCount > 0 && + errors.hasOwnProperty('birthdate') && ( {errors.birthdate} )} - - + - - - - - - Address - - - - - {errors.hasOwnProperty('address') && ( + + + + + 0 && + errors.hasOwnProperty('address') + } + > + + Address + + + + + {submitCount > 0 && + errors.hasOwnProperty('address') && ( {errors.address} )} - - + - -
- - - - - + + +
+ + + + - - ); - }} + + + )} ); }; diff --git a/resources/js/views/auth/passwords/Request.js b/resources/js/views/auth/passwords/Request.js index d29a786..883aada 100755 --- a/resources/js/views/auth/passwords/Request.js +++ b/resources/js/views/auth/passwords/Request.js @@ -49,12 +49,7 @@ class PasswordRequest extends Component { message: { type: 'success', title: 'Link Sent', - body: ( - - Check your email to reset your account. -
Thank you. -
- ), + body: `Check your email to reset your account. Thank you.`, action: () => history.push(`/signin?username=${email}`), }, }); @@ -65,12 +60,7 @@ class PasswordRequest extends Component { message: { type: 'error', title: 'Something went wrong', - body: ( - - Oops? Something went wrong here. -
Please try again. -
- ), + body: `Oops? Something went wrong here. Please try again.`, action: () => window.location.reload(), }, }); @@ -126,7 +116,13 @@ class PasswordRequest extends Component { ), })} > - {({ values, handleChange, errors, isSubmitting }) => ( + {({ + values, + handleChange, + errors, + submitCount, + isSubmitting, + }) => (
@@ -138,8 +134,12 @@ class PasswordRequest extends Component { onChange={handleChange} variant="outlined" fullWidth - error={errors.hasOwnProperty('email')} + error={ + submitCount > 0 && + errors.hasOwnProperty('email') + } helperText={ + submitCount > 0 && errors.hasOwnProperty('email') && errors.email } @@ -180,7 +180,8 @@ class PasswordRequest extends Component { disabled={ (errors && Object.keys(errors).length > - 0) || + 0 && + submitCount > 0) || isSubmitting } > diff --git a/resources/js/views/auth/passwords/Reset.js b/resources/js/views/auth/passwords/Reset.js index 37444ce..eb76c96 100755 --- a/resources/js/views/auth/passwords/Reset.js +++ b/resources/js/views/auth/passwords/Reset.js @@ -72,7 +72,18 @@ class PasswordReset extends Component { this.setState({ loading: false }); } catch (error) { if (!error.response) { - throw new Error('Unknown error'); + this.setState({ + loading: false, + message: { + type: 'error', + title: 'Something went wrong', + body: + 'Oops? Something went wrong here. Please try again.', + action: () => window.location.reload(), + }, + }); + + return; } const { errors } = error.response.data; @@ -159,7 +170,13 @@ class PasswordReset extends Component { ), })} > - {({ values, handleChange, errors, isSubmitting }) => ( + {({ + values, + handleChange, + errors, + submitCount, + isSubmitting, + }) => ( @@ -173,10 +190,12 @@ class PasswordReset extends Component { onChange={handleChange} variant="outlined" fullWidth - error={errors.hasOwnProperty( - 'password', - )} + error={ + submitCount > 0 && + errors.hasOwnProperty('password') + } helperText={ + submitCount > 0 && errors.hasOwnProperty('password') && errors.password } @@ -216,13 +235,18 @@ class PasswordReset extends Component { onChange={handleChange} variant="outlined" fullWidth - error={errors.hasOwnProperty( - 'password_confirmation', - )} + error={ + submitCount > 0 && + errors.hasOwnProperty( + 'password_confirmation', + ) + } helperText={ + submitCount > 0 && errors.hasOwnProperty( 'password_confirmation', - ) && errors.password_confirmation + ) && + errors.password_confirmation } InputProps={{ endAdornment: ( @@ -281,7 +305,8 @@ class PasswordReset extends Component { disabled={ (errors && Object.keys(errors).length > - 0) || + 0 && + submitCount > 0) || isSubmitting } > diff --git a/resources/js/views/layouts/Auth.js b/resources/js/views/layouts/Auth.js index 94d06ff..08d8080 100755 --- a/resources/js/views/layouts/Auth.js +++ b/resources/js/views/layouts/Auth.js @@ -50,11 +50,7 @@ const Auth = props => ( : props.title} - + {props.message.hasOwnProperty('body') ? props.message.body : props.subTitle}