import { useState, useContext, useEffect } from 'react'; import { Link } from 'react-router-dom'; import useForm from '../../../hooks/useForm'; import useUpdateUserProfile from '../../../hooks/useUpdateUserProfile'; import useGetData from '../../../hooks/useGetData'; import { CredentialsWrapper, Input, LockIcon, PersonIcon, PhoneIcon, EditButton, FullNameWrapper, Wrapper, ButtonsWrapper, SaveButton, CancelButton, LockOpenIcon, } from './styles'; import { IconButton } from '@mui/material'; import { UserContext } from '../../../context/UserContext'; const normalizeProfile = (profile) => ({ firstName: profile.firstName, lastName: profile.lastName, email: profile.email, mobile: profile.mobile, plates: profile.plates }); const MyProfile = () => { const { user } = useContext(UserContext); const { data: userProfile, isLoading: isUserProfileLoading } = useGetData({ url: `/registriranParkirac/${user.id}`, }); const [isEditActivated, setIsEditActivated] = useState(false); const { data, onFormChange, setNewData } = useForm(null); const [password, setPassword] = useState(''); const [isPasswordVisible, setIsPasswordVisible] = useState(false); const { updateUserProfile } = useUpdateUserProfile(); const handleEditButton = () => { setIsEditActivated(true); }; const handleOnCancel = () => { setIsEditActivated(false); setNewData(normalizeProfile(userProfile)); setPassword(''); }; const successfulSave = () => { setIsEditActivated(false); setPassword(''); } const handleOnSave = () => { const updatedUserData = { ...data, password, }; updateUserProfile({ userData: updatedUserData, id: user.id, successfulSave, }); }; useEffect(() => { if(!userProfile) return; setNewData(normalizeProfile(userProfile)); }, [userProfile]); return ( , }} /> , }} /> setPassword(e.target.value)} InputProps={{ startAdornment: ( setIsPasswordVisible( !isPasswordVisible && isEditActivated ) } > {isPasswordVisible ? ( ) : ( )} ), }} type={ isPasswordVisible && isEditActivated ? 'text' : 'password' } /> {isEditActivated ? ( Откажи Сочувај ) : ( Уреди )} ); }; export default MyProfile;