Skip to content

Commit

Permalink
Merge pull request #24 from 001elijah/auth
Browse files Browse the repository at this point in the history
Fix password
  • Loading branch information
001elijah authored Jun 26, 2023
2 parents e2ba9a4 + 0014758 commit 4013c59
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/assets/icons/sprite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/components/LoginForm/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const Login = () => {
value={formik.values.password}
placeholder="Confirm a password"
/>

<svg className={y.eye} onClick={toggleShowPassword}>
<use
href={showPassword ? `${icons}#icon-eye` : `${icons}#icon-antiEye`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/LoginForm/Login.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.error {
position: absolute;
color: $red-error-color;
font-size: 12px;
font-size: 10px;
}

.eye {
Expand Down
87 changes: 46 additions & 41 deletions src/components/ModalEditProfile/ModalEditProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { useFormik } from 'formik';
import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import PropTypes from 'prop-types';
// import { resizeFile } from 'react-image-file-resizer';
import { Notify } from 'notiflix';

import { ButtonAuth } from '../ButtonAuth/ButtonAuth';
import { validationSchemaRegister } from '../SchemaValidation/schemaValidation';
import { validationSchemaEditProfile } from '../SchemaValidation/schemaValidation';
import { updateUser } from 'redux/Auth/authOperations';
import { selectorTheme } from 'redux/Auth/authSelectors';
import {
selectorTheme,
selectorUserName,
selectorAvatarURL,
selectorEmail,
} from 'redux/Auth/authSelectors';

import icons from '../../assets/icons/sprite.svg';
import s from './ModalEditProfile.module.scss';
Expand All @@ -19,12 +24,10 @@ export const ModalEditProfile = ({ onClose }) => {
const [showPassword, setShowPassword] = useState(false);
const [imageFile, setImageFile] = useState(null);

// eslint-disable-next-line
const formData = new FormData();

const name = useSelector(state => state.auth.userName);
const email = useSelector(state => state.auth.email);
const name = useSelector(selectorUserName);
const email = useSelector(selectorEmail);
const thema = useSelector(selectorTheme);
const avatar = useSelector(selectorAvatarURL);

const toggleShowPassword = () => setShowPassword(!showPassword);

Expand All @@ -33,48 +36,51 @@ export const ModalEditProfile = ({ onClose }) => {
userName: name,
email: email,
password: '',
// avatarUrl: null,
// avatarUrl: avatar,
},
validationSchema: validationSchemaRegister,
validationSchema: validationSchemaEditProfile,
onSubmit: values => {
onClose();
console.log(values);
dispatch(updateUser(values));

const formData = new FormData();

formData.append('userName', values.userName);
formData.append('email', values.email);
formData.append('password', values.password);

if (imageFile) {
formData.append('avatarUrl', imageFile);
}

dispatch(updateUser(formData));
},
});

// const handleImageChange = event => {
// // formik.setFieldValue('image', event.currentTarget.files[0]);
// const file = event.currentTarget.files[0];
// formik.setFieldValue('image', file);
// setSelectedImage(URL.createObjectURL(file));
// };

const handleImageChange = event => {
console.log(event.currentTarget.files[0]);
// setImageFile(event.currentTarget.files[0])
const file = event.currentTarget.files[0];
setImageFile(URL.createObjectURL(file));
const imageFormat = ['image/png', 'image/jpeg', 'image/jpg'];

if (file) {
if (!imageFormat.includes(file.type)) {
Notify.failure('Invalid image format');
} else {
setImageFile(file);
}
}
};
// const handleImageChange = async event => {
// const file = event.currentTarget.files[0];

// try {
// console.log("gogogo")
// const resizedFile = await resizeFile(file, 300, 300, 'PNG', 80);
// formik.setFieldValue('image', resizedFile);
// setSelectedImage(URL.createObjectURL(resizedFile));
// } catch (error) {
// console.log('Ошибка при изменении размера изображения:', error);
// }
// };

return (
<form onSubmit={formik.handleSubmit}>
<label htmlFor="image" className={s.wrapImage}>
<label htmlFor="avatarUrl" className={s.wrapImage}>
<div>
{imageFile ? (
<img src={imageFile} alt="Selected Avatar" className={s.image} />
<img
src={URL.createObjectURL(imageFile)}
alt="Selected Avatar"
className={s.image}
/>
) : avatar ? (
<img src={avatar} alt="Avatar" className={s.image} />
) : (
<svg className={s.avatar}>
<use href={`${icons}#icon-user-${thema || 'light'}`}></use>
Expand All @@ -90,11 +96,10 @@ export const ModalEditProfile = ({ onClose }) => {
<use href={`${icons}#icon-plus`}></use>
</svg> */}
</div>
{/* {imageFile && <img src={imageFile} alt="Selected Avatar" />} */}
<input
className={s.isHidden}
id="image"
name="image"
id="avatarUrl"
name="avatarUrl"
type="file"
onChange={handleImageChange}
/>
Expand Down Expand Up @@ -138,7 +143,7 @@ export const ModalEditProfile = ({ onClose }) => {
value={formik.values.password}
placeholder="Password"
/>
<svg className={y.eye} onClick={toggleShowPassword}>
<svg className={`${s.eye} ${s[thema]}`} onClick={toggleShowPassword}>
<use
href={showPassword ? `${icons}#icon-eye` : `${icons}#icon-antiEye`}
></use>
Expand All @@ -153,5 +158,5 @@ export const ModalEditProfile = ({ onClose }) => {
};

ModalEditProfile.propTypes = {
onClose: PropTypes.node.isRequired,
onClose: PropTypes.func.isRequired,
};
30 changes: 25 additions & 5 deletions src/components/ModalEditProfile/ModalEditProfile.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
&:not(:last-of-type) {
margin-bottom: 14px;
}

&:focus-within .eye {
opacity: 0.9;
}
}

.avatar {
Expand Down Expand Up @@ -99,10 +103,26 @@
display: block;
width: 68px;
height: 68px;
border-radius: 8px;
}

// .buttonPlus {
// display: block;
// width: 7px;
// height: 7px;
// }
.eye {
position: absolute;
width: 19px;
height: 19px;

stroke: currentColor;
fill: currentColor;
color: $white-text-color;

right: 18px;
bottom: 15px;

opacity: 0.4;
cursor: pointer;

&.light,
&.colorful {
color: $black-text-color;
}
}
38 changes: 34 additions & 4 deletions src/components/SchemaValidation/schemaValidation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const validationSchemaLogin = yup.object().shape({
.string()
.matches(/^\S*$/, 'Must not contain spaces')
.matches(
/^[a-zA-Z0-9]+$/,
'Can only contain Latin letters, numbers and signs',
/^[a-zA-Z0-9!"@#$%^&*()]+$/,
'Can only contain Latin letters, numbers, and signs',
)
.min(8, 'Minimum length 8 characters')
.max(64, 'Maximum length 64 characters')
Expand Down Expand Up @@ -41,10 +41,40 @@ export const validationSchemaRegister = yup.object().shape({
.string()
.matches(/^\S*$/, 'Must not contain spaces')
.matches(
/^[a-zA-Z0-9]+$/,
'Can only contain Latin letters, numbers and signs',
/^[a-zA-Z0-9!"@#$%^&*()]+$/,
'Can only contain Latin letters, numbers, and signs',
)
.min(8, 'Minimum length 8 characters')
.max(64, 'Maximum length 64 characters')
.required('Password required'),
});

export const validationSchemaEditProfile = yup.object().shape({
userName: yup
.string()
.matches(
/^[a-zA-Z0-9_]+( [a-zA-Z0-9_]+)?$/,
'Only letters, numbers, underscores, and spaces are allowed',
)
.min(2, 'Minimum length 2 characters')
.max(32, 'Maximum length 32 characters')
.required('Name required'),
email: yup
.string()
.matches(
/^[\w.%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
'Invalid email address. Example: [email protected]',
)
.required('Email required'),
password: yup
.string()
.matches(/^\S*$/, 'Must not contain spaces')
.matches(
/^[a-zA-Z0-9!"@#$%^&*()]+$/,
'Can only contain Latin letters, numbers, and signs',
)
.min(8, 'Minimum length 8 characters')
.max(64, 'Maximum length 64 characters')
.required('Password required'),
avatarUrl: yup.string(),
});
2 changes: 1 addition & 1 deletion src/pages/HomePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Container } from 'components/Container';

export const HomePage = () => {
const isDesktopSize = useMediaQuery({ query: '(min-width: 1280px)' });

return (
<div>
<Container>
Expand Down
4 changes: 3 additions & 1 deletion src/redux/Auth/authOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export const updateUser = createAsyncThunk(
'auth/updateUser',
async (userData, { rejectWithValue }) => {
try {
const { data } = await updateUserApi(userData);
const data = await updateUserApi(userData);
Notify.success('Successful editing');

return data;
} catch (error) {
return rejectWithValue(error.message);
Expand Down
2 changes: 2 additions & 0 deletions src/redux/Auth/authSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const selectorTheme = state => state.auth.theme;
export const selectorUserName = state => state.auth.userName;

export const selectorAvatarURL = state => state.auth.avatarUrl;

export const selectorEmail = state => state.auth.email;
2 changes: 1 addition & 1 deletion src/services/backendAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const themeChangeUserApi = async theme => {

export const updateUserApi = async userData => {
const { data } = await axios.patch('user/updateUserInfo', userData);
return data;
return data.user;
};
//---------------------------------------------BOARDS---------------------//

Expand Down

0 comments on commit 4013c59

Please sign in to comment.