@@ -6,15 +6,15 @@ import Card from '../../components/Card/Card';
6
6
import CardBody from '../../components/Card/CardBody' ;
7
7
import Button from '../../components/CustomButtons/Button' ;
8
8
import FormLabel from '@material-ui/core/FormLabel' ;
9
- import { getUser , updateUser , getUserLoggedIn } from '../../services/user' ;
9
+ import { getUser , updateUser , getUserLoggedIn , UserData } from '../../services/user' ;
10
10
import { makeStyles } from '@material-ui/core/styles' ;
11
11
12
12
import { LogoGithubIcon } from '@primer/octicons-react' ;
13
13
import CloseRounded from '@material-ui/icons/CloseRounded' ;
14
14
import { Check , Save } from '@material-ui/icons' ;
15
- import { TextField } from '@material-ui/core' ;
15
+ import { TextField , Theme } from '@material-ui/core' ;
16
16
17
- const useStyles = makeStyles ( ( theme ) => ( {
17
+ const useStyles = makeStyles ( ( theme : Theme ) => ( {
18
18
root : {
19
19
'& .MuiTextField-root' : {
20
20
margin : theme . spacing ( 1 ) ,
@@ -23,56 +23,78 @@ const useStyles = makeStyles((theme) => ({
23
23
} ,
24
24
} ) ) ;
25
25
26
- export default function Dashboard ( ) {
26
+ export default function UserProfile ( ) : React . ReactElement {
27
27
const classes = useStyles ( ) ;
28
- const [ data , setData ] = useState ( [ ] ) ;
29
- const [ auth , setAuth ] = useState ( true ) ;
30
- const [ isLoading , setIsLoading ] = useState ( true ) ;
31
- const [ isError , setIsError ] = useState ( false ) ;
32
- const [ isProfile , setIsProfile ] = useState ( false ) ;
33
- const [ isAdmin , setIsAdmin ] = useState ( false ) ;
34
- const [ gitAccount , setGitAccount ] = useState ( '' ) ;
28
+ const [ data , setData ] = useState < UserData | null > ( null ) ;
29
+ const [ auth , setAuth ] = useState < boolean > ( true ) ;
30
+ const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
31
+ const [ isError , setIsError ] = useState < boolean > ( false ) ;
32
+ const [ isProfile , setIsProfile ] = useState < boolean > ( false ) ;
33
+ const [ isAdmin , setIsAdmin ] = useState < boolean > ( false ) ;
34
+ const [ gitAccount , setGitAccount ] = useState < string > ( '' ) ;
35
35
const navigate = useNavigate ( ) ;
36
- const { id } = useParams ( ) ;
36
+ const { id } = useParams < { id ?: string } > ( ) ;
37
37
38
38
useEffect ( ( ) => {
39
39
if ( id == null ) {
40
40
setIsProfile ( true ) ;
41
41
}
42
42
43
43
if ( id ) {
44
- getUser ( setIsLoading , setData , setAuth , setIsError , id ) ;
44
+ getUser (
45
+ setIsLoading ,
46
+ ( userData : UserData ) => {
47
+ setData ( userData ) ;
48
+ setGitAccount ( userData . gitAccount || '' ) ;
49
+ } ,
50
+ setAuth ,
51
+ setIsError ,
52
+ id ,
53
+ ) ;
45
54
getUserLoggedIn ( setIsLoading , setIsAdmin , setIsError , setAuth ) ;
46
55
} else {
47
56
console . log ( 'getting user data' ) ;
48
57
setIsProfile ( true ) ;
49
- getUser ( setIsLoading , setData , setAuth , setIsError ) ;
58
+ getUser (
59
+ setIsLoading ,
60
+ ( userData : UserData ) => {
61
+ setData ( userData ) ;
62
+ setGitAccount ( userData . gitAccount || '' ) ;
63
+ } ,
64
+ setAuth ,
65
+ setIsError ,
66
+ ) ;
50
67
}
51
- } , [ ] ) ;
68
+ } , [ id ] ) ;
52
69
53
70
if ( isLoading ) return < div > Loading...</ div > ;
54
71
if ( isError ) return < div > Something went wrong ...</ div > ;
55
72
if ( ! auth && window . location . pathname === '/dashboard/profile' ) {
56
73
return < Navigate to = '/login' /> ;
57
74
}
75
+ if ( ! data ) return < div > No user data available</ div > ;
58
76
59
- const updateProfile = async ( ) => {
77
+ const updateProfile = async ( ) : Promise < void > => {
60
78
try {
61
- data . gitAccount = escapeHTML ( gitAccount ) ;
62
- await updateUser ( data ) ;
79
+ const updatedData = {
80
+ ...data ,
81
+ gitAccount : escapeHTML ( gitAccount ) ,
82
+ } ;
83
+ await updateUser ( updatedData ) ;
63
84
navigate ( `/dashboard/profile` ) ;
64
85
} catch {
65
86
setIsError ( true ) ;
66
87
}
67
88
} ;
68
89
69
- const UpdateButton = ( ) => (
90
+ const UpdateButton = ( ) : React . ReactElement => (
70
91
< Button variant = 'outlined' color = 'success' onClick = { updateProfile } >
71
- < Save > </ Save > Update
92
+ < Save />
93
+ Update
72
94
</ Button >
73
95
) ;
74
96
75
- const escapeHTML = ( str ) => {
97
+ const escapeHTML = ( str : string ) : string => {
76
98
return str
77
99
. replace ( / & / g, '&' )
78
100
. replace ( / < / g, '<' )
@@ -104,7 +126,8 @@ export default function Dashboard() {
104
126
width = { '75px' }
105
127
style = { { borderRadius : '5px' } }
106
128
src = { `https://github.com/${ data . gitAccount } .png` }
107
- > </ img >
129
+ alt = { `${ data . displayName } 's GitHub avatar` }
130
+ />
108
131
</ GridItem >
109
132
) }
110
133
< GridItem xs = { 2 } sm = { 2 } md = { 2 } >
@@ -138,7 +161,7 @@ export default function Dashboard() {
138
161
< Check fontSize = 'small' />
139
162
</ span >
140
163
) : (
141
- < CloseRounded color = 'error' > </ CloseRounded >
164
+ < CloseRounded color = 'error' / >
142
165
) }
143
166
</ GridItem >
144
167
</ GridContainer >
@@ -147,7 +170,7 @@ export default function Dashboard() {
147
170
< hr style = { { opacity : 0.2 } } />
148
171
< div style = { { marginTop : '25px' } } >
149
172
< FormLabel component = 'legend' >
150
- What is your < LogoGithubIcon > </ LogoGithubIcon > username?
173
+ What is your < LogoGithubIcon / > username?
151
174
</ FormLabel >
152
175
< div style = { { textAlign : 'right' } } >
153
176
< TextField
@@ -156,7 +179,9 @@ export default function Dashboard() {
156
179
variant = 'outlined'
157
180
placeholder = 'Enter a new GitHub username...'
158
181
value = { gitAccount }
159
- onChange = { ( e ) => setGitAccount ( e . target . value ) }
182
+ onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) =>
183
+ setGitAccount ( e . target . value )
184
+ }
160
185
/>
161
186
< UpdateButton />
162
187
</ div >
0 commit comments