From 5fc80b833cb7f0a452d56ee2e0176bd22a752faf Mon Sep 17 00:00:00 2001 From: number-nine Date: Tue, 21 Nov 2023 21:36:41 +0200 Subject: [PATCH] errors on logout --- src/components/App/App.jsx | 52 +++++++++---------- src/components/PrivateRoute/PrivateRoute.jsx | 4 +- .../SectionTemplateNoAuth.styled.js | 2 +- src/redux/auth/authSlice.js | 8 +++ 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/components/App/App.jsx b/src/components/App/App.jsx index dd6e4e25..fda6f76e 100644 --- a/src/components/App/App.jsx +++ b/src/components/App/App.jsx @@ -31,38 +31,38 @@ import { selectMealsIsLoading } from '../../redux/selectors.js'; function App() { const location = useLocation(); const dispatch = useDispatch(); - const { isRefreshing } = useAuth(); + const { isRefreshing, isLoggedIn } = useAuth(); const { profile } = useSelector((state) => state.profile); - const auth = useSelector(selectUserAuthenticated); + // const auth = useSelector(selectUserAuthenticated); const workoutIsLoading = useSelector(selectWorkoutsIsLoading); const profileIsLoading = useSelector(selectUserProfileIsLoading); const productIsLoading = useSelector(selectProductsIsLoading); const mealsIsLoading = useSelector(selectMealsIsLoading); - let isFilled = true; - - if (auth) { - const profileArray = profile - ? [ - profile.height, - profile.currentWeight, - profile.desiredWeight, - profile.blood, - profile.sex, - profile.levelActivity, - profile.birthday, - ] - : []; - - for (let item of profileArray) { - if (item) { - isFilled = true; - } else { - isFilled = false; - break; - } - } - } + let isFilled = isLoggedIn && profile ? true : false; + + // if (isLoggedIn && profile) { + // const profileArray = profile + // ? [ + // profile.height, + // profile.currentWeight, + // profile.desiredWeight, + // profile.blood, + // profile.sex, + // profile.levelActivity, + // profile.birthday, + // ] + // : []; + + // for (let item of profileArray) { + // if (item) { + // isFilled = true; + // } else { + // isFilled = false; + // break; + // } + // } + // } useEffect(() => { dispatch(refreshUser()); diff --git a/src/components/PrivateRoute/PrivateRoute.jsx b/src/components/PrivateRoute/PrivateRoute.jsx index 44f66b6d..a32e5394 100644 --- a/src/components/PrivateRoute/PrivateRoute.jsx +++ b/src/components/PrivateRoute/PrivateRoute.jsx @@ -3,8 +3,8 @@ import { useAuth } from '../../hooks/useAuth'; const PrivateRoute = ({ component: Component, redirectTo = '/signin' }) => { const { isLoggedIn, isRefreshing } = useAuth(); - const shouldRedirect = !isLoggedIn && !isRefreshing; - return shouldRedirect ? : Component; + const shouldRedirect = !isLoggedIn ; + return shouldRedirect ? : Component; }; export default PrivateRoute; diff --git a/src/components/SectionTemplateNoAuth/SectionTemplateNoAuth.styled.js b/src/components/SectionTemplateNoAuth/SectionTemplateNoAuth.styled.js index b9d0b046..d4fca7a9 100644 --- a/src/components/SectionTemplateNoAuth/SectionTemplateNoAuth.styled.js +++ b/src/components/SectionTemplateNoAuth/SectionTemplateNoAuth.styled.js @@ -8,7 +8,7 @@ import welcomeMobile2x from '../../assets/backgroundImages/welcome-mobile@2x.jpg import signMobile from '../../assets/backgroundImages/sign-mobile.jpg'; import signMobile2x from '../../assets/backgroundImages/sign-mobile@2x.jpg'; -const getBackgroundImage = (pathname, highResolution = false) => { +const getBackgroundImage = (pathname="", highResolution = false) => { if (pathname.includes('/welcome')) { return `url(${highResolution ? welcomeMobile2x : welcomeMobile})`; } else if (pathname.includes('/signup') || pathname.includes('/signin')) { diff --git a/src/redux/auth/authSlice.js b/src/redux/auth/authSlice.js index 3cfdf1f4..f05616f4 100644 --- a/src/redux/auth/authSlice.js +++ b/src/redux/auth/authSlice.js @@ -41,6 +41,14 @@ const authSlice = createSlice({ state.error = null; }) // ---------------Log Out------------------ + .addCase(logOutUser.pending, (state) => { + state.isLoggedIn = false; + state.isLoading = true; + state.authenticated = false; + state.userData = null; + state.token = null; + state.error = null; + }) .addCase(logOutUser.fulfilled, (state) => { state.isLoggedIn = false; state.isLoading = false;