From ad2d7ace88956ca89ef16379b4962ddbc725ed63 Mon Sep 17 00:00:00 2001 From: seeleng Date: Wed, 25 Sep 2024 16:15:56 +0800 Subject: [PATCH] fix: onboarding race condition loading session and template check just write to user after editing categories --- frontend/app/(authenticated)/onboarding/page.tsx | 4 +++- frontend/app/(authenticated)/template.tsx | 16 +++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/frontend/app/(authenticated)/onboarding/page.tsx b/frontend/app/(authenticated)/onboarding/page.tsx index 6afd09c9..7649c1dc 100644 --- a/frontend/app/(authenticated)/onboarding/page.tsx +++ b/frontend/app/(authenticated)/onboarding/page.tsx @@ -17,6 +17,7 @@ export default function Onboarding() { const { data: categories, isLoading } = useQuery(getCategories()); const [categoryIds, setCategoryIds] = useState([]); const user = useUserStore((state) => state.user); + const setLoggedIn = useUserStore((state) => state.setLoggedIn); const toggleCategory = (id: number) => { if (!categoryIds.includes(id)) { @@ -33,7 +34,8 @@ export default function Onboarding() { updateProfileMutation.mutate( { categoryIds }, { - onSuccess: () => { + onSuccess: (data) => { + setLoggedIn(data.data!); router.push("/"); }, }, diff --git a/frontend/app/(authenticated)/template.tsx b/frontend/app/(authenticated)/template.tsx index 135fe400..d4e7f70e 100644 --- a/frontend/app/(authenticated)/template.tsx +++ b/frontend/app/(authenticated)/template.tsx @@ -10,17 +10,19 @@ export default function RedirectIfNotAuthenticated({ children, }: Readonly<{ children: React.ReactNode }>) { const router = useRouter(); - const { status } = useQuery(getUserProfile()); + const { fetchStatus } = useQuery(getUserProfile()); const isLoggedIn = useUserStore((state) => state.isLoggedIn); const user = useUserStore((state) => state.user); - console.log({ isLoggedIn, status }); - if (!isLoggedIn && !status) { - router.push("/login"); - } - if (isLoggedIn && !user!.categories.length) { - router.push("/onboarding"); + if (fetchStatus !== "fetching") { + if (!isLoggedIn) { + router.push("/login"); + } + console.log("fetching", user); + if (isLoggedIn && !user!.categories.length) { + router.push("/onboarding"); + } } return {children};