Skip to content

Commit

Permalink
Merge pull request #580 from ita-social-projects/#579-EditProfileFixB…
Browse files Browse the repository at this point in the history
…ugWithUndefinedValue

#579 [Edit Profile] Fix bug with undefined value
  • Loading branch information
Lvyshnevska authored May 13, 2024
2 parents 159ec69 + 64c374c commit 3c7d151
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PropTypes } from 'prop-types';
import preventEnterSubmit from '../../../../utils/preventEnterSubmit';
import css from './TextField.module.css';

Expand All @@ -23,6 +24,7 @@ const TextField = (props) => {
onKeyDown={preventEnterSubmit}
required={(props.requredField) ? 'required' : ''}
disabled={(props.name === 'email') ? 'disabled' : ''}
onBlur={props.onBlur}
></textarea>
</div>
<div className={css['count__symbols']}>
Expand All @@ -38,3 +40,15 @@ const TextField = (props) => {
};

export default TextField;

TextField.propTypes = {
requredField: PropTypes.bool,
label: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
value: PropTypes.string,
fieldPlaceholder: PropTypes.string,
maxLength: PropTypes.number,
updateHandler: PropTypes.func,
error: PropTypes.string,
onBlur: PropTypes.func,
};
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const GeneralInfo = (props) => {
let isValid = true;
const newFormState = {};
for (const key in profile) {
if (key in ERRORS && (!profile[key] || (Array.isArray(profile[key]) && profile[key].length === 0))) {
if (key in ERRORS && (!profile[key] || (Array.isArray(profile[key]) && profile[key]?.length === 0))) {
isValid = false;
newFormState[key] = {
'error': true,
Expand All @@ -116,10 +116,10 @@ const GeneralInfo = (props) => {
}
}
setFormStateErr({ ...formStateErr, ...newFormState });
if (profile.official_name.length !== 0 && profile.official_name.length < 2) {
if (profile.official_name?.length !== 0 && profile.official_name?.length < 2) {
isValid = false;
}
if (profile.name.length < 2) {
if (profile.name?.length < 2) {
isValid = false;
}
if (profile.edrpou) {
Expand Down Expand Up @@ -147,7 +147,7 @@ const GeneralInfo = (props) => {

const onUpdateField = e => {
const { value: fieldValue, name: fieldName } = e.target;
const symbolCount = (fieldValue.replace(/[\s]/g,'')).length;
const symbolCount = (fieldValue.replace(/[\s]/g,''))?.length;
setFormStateErr({ ...formStateErr, [fieldName]: {'error': false, 'message': ''}});
if (fieldName === 'name' && symbolCount < 2) {
setFormStateErr({ ...formStateErr, [fieldName]: {'error': true, 'message': 'Введіть від 2 до 100 символів'}});
Expand Down Expand Up @@ -220,7 +220,7 @@ const GeneralInfo = (props) => {
};

const onUpdateTextAreaField = e => {
if (e.target.value.length <= TEXT_AREA_MAX_LENGTH)
if (e.target.value?.length <= TEXT_AREA_MAX_LENGTH)
setProfile((prevState) => {
return { ...prevState, [e.target.name]: e.target.value };
});
Expand Down Expand Up @@ -491,6 +491,7 @@ const GeneralInfo = (props) => {
name="common_info"
label={LABELS.common_info}
updateHandler={onUpdateTextAreaField}
onBlur={onBlurHandler}
value={profile.common_info ?? ''}
maxLength={TEXT_AREA_MAX_LENGTH}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const UserInfo = (props) => {
}));
};

const errorsInNameSurname = formStateErr['name']['message'].length > 1 || formStateErr['surname']['message'].length > 1;
const errorsInNameSurname = formStateErr['name']['message']?.length > 1 || formStateErr['surname']['message']?.length > 1;

const checkRequiredFields = () => {
let isValid = true;
Expand All @@ -100,10 +100,10 @@ const UserInfo = (props) => {
}
setFormStateErr({ ...formStateErr, ...newFormState });

if (updateUser.name.length < 2 || updateUser.surname.length < 2) {
if (updateUser.name?.length < 2 || updateUser.surname?.length < 2) {
isValid = false;
}
if (updateProfile.person_position.length !== 0 && updateProfile.person_position.length < 2) {
if (updateProfile.person_position?.length !== 0 && updateProfile.person_position?.length < 2) {
isValid = false;
}

Expand Down

0 comments on commit 3c7d151

Please sign in to comment.