1- import React from 'react' ;
1+ import React , { ReactElement , useEffect , useRef , useState } from 'react' ;
22import {
33 StyleSheet ,
44 View ,
@@ -60,33 +60,25 @@ const styles = StyleSheet.create({
6060 } ,
6161} ) ;
6262
63- interface State {
64- access_token ?: string
65- expires_in ?: number
66- refreshing : boolean
67- localizedFirstName ?: string
68- message ?: string
69- }
70- export default class AppContainer extends React . Component < { } , State > {
71- state = {
72- access_token : undefined ,
73- expires_in : undefined ,
74- refreshing : false ,
75- localizedFirstName : undefined ,
76- message : undefined ,
77- }
63+ export default function AppContainer ( ) : ReactElement {
64+ const [ accessToken , setAccessToken ] = useState < string | undefined > ( ) ;
65+ const [ expiresIn , setExpiresIn ] = useState < any | undefined > ( ) ;
66+ const [ refreshing , setRefreshing ] = useState < boolean > ( false ) ;
67+ const [ localizedFirstName , setLocalizedFirstName ] = useState < string > ( ) ;
68+ const [ message , setMessage ] = useState < string | undefined > ( ) ;
69+
7870
79- modal = React . createRef < LinkedInModal > ( )
71+ const modal = useRef < any > ( ) ;
8072
81- constructor ( props : any ) {
82- super ( props )
73+ useEffect ( ( ) => {
8374 StatusBar . setHidden ( true )
84- }
75+ } , [ ] ) ;
76+
8577
86- getUser = async ( data : LinkedInToken ) => {
78+ const getUser = async ( data : LinkedInToken ) => {
8779 const { access_token, authentication_code} = data ;
8880 if ( ! authentication_code ) {
89- this . setState ( { refreshing : true } ) ;
81+ setRefreshing ( true ) ;
9082
9183 const response = await fetch ( 'https://api.linkedin.com/v2/me' , {
9284 method : 'GET' ,
@@ -95,13 +87,17 @@ export default class AppContainer extends React.Component<{}, State> {
9587 } ,
9688 } ) ;
9789 const payload = await response . json ( ) ;
98- this . setState ( { ...payload , refreshing : false } ) ;
90+ setAccessToken ( payload . access_token ) ;
91+ setExpiresIn ( payload ?. expires_in ) ;
92+ setLocalizedFirstName ( payload ?. localizedFirstName ) ;
93+ setMessage ( payload ?. message ) ;
94+ setRefreshing ( false ) ;
9995 } else {
10096 alert ( `authentication_code = ${ authentication_code } ` ) ;
10197 }
10298 }
10399
104- renderItem ( label : string , value : string ) {
100+ const renderItem = ( label : string , value : string ) : ReactElement => {
105101 return value ? (
106102 < View style = { styles . item } >
107103 < View style = { styles . labelContainer } >
@@ -112,47 +108,44 @@ export default class AppContainer extends React.Component<{}, State> {
112108 < Text style = { styles . value } > { value } </ Text >
113109 </ View >
114110 </ View >
115- ) : null ;
111+ ) : < > </ > ;
116112 }
117113
118- signOut = ( ) => {
119- this . setState ( { refreshing : true } ) ;
120- this . modal . current
121- ?. logoutAsync ( )
122- . then ( ( ) =>
123- this . setState ( { localizedFirstName : undefined , refreshing : false } ) ,
124- ) ;
114+ const signOut = ( ) => {
115+ setRefreshing ( true ) ;
116+ modal . current ?. logoutAsync ( )
117+ . then ( ( ) => {
118+ setLocalizedFirstName ( undefined ) ;
119+ setRefreshing ( false ) ;
120+ } ) ;
125121 }
126122
127- render ( ) {
128- const { refreshing, localizedFirstName} = this . state ;
129- return (
130- < View style = { styles . container } >
131- < View style = { styles . linkedInContainer } >
132- < LinkedInModal
133- ref = { this . modal }
134- clientID = { CLIENT_ID }
135- clientSecret = { CLIENT_SECRET }
136- redirectUri = { REDIRECT_URL }
137- onSuccess = { this . getUser }
138- />
139- < Button
140- title = "Open from external"
141- onPress = { ( ) => this . modal . current ?. open ( ) }
142- />
143- </ View >
123+ return (
124+ < View style = { styles . container } >
125+ < View style = { styles . linkedInContainer } >
126+ < LinkedInModal
127+ ref = { modal }
128+ clientID = { CLIENT_ID }
129+ clientSecret = { CLIENT_SECRET }
130+ redirectUri = { REDIRECT_URL }
131+ onSuccess = { getUser }
132+ />
133+ < Button
134+ title = "Open from external"
135+ onPress = { ( ) => { modal . current ?. open ( ) } }
136+ />
137+ </ View >
144138
145- { refreshing && < ActivityIndicator size = "large" /> }
139+ { refreshing && < ActivityIndicator size = "large" /> }
146140
147- { localizedFirstName && (
148- < >
149- < View style = { styles . userContainer } >
150- { this . renderItem ( 'Last name' , localizedFirstName ) }
151- </ View >
152- < Button title = "Log Out" onPress = { this . signOut } />
153- </ >
154- ) }
155- </ View >
156- ) ;
157- }
141+ { localizedFirstName && (
142+ < >
143+ < View style = { styles . userContainer } >
144+ { renderItem ( 'Last name' , localizedFirstName ) }
145+ </ View >
146+ < Button title = "Log Out" onPress = { signOut } />
147+ </ >
148+ ) }
149+ </ View >
150+ ) ;
158151}
0 commit comments