source: sources/client/src/components/user/MyProfile/index.js@ 747e0ab

Last change on this file since 747e0ab was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 2 years ago

Push before video

  • Property mode set to 100644
File size: 5.3 KB
Line 
1import { useState, useContext, useEffect } from 'react';
2import { Link } from 'react-router-dom';
3import useForm from '../../../hooks/useForm';
4import useUpdateUserProfile from '../../../hooks/useUpdateUserProfile';
5import useGetData from '../../../hooks/useGetData';
6
7import {
8 CredentialsWrapper,
9 Input,
10 LockIcon,
11 PersonIcon,
12 PhoneIcon,
13 EditButton,
14 FullNameWrapper,
15 Wrapper,
16 ButtonsWrapper,
17 SaveButton,
18 CancelButton,
19 LockOpenIcon,
20} from './styles';
21import { IconButton } from '@mui/material';
22
23import { UserContext } from '../../../context/UserContext';
24
25const normalizeProfile = (profile) => ({
26 firstName: profile.firstName,
27 lastName: profile.lastName,
28 email: profile.email,
29 mobile: profile.mobile,
30 plates: profile.plates
31});
32
33const MyProfile = () => {
34 const { user } = useContext(UserContext);
35 const { data: userProfile, isLoading: isUserProfileLoading } = useGetData({
36 url: `/registriranParkirac/${user.id}`,
37 });
38 const [isEditActivated, setIsEditActivated] = useState(false);
39 const { data, onFormChange, setNewData } = useForm(null);
40 const [password, setPassword] = useState('');
41 const [isPasswordVisible, setIsPasswordVisible] = useState(false);
42 const { updateUserProfile } = useUpdateUserProfile();
43
44 const handleEditButton = () => {
45 setIsEditActivated(true);
46 };
47
48 const handleOnCancel = () => {
49 setIsEditActivated(false);
50 setNewData(normalizeProfile(userProfile));
51 setPassword('');
52 };
53
54 const successfulSave = () => {
55 setIsEditActivated(false);
56 setPassword('');
57 }
58
59 const handleOnSave = () => {
60 const updatedUserData = {
61 ...data,
62 password,
63 };
64 updateUserProfile({
65 userData: updatedUserData,
66 id: user.id,
67 successfulSave,
68 });
69 };
70
71 useEffect(() => {
72 if(!userProfile) return;
73 setNewData(normalizeProfile(userProfile));
74 }, [userProfile]);
75
76 return (
77 <Wrapper>
78 <CredentialsWrapper>
79 <FullNameWrapper>
80 <Input
81 disabled={!isEditActivated}
82 name='firstName'
83 placeholder='Име'
84 style={{
85 width: '49%',
86 }}
87 value={data.firstName}
88 onChange={onFormChange}
89 />
90 <Input
91 disabled={!isEditActivated}
92 name='lastName'
93 placeholder='Презиме'
94 style={{
95 width: '49%',
96 }}
97 value={data.lastName}
98 onChange={onFormChange}
99 />
100 </FullNameWrapper>
101 <Input
102 disabled={!isEditActivated}
103 name='mobile'
104 placeholder='Телефонски број'
105 value={data.mobile}
106 onChange={onFormChange}
107 InputProps={{
108 startAdornment: <PhoneIcon />,
109 }}
110 />
111 <Input
112 disabled={!isEditActivated}
113 name='email'
114 placeholder='Емаил адреса'
115 value={data.email}
116 onChange={onFormChange}
117 InputProps={{
118 startAdornment: <PersonIcon />,
119 }}
120 />
121 <Input
122 disabled={!isEditActivated}
123 name='password'
124 placeholder='Лозинка'
125 value={password}
126 onChange={(e) => setPassword(e.target.value)}
127 InputProps={{
128 startAdornment: (
129 <IconButton
130 style={{ padding: 0 }}
131 onClick={() =>
132 setIsPasswordVisible(
133 !isPasswordVisible && isEditActivated
134 )
135 }
136 >
137 {isPasswordVisible ? (
138 <LockOpenIcon />
139 ) : (
140 <LockIcon />
141 )}
142 </IconButton>
143 ),
144 }}
145 type={
146 isPasswordVisible && isEditActivated
147 ? 'text'
148 : 'password'
149 }
150 />
151 </CredentialsWrapper>
152 {isEditActivated ? (
153 <ButtonsWrapper>
154 <CancelButton onClick={handleOnCancel}>Откажи</CancelButton>
155 <SaveButton onClick={handleOnSave}>Сочувај</SaveButton>
156 </ButtonsWrapper>
157 ) : (
158 <EditButton onClick={handleEditButton}>Уреди</EditButton>
159 )}
160 </Wrapper>
161 );
162};
163
164export default MyProfile;
Note: See TracBrowser for help on using the repository browser.