Get me hooks
Wed Oct 28 2020 12:31:47 GMT+0000 (Coordinated Universal Time)
Saved by
@Tony Dung
#javascript
import { useCallback, useRef, useState } from 'react';
import { useAsync } from 'react-async';
import SplashScreen from 'react-native-splash-screen';
/**
* @description Get me API hook. Perform the get me API call when component mounted
* @returns {Object}
*/
const useGetMe = () => {
const dispatch = useDispatch();
const token = useSelector(selectAccessToken());
const lastCallInstance = useRef(null);
const getUserProfile = useCallback(
async ({ accessToken }) => {
if (lastCallInstance.current) {
clearTimeout(lastCallInstance.current);
}
const cancelToken = setTimeout(async () => {
try {
const { data } = await ApiLib.accountApi.getApiV1AccountsMe(
UtilLib.getAuthorizationHeaders(accessToken),
);
SplashScreen.hide();
dispatch(updateUser(data?.data));
await UtilLib.delay(1000);
NavigationServiceLib.reset(screens.Main);
} catch (error) {
await UtilLib.delay(1000);
SplashScreen.hide();
NavigationServiceLib.reset(screens.Login);
}
}, 1000);
lastCallInstance.current = cancelToken;
},
[dispatch],
);
useAsync({ promiseFn: getUserProfile, accessToken: token, watch: token });
};
content_copyCOPY
Call this hook in the launch screen
Comments