1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , useContext } from 'react' ;
22import { Navigate , useNavigate , useParams } from 'react-router-dom' ;
33import GridItem from '../../components/Grid/GridItem' ;
44import GridContainer from '../../components/Grid/GridContainer' ;
55import Card from '../../components/Card/Card' ;
66import CardBody from '../../components/Card/CardBody' ;
77import Button from '../../components/CustomButtons/Button' ;
88import FormLabel from '@material-ui/core/FormLabel' ;
9- import { getUser , updateUser , getUserLoggedIn } from '../../services/user' ;
9+ import { getUser , updateUser } from '../../services/user' ;
10+ import { UserContext } from '../../../context' ;
1011
1112import { UserData } from '../../../types/models' ;
1213import { makeStyles } from '@material-ui/core/styles' ;
@@ -15,6 +16,7 @@ import { LogoGithubIcon } from '@primer/octicons-react';
1516import CloseRounded from '@material-ui/icons/CloseRounded' ;
1617import { Check , Save } from '@material-ui/icons' ;
1718import { TextField , Theme } from '@material-ui/core' ;
19+ import { UserContextType } from '../RepoDetails/RepoDetails' ;
1820
1921const useStyles = makeStyles ( ( theme : Theme ) => ( {
2022 root : {
@@ -31,42 +33,23 @@ export default function UserProfile(): React.ReactElement {
3133 const [ auth , setAuth ] = useState < boolean > ( true ) ;
3234 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
3335 const [ isError , setIsError ] = useState < boolean > ( false ) ;
34- const [ isProfile , setIsProfile ] = useState < boolean > ( false ) ;
35- const [ isAdmin , setIsAdmin ] = useState < boolean > ( false ) ;
3636 const [ gitAccount , setGitAccount ] = useState < string > ( '' ) ;
3737 const navigate = useNavigate ( ) ;
3838 const { id } = useParams < { id ?: string } > ( ) ;
39+ const { user : loggedInUser } = useContext < UserContextType > ( UserContext ) ;
40+ const isOwnProfile = ! id ;
3941
4042 useEffect ( ( ) => {
41- if ( id == null ) {
42- setIsProfile ( true ) ;
43- }
44-
45- if ( id ) {
46- getUser (
47- setIsLoading ,
48- ( userData : UserData ) => {
49- setData ( userData ) ;
50- setGitAccount ( userData . gitAccount || '' ) ;
51- } ,
52- setAuth ,
53- setIsError ,
54- id ,
55- ) ;
56- getUserLoggedIn ( setIsLoading , setIsAdmin , setIsError , setAuth ) ;
57- } else {
58- console . log ( 'getting user data' ) ;
59- setIsProfile ( true ) ;
60- getUser (
61- setIsLoading ,
62- ( userData : UserData ) => {
63- setData ( userData ) ;
64- setGitAccount ( userData . gitAccount || '' ) ;
65- } ,
66- setAuth ,
67- setIsError ,
68- ) ;
69- }
43+ getUser (
44+ setIsLoading ,
45+ ( userData : UserData ) => {
46+ setData ( userData ) ;
47+ setGitAccount ( userData . gitAccount || '' ) ;
48+ } ,
49+ setAuth ,
50+ setIsError ,
51+ id ,
52+ ) ;
7053 } , [ id ] ) ;
7154
7255 if ( isLoading ) return < div > Loading...</ div > ;
@@ -169,7 +152,7 @@ export default function UserProfile(): React.ReactElement {
169152 ) }
170153 </ GridItem >
171154 </ GridContainer >
172- { isProfile || isAdmin ? (
155+ { isOwnProfile || loggedInUser . admin ? (
173156 < div style = { { marginTop : '50px' } } >
174157 < hr style = { { opacity : 0.2 } } />
175158 < div style = { { marginTop : '25px' } } >
0 commit comments