ProtectedRoute Update
This commit is contained in:
parent
c94ebd72a2
commit
b3beb64265
|
|
@ -1,14 +1,18 @@
|
||||||
import { JSX, useLayoutEffect } from 'react';
|
import { JSX, useLayoutEffect } from 'react';
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { fetchUserDetails } from '../../store/slices/userSlice';
|
import { fetchUserDetails } from '../../store/slices/userSlice';
|
||||||
import { AppDispatch } from '../../store/store';
|
import { AppDispatch, RootState } from '../../store/store';
|
||||||
|
|
||||||
const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
|
const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
|
||||||
const dispatch = useDispatch<AppDispatch>();
|
const dispatch = useDispatch<AppDispatch>();
|
||||||
|
const user = useSelector((state: RootState) => state.user);
|
||||||
|
|
||||||
// useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
dispatch(fetchUserDetails());
|
// Fetch only if we don't have user data yet
|
||||||
// }, []);
|
if (user.id === null && user.loading === false) {
|
||||||
|
dispatch(fetchUserDetails());
|
||||||
|
}
|
||||||
|
}, [dispatch, user, status]);
|
||||||
|
|
||||||
return children;
|
return children;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ const initialState: UserState = {
|
||||||
language: "",
|
language: "",
|
||||||
account: null,
|
account: null,
|
||||||
picture: undefined,
|
picture: undefined,
|
||||||
loading: true,
|
loading: false,
|
||||||
error: null,
|
error: null,
|
||||||
authorized: false,
|
authorized: false,
|
||||||
accessToken: undefined,
|
accessToken: undefined,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue