generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Yura-Platonov/ChangePass
Change pass
- Loading branch information
Showing
9 changed files
with
216 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,132 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import * as Yup from 'yup'; | ||
import { Formik } from 'formik'; | ||
import { useEffect, useState } from 'react'; | ||
import { selectError } from 'redux/auth/selectors'; | ||
import { selectUser } from 'redux/user/selectors'; | ||
import { changePass } from 'redux/user/opetations'; | ||
import { Button, ErrorMessage, Field, Form, Label, Wraper } from 'components/ForgotPass/ForgotPass.styled'; | ||
import { HidePassword, PasswordInput } from 'components/LoginForm/LoginForm.styled'; | ||
import { ErrorSVG, ViewSVG } from 'components/LoginForm/chackBox'; | ||
|
||
const userSchema = Yup.object().shape({ | ||
password: Yup.string() | ||
.required('Введіть пароль') | ||
.min(5, 'Пароль повинен містити принаймні 5 символів') | ||
.matches( | ||
/^[^\u0400-\u04FF]*$/, | ||
'Пароль не повинен містити кириличні символи' | ||
) | ||
.matches( | ||
/^(?=.*[A-Z])/, | ||
'Пароль повинен містити принаймні одну велику літеру' | ||
) | ||
.matches( | ||
/^(?=.*[a-z])/, | ||
'Пароль повинен містити принаймні одну малу літеру' | ||
) | ||
.matches(/^(?=.*\d)/, 'Пароль повинен містити принаймні одну цифру') | ||
.matches(/^[^\s]*$/, 'Пароль не повинен містити пробіли'), | ||
password2: Yup.string() | ||
.required('Повторіть новий пароль') | ||
.test('passwords-match', 'Паролі повинні співпадати', function (value) { | ||
return value === this.parent.password; | ||
}), | ||
}); | ||
|
||
export default function ChangePass() { | ||
return <div></div>; | ||
const [showPassword, setShowPassword] = useState(false); | ||
const [showPassword2, setShowPassword2] = useState(false); | ||
const dispatch = useDispatch(); | ||
const [refError, setRefError] = useState(false); | ||
const requestError = useSelector(selectError); | ||
const userInfo = useSelector(selectUser); | ||
|
||
const handleSubmit = (values, { resetForm, setSubmitting }) => { | ||
const { password } = values; | ||
try { | ||
dispatch(changePass({ email: userInfo.email, password: password })); | ||
resetForm(); | ||
setSubmitting(false); | ||
} catch (error) { | ||
console.error('Error submitting form:', error); | ||
} | ||
}; | ||
|
||
const handleShowPassword = () => { | ||
setShowPassword(!showPassword); | ||
}; | ||
const handleShowPassword2 = () => { | ||
setShowPassword2(!showPassword2); | ||
}; | ||
|
||
useEffect(() => { | ||
if (requestError) setRefError(true); | ||
}, [requestError]); | ||
|
||
return ( | ||
<Formik | ||
onSubmit={handleSubmit} | ||
initialValues={{ password: '' }} | ||
validationSchema={userSchema} | ||
> | ||
{values => { | ||
return ( | ||
<Wraper> | ||
<h4 className="title">Заміна паролю</h4> | ||
<p className="text"> | ||
Спочатку введіть новий пароль. А потім повторіть його | ||
</p> | ||
<Form> | ||
<Label className={` marg8`}> | ||
<PasswordInput> | ||
<Field | ||
type={showPassword ? 'text' : 'password'} | ||
className={refError ? 'is-invalid' : ''} | ||
name="password" | ||
placeholder="Ввеліть новий пароль" | ||
value={values.password} | ||
/> | ||
|
||
{refError ? ( | ||
<span className="errorSVG"> | ||
<ErrorSVG /> | ||
</span> | ||
) : null} | ||
|
||
<HidePassword type="button" onClick={handleShowPassword}> | ||
<ViewSVG /> | ||
</HidePassword> | ||
</PasswordInput> | ||
<ErrorMessage name="password" component="div" /> | ||
</Label> | ||
<Label style={{marginTop:'24px'}}> | ||
<PasswordInput> | ||
<Field | ||
type={showPassword2 ? 'text' : 'password'} | ||
className={refError ? 'is-invalid' : ''} | ||
name="password2" | ||
placeholder="Повторіть новий пароль" | ||
value={values.password2} | ||
/> | ||
|
||
{refError ? ( | ||
<span className="errorSVG"> | ||
<ErrorSVG /> | ||
</span> | ||
) : null} | ||
|
||
<HidePassword type="button" onClick={handleShowPassword2}> | ||
<ViewSVG /> | ||
</HidePassword> | ||
</PasswordInput> | ||
<ErrorMessage name="password2" component="div" /> | ||
</Label> | ||
<Button type="submit">Зберегти</Button> | ||
</Form> | ||
</Wraper> | ||
); | ||
}} | ||
</Formik> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { Navigate, Outlet } from 'react-router-dom'; | ||
import { Navigate } from 'react-router-dom'; | ||
import { selectIsLoggedIn } from '../redux/auth/selectors'; | ||
|
||
export const PrivateRoute = () => { | ||
export const PrivateRoute = ({ children, redirectTo = '/' }) => { | ||
const IsLoggedIn = useSelector(selectIsLoggedIn); | ||
|
||
return IsLoggedIn ? <Outlet /> : <Navigate to="/" replace />; | ||
return IsLoggedIn ? children : <Navigate to={redirectTo} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.