All files / lib/providers UserInfoProvider.tsx

0% Statements 0/15
0% Branches 0/6
0% Functions 0/3
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                                                 
'use client';
 
import { useGetMeQuery } from '@lib/api/queries';
import { setLocalStorage } from '@lib/utils/localStorage';
import { useRouter } from 'next/navigation';
import React from 'react';
 
const UserInfoContext = React.createContext<Record<string, string> | null>(null);
 
export const useUserInfoContext = () => React.useContext(UserInfoContext);
 
export default function UserInfoProvider({ children }: React.PropsWithChildren) {
  const router = useRouter();
 
  const { data } = useGetMeQuery();
 
  React.useEffect(() => {
    if (data) {
      const { name, email, role } = data;
      setLocalStorage('name', name);
      setLocalStorage('email', email);
      setLocalStorage('role', role);
      setLocalStorage('profileImageUrl', data.profileImage);
 
      if (email === 'realtreasurehunter1@gmail.com' || email === 'realtreasurehunter2@gmail.com') {
        router.push('/userRewards');
      }
    }
  }, [data]);
 
  return <UserInfoContext.Provider value={data}>{children}</UserInfoContext.Provider>;
}