generated from yandex-praktikum/mesto-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditProfilePopup.js
104 lines (88 loc) · 3.26 KB
/
EditProfilePopup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useState, useEffect, useContext } from "react";
import { CurrentUserContext } from "../../contexts/CurrentUserContext";
import PopupWithForm from "../PopupWithForm/PopupWithForm";
export default function EditProfilePopup(props) {
const { onUpdateUser, isOpened, popupPackProps } = props;
const currentUser = useContext(CurrentUserContext);
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const userNameLength = name && name.length;
const userDescriptionLength = description && description.length;
useEffect(() => {
if (isOpened) {
setName(currentUser.name);
setDescription(currentUser.about);
};
}, [isOpened, currentUser]);
function handleChangeName(evt) {
const target = evt.target.value.replace(/^\s/, '');
setName(target);
};
function handleChangeDescription(evt) {
const target = evt.target.value.replace(/^\s/, '');
setDescription(target);
};
function handleSubmit(evt) {
evt.preventDefault();
onUpdateUser({
name: name.trim().replace(/\s+/g, ' '),
about: description.trim().replace(/\s+/g, ' ')
});
};
function isInputValueValid(value) {
return value >= 2;
};
function isEditProfilePopupValid() {
return isInputValueValid(userNameLength) && isInputValueValid(userDescriptionLength);
};
function showErrorMessage(inputValue) {
if (inputValue >= 1 && inputValue < 2) {
return `Текст должен быть не короче 2 симв. Длина текста сейчас: ${inputValue} символ.`
};
return 'Заполните это поле.';
};
return (
<PopupWithForm
popupData={{
classSelector: "edit-profile",
classSelectorModifierForm: "popup__form_type_profile",
formName: "profileInfoEditor",
title: "Редактировать профиль",
submitBtn: "Сохранить",
isPopupValid: isEditProfilePopupValid()
}}
onSubmit={handleSubmit}
isOpened={isOpened}
popupPackProps={popupPackProps}
>
<fieldset className="popup__form-fieldset">
<input
className={`popup__form-field ${!isInputValueValid(userNameLength) && 'popup__form-field_type_error'} popup__form-field_type_profile-name`}
name="userName"
type="text"
placeholder="Имя"
maxLength="40"
required
value={name || ''}
onChange={handleChangeName}
/>
<span className={`popup__error ${!isInputValueValid(userNameLength) && 'popup__error_visible'} input-name-error`}>
{showErrorMessage(userNameLength)}
</span>
<input
className={`popup__form-field ${!isInputValueValid(userDescriptionLength) && 'popup__form-field_type_error'} popup__form-field_type_profile-job`}
name="userAbout"
type="text"
placeholder="О себе"
maxLength="200"
required
value={description || ''}
onChange={handleChangeDescription}
/>
<span className={`popup__error ${!isInputValueValid(userDescriptionLength) && 'popup__error_visible'} input-job-error`}>
{showErrorMessage(userDescriptionLength)}
</span>
</fieldset>
</PopupWithForm>
);
};