AcadeDoc/frontend/src/features/user/user-provider.tsx
Philipinho 2689b267cf sidebar page tree
* frontend and backend implementation
2023-10-15 01:01:17 +01:00

25 lines
656 B
TypeScript

'use client';
import { useAtom } from 'jotai';
import { currentUserAtom } from '@/features/user/atoms/current-user-atom';
import React, { useEffect } from 'react';
import useCurrentUser from '@/features/user/hooks/use-current-user';
export function UserProvider({ children }: React.PropsWithChildren) {
const [, setCurrentUser] = useAtom(currentUserAtom);
const { data, isLoading, error } = useCurrentUser();
useEffect(() => {
if (data && data.user) {
setCurrentUser(data);
}
}, [data, isLoading, setCurrentUser]);
if (isLoading) return <></>;
if (error) {
return <>an error occurred</>;
}
return <>{children}</>;
}