all repos — caroster @ 3c0bbee900166b5b841ef12b7b71dfc5330de1f8

[Octree] Group carpool to your event https://caroster.io

frontend/hooks/useProfile.ts (view raw)

 1import {useEffect, useState} from 'react';
 2import useAuthStore from '../stores/useAuthStore';
 3import {useProfileLazyQuery} from '../generated/graphql';
 4
 5const useProfile = () => {
 6  const token = useAuthStore(s => s.token);
 7  const user = useAuthStore(s => s.user);
 8  const [isReady, setIsReady] = useState(false);
 9  const [
10    fetchProfile,
11    {data: {me: {profile = null} = {}} = {}},
12  ] = useProfileLazyQuery({
13    onCompleted: () => setIsReady(true),
14  });
15
16  useEffect(() => {
17    if (profile) setIsReady(true);
18    else if (token) fetchProfile();
19    else setIsReady(true);
20  }, [token, profile]);
21
22  return {
23    profile,
24    connected: !!token,
25    user: user,
26    isReady,
27  };
28};
29
30export default useProfile;