From 8a8db0dc7e588fa86dbc5122819cbdbcef7d584a Mon Sep 17 00:00:00 2001 From: sankalp Date: Tue, 24 Aug 2021 21:00:25 +0530 Subject: [PATCH] [change] Reusable fuction for errors #357 Added get-error-field to remove repeated code. Closes #357 --- client/components/login/login.js | 29 +--- .../mobile-phone-change.js | 16 +- .../mobile-phone-verification.js | 16 +- .../password-change/password-change.js | 25 +-- .../password-confirm/password-confirm.js | 24 +-- .../password-reset/password-reset.js | 8 +- .../components/registration/registration.js | 151 +++++------------- client/utils/get-error-field.js | 11 ++ 8 files changed, 72 insertions(+), 208 deletions(-) create mode 100644 client/utils/get-error-field.js diff --git a/client/components/login/login.js b/client/components/login/login.js index 6a29f2c5..520573f4 100644 --- a/client/components/login/login.js +++ b/client/components/login/login.js @@ -25,6 +25,7 @@ import handleChange from "../../utils/handle-change"; import Contact from "../contact-box"; import Modal from "../modal"; import {Status} from "../organization-wrapper/lazy-import"; +import getErrorField from "../../utils/get-error-field"; const PhoneInput = React.lazy(() => import("react-phone-input-2")); @@ -111,12 +112,7 @@ export default class Login extends React.Component { return (
- {errors.username && ( -
- ! - {errors.username} -
- )} + {getErrorField(errors, "username")} - {errors.username && ( -
- ! - {errors.username} -
- )} + {getErrorField(errors, "username")} - {errors.nonField && ( -
- ! - {errors.nonField} -
- )} + {getErrorField(errors, "nonField", true)} {this.getUsernameField(input_fields)}
- {errors.password && ( -
- ! - {errors.password} -
- )} + {getErrorField(errors, "password")} import("react-phone-input-2")); @@ -127,21 +128,10 @@ class MobilePhoneChange extends React.Component { >
- {errors.nonField && ( -
- ! - {errors.nonField} -
- )} - + {getErrorField(errors, "nonField", true)}
- {errors.phone_number && ( -
- ! - {errors.phone_number} -
- )} + {getErrorField(errors, "phone_number")}

{t`PHONE_VERIFY (${phone_number})`}

- - {errors.nonField && ( -
- ! - {errors.nonField} -
- )} + {getErrorField(errors, "nonField", true)}
- {errors.code && ( -
- ! - {errors.code} -
- )} + {getErrorField(errors, "code")}

{t`PWD_CHANGE_TITL`}

- - {errors.nonField && ( -
- ! - {errors.nonField} -
- )} + {getErrorField(errors, "nonField", true)}
- - {errors.newPassword1 && ( -
- ! - {errors.newPassword1} -
- )} + {getErrorField(errors, "newPassword1")} - - {errors.newPassword2 && ( -
- ! - {errors.newPassword2} -
- )} + {getErrorField(errors, "newPassword2")} - {errors.nonField && ( -
- ! - {errors.nonField} -
- )} + {getErrorField(errors, "nonField", true)}
- - {errors.newPassword1 && ( -
- ! - {errors.newPassword1} -
- )} + {getErrorField(errors, "newPassword1")} - - {errors.newPassword2 && ( -
- ! - {errors.newPassword2} -
- )} + {getErrorField(errors, "newPassword2")}
- {errors.email && ( -
- ! - {errors.email} -
- )} + {getErrorField(errors, "email")} import("react-phone-input-2")); @@ -364,12 +365,7 @@ export default class Registration extends React.Component { >
- {errors.nonField && ( -
- ! - {errors.nonField} -
- )} + {getErrorField(errors, "nonField", true)} {plans.length > 0 && this.getPlanSelection()} {(plans.length === 0 || (plans.length > 0 && selectedPlan !== null)) && ( @@ -379,15 +375,7 @@ export default class Registration extends React.Component { input_fields.phone_number && (
- - {errors.phone_number && ( -
- ! - - {errors.phone_number} - -
- )} + {getErrorField(errors, "phone_number")} - {errors.email && ( -
- ! - - {errors.email} - -
- )} + {getErrorField(errors, "email", false, "email")} - {errors.email && ( -
- ! - - {errors.username} - -
- )} + {getErrorField(errors, "email", false, "username")} - {errors.first_name && ( -
- ! - - {errors.first_name} - -
+ {getErrorField( + errors, + "first_name", + false, + "first_name", )} - {errors.last_name && ( -
- ! - - {errors.last_name} - -
+ {getErrorField( + errors, + "last_name", + false, + "last_name", )} - {errors.birth_date && ( -
- ! - - {errors.birth_date} - -
+ {getErrorField( + errors, + "birth_date", + false, + "birth_date", )} - {errors.location && ( -
- ! - - {errors.location} - -
- )} + {getErrorField(errors, "location", false, "location")} - {errors.password1 && ( -
- ! - {errors.password1} -
- )} + {getErrorField(errors, "password1")} {t`CONFIRM_PWD_LBL`} - {errors.password2 && ( -
- ! - - {errors.password2} - -
- )} + {getErrorField(errors, "password2", false, "confirm")}
- {errors.country && ( -
- ! - - {errors.country} - -
+ {getErrorField( + errors, + "country", + false, + "country", )}
- {errors.street && ( -
- ! - - {errors.street} - -
- )} + {getErrorField(errors, "street", false, "street")}
- {errors.zipcode && ( -
- ! - - {errors.zipcode} - -
+ {getErrorField( + errors, + "zipcode", + false, + "zipcode", )} {t`TAX_NUMBER_LBL`} - {errors.tax_number && ( -
- ! - - {errors.tax_number} - -
+ {getErrorField( + errors, + "tax_number", + false, + "tax_number", )} + errors[key] ? ( +
+ ! + {errors[key]} +
+ ) : null; + +export default getErrorField;