useLogout Hook

PHOTO EMBED

Wed Oct 28 2020 12:33:58 GMT+0000 (Coordinated Universal Time)

Saved by @Tony Dung #javascript

import { useCallback, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';

/**
 * @description Get me API hook. Perform the get me API call when component mounted
 * @returns {Object}
 */
const useLogout = () => {
  const dispatch = useDispatch();
  const token = useSelector(selectAccessToken());
  const Loading = useRef(GlobalLib.Loading.get());
  const callLogout = useCallback(async () => {
    try {
      Loading.current.show();
      await ApiLib.authApi.postApiV1AuthLogout(
        UtilLib.getAuthorizationHeaders(token),
      );
      dispatch(updateAccessToken(null));
      dispatch(updateUser({}));
      NavigationServiceLib.reset(screens.Launch);
    } catch (error) {
    } finally {
      Loading.current.hide();
    }
  }, [dispatch, token]);
  return {
    logout: callLogout,
  };
};
content_copyCOPY

Call this hook to get the logout function