| const [openDrawer, setOpenDrawer] = useState(false); | const [openDrawer, setOpenDrawer] = useState(false); | ||||
| useEffect(() => { | useEffect(() => { | ||||
| dispatch(fetchMineProfile()); | |||||
| if (user && user?.length > 0) { | |||||
| dispatch(fetchMineProfile()); | |||||
| } | |||||
| }, []); | }, []); | ||||
| useEffect(() => { | useEffect(() => { | ||||
| setUserPopoverOpen(false); | setUserPopoverOpen(false); | ||||
| ) { | ) { | ||||
| shouldShowHeader = false; | shouldShowHeader = false; | ||||
| } | } | ||||
| if (location.pathname === "/" && user?.length === 0) { | |||||
| shouldShowHeader = false; | |||||
| } | |||||
| setShouldShow(shouldShowHeader); | setShouldShow(shouldShowHeader); | ||||
| }, [location.pathname, user, routeMatch]); | }, [location.pathname, user, routeMatch]); | ||||
| const error = useSelector(selectLoginError); | const error = useSelector(selectLoginError); | ||||
| return ( | return ( | ||||
| <> | <> | ||||
| {formik.errors.password && formik.touched.password && ( | |||||
| <ErrorText>{formik.errors.password}</ErrorText> | |||||
| )} | |||||
| {error.length > 0 && !formik.errors.password && ( | |||||
| {error.length > 0 ? ( | |||||
| <ErrorText>{error}</ErrorText> | <ErrorText>{error}</ErrorText> | ||||
| ) : formik.errors.email?.length > 0 && formik.touched.email ? ( | |||||
| <ErrorText>{formik.errors.email}</ErrorText> | |||||
| ) : formik.errors.password?.length > 0 && formik.touched.password && ( | |||||
| <ErrorText>{formik.errors.password}</ErrorText> | |||||
| )} | )} | ||||
| </> | </> | ||||
| ); | ); |
| import React, { useEffect, useState } from "react"; | |||||
| import React, { forwardRef, useEffect, useState } from "react"; | |||||
| import PropTypes from "prop-types"; | import PropTypes from "prop-types"; | ||||
| import IconButton from "../../../IconButton/IconButton"; | import IconButton from "../../../IconButton/IconButton"; | ||||
| import { ReactComponent as VisibilityOn } from "../../../../assets/images/svg/eye-striked.svg"; | import { ReactComponent as VisibilityOn } from "../../../../assets/images/svg/eye-striked.svg"; | ||||
| import { TextField } from "../../../TextFields/TextField/TextField"; | import { TextField } from "../../../TextFields/TextField/TextField"; | ||||
| import { useTranslation } from "react-i18next"; | import { useTranslation } from "react-i18next"; | ||||
| const PasswordField = (props) => { | |||||
| const PasswordField = forwardRef((props, ref) => { | |||||
| const formik = props.formik; | const formik = props.formik; | ||||
| const [showPassword, setShowPassword] = useState(false); | const [showPassword, setShowPassword] = useState(false); | ||||
| const handleClickShowPassword = () => setShowPassword(!showPassword); | const handleClickShowPassword = () => setShowPassword(!showPassword); | ||||
| const handleMouseDownPassword = () => setShowPassword(!showPassword); | const handleMouseDownPassword = () => setShowPassword(!showPassword); | ||||
| const error = useSelector(selectLoginError); | const error = useSelector(selectLoginError); | ||||
| const {t} = useTranslation(); | const {t} = useTranslation(); | ||||
| // useEffect(() => { | |||||
| // console.log("error", error); | |||||
| // console.log("formik errors password", formik.errors.password); | |||||
| // console.log("formik touched", formik.touched); | |||||
| // console.log("formik.isVaid", formik); | |||||
| // if (!formik.isValid) formik.setFieldValue("password", ""); | |||||
| // // if (error?.length > 0 || formik.errors.password) { | |||||
| // // formik.setFieldValue("password", ""); | |||||
| // // console.log(formik.errors.password) | |||||
| // // } | |||||
| // }, [formik.isValid]) | |||||
| useEffect(() => { | useEffect(() => { | ||||
| if (error?.length > 0 || formik.errors.password) { | |||||
| formik.setFieldValue("password", ""); | |||||
| console.log(formik.errors.password) | |||||
| } | |||||
| }, [error, formik.errors.password]) | |||||
| console.log(error); | |||||
| }, [error]) | |||||
| return ( | return ( | ||||
| <TextField | <TextField | ||||
| name="password" | name="password" | ||||
| ref={ref} | |||||
| placeholder={t("common.labelPassword")} | placeholder={t("common.labelPassword")} | ||||
| margin="normal" | margin="normal" | ||||
| type={showPassword ? "text" : "password"} | type={showPassword ? "text" : "password"} | ||||
| value={formik.values.password} | value={formik.values.password} | ||||
| onChange={formik.handleChange} | onChange={formik.handleChange} | ||||
| error={ | error={ | ||||
| (formik.touched.password && formik.errors.password) || error.length > 0 | |||||
| (formik.touched.password && formik.errors.password?.length > 0) || error.length > 0 | |||||
| } | } | ||||
| helperText={formik.touched.password && formik.errors.password} | helperText={formik.touched.password && formik.errors.password} | ||||
| fullWidth | fullWidth | ||||
| }} | }} | ||||
| /> | /> | ||||
| ); | ); | ||||
| }; | |||||
| }); | |||||
| PasswordField.displayName = "PasswordField"; | |||||
| PasswordField.propTypes = { | PasswordField.propTypes = { | ||||
| formik: PropTypes.any, | formik: PropTypes.any, |
| import React, { useEffect } from "react"; | |||||
| import React, { useEffect, useRef, useState } from "react"; | |||||
| import PropTypes from "prop-types"; | import PropTypes from "prop-types"; | ||||
| import { useFormik } from "formik"; | import { useFormik } from "formik"; | ||||
| import { useDispatch, useSelector } from "react-redux"; | import { useDispatch, useSelector } from "react-redux"; | ||||
| const dispatch = useDispatch(); | const dispatch = useDispatch(); | ||||
| const error = useSelector(selectLoginError); | const error = useSelector(selectLoginError); | ||||
| const history = useHistory(); | const history = useHistory(); | ||||
| const passwordRef = useRef(null); | |||||
| const [passwordReset, setPasswordReseted] = useState(false); | |||||
| useEffect(() => { | useEffect(() => { | ||||
| dispatch(clearLoginErrors()); | dispatch(clearLoginErrors()); | ||||
| }); | }); | ||||
| }; | }; | ||||
| useEffect(() => { | |||||
| if (passwordReset) { | |||||
| formik.setFieldValue("password", ""); | |||||
| } | |||||
| }, [passwordReset]); | |||||
| const handleApiResponseError = () => { | |||||
| setPasswordReseted(true); | |||||
| }; | |||||
| const handleSubmitForm = (e) => { | |||||
| e.preventDefault(); | |||||
| console.log("handle submit form"); | |||||
| if (!formik.isValid) { | |||||
| console.log("greska"); | |||||
| formik.setFieldValue("password", ""); | |||||
| } | |||||
| formik.handleSubmit(e); | |||||
| }; | |||||
| const handleSubmit = (values) => { | const handleSubmit = (values) => { | ||||
| const { email, password } = values; | const { email, password } = values; | ||||
| dispatch(clearLoginErrors()); | |||||
| dispatch( | |||||
| fetchLogin({ | |||||
| email, | |||||
| password, | |||||
| handleApiResponseSuccess, | |||||
| }) | |||||
| ); | |||||
| console.log(values); | |||||
| if (!formik.isValid) { | |||||
| console.log("invalid"); | |||||
| } else { | |||||
| dispatch(clearLoginErrors()); | |||||
| dispatch( | |||||
| fetchLogin({ | |||||
| email, | |||||
| password, | |||||
| handleApiResponseSuccess, | |||||
| handleApiResponseError, | |||||
| }) | |||||
| ); | |||||
| console.log(values); | |||||
| } | |||||
| }; | }; | ||||
| const formik = useFormik({ | const formik = useFormik({ | ||||
| useEffect(() => { | useEffect(() => { | ||||
| if (error) { | if (error) { | ||||
| if (formik.errors.email || formik.errors.password) { | if (formik.errors.email || formik.errors.password) { | ||||
| dispatch(clearLoginErrors()); | |||||
| if (!passwordReset) { | |||||
| dispatch(clearLoginErrors()); | |||||
| } else { | |||||
| setPasswordReseted(false); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| }, [formik.errors.email, formik.errors.password]); | }, [formik.errors.email, formik.errors.password]); | ||||
| <Logo /> | <Logo /> | ||||
| <LoginTitle /> | <LoginTitle /> | ||||
| <LoginDescription /> | <LoginDescription /> | ||||
| <LoginFormContainer component="form" onSubmit={formik.handleSubmit}> | |||||
| <LoginFormContainer component="form" onSubmit={handleSubmitForm}> | |||||
| <EmailField formik={formik} /> | <EmailField formik={formik} /> | ||||
| <PasswordField formik={formik} /> | |||||
| <PasswordField formik={formik} ref={passwordRef} /> | |||||
| <ErrorMessage formik={formik} /> | <ErrorMessage formik={formik} /> | ||||
| <ForgotPasswordLink /> | <ForgotPasswordLink /> | ||||
| <LoginButton formik={formik} /> | <LoginButton formik={formik} /> |
| height="48px" | height="48px" | ||||
| fullWidth | fullWidth | ||||
| buttoncolor={selectedTheme.primaryPurple} | buttoncolor={selectedTheme.primaryPurple} | ||||
| onClick={formik.handleSubmit} | |||||
| textcolor="white" | textcolor="white" | ||||
| disabled={ | disabled={ | ||||
| formik.values.email.length === 0 || formik.values.password.length === 0 | formik.values.email.length === 0 || formik.values.password.length === 0 |
| function* fetchLogin({ payload }) { | function* fetchLogin({ payload }) { | ||||
| try { | try { | ||||
| console.log('barem ide') | |||||
| const { data } = yield call(attemptLogin, payload); | const { data } = yield call(attemptLogin, payload); | ||||
| if (data.token) { | if (data.token) { | ||||
| const token = data.token; | const token = data.token; |