From 7b303a2b7f1cf1c77d550965e0af8620fa315f93 Mon Sep 17 00:00:00 2001 From: Nicholas Chiang Date: Sat, 15 May 2021 21:12:56 -0700 Subject: [PATCH] wip(pages/signup): redirect logged in users to profile Temporary fix for #181 that clears any edits on the signup page due to SWR revalidating the global user object. Instead of allowing users to edit their profiles directly on the org signup page, TB now just redirects them to their profile pages. --- pages/[org]/signup.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pages/[org]/signup.tsx b/pages/[org]/signup.tsx index 119a749e..bec3d064 100644 --- a/pages/[org]/signup.tsx +++ b/pages/[org]/signup.tsx @@ -1,8 +1,8 @@ import { ParsedUrlQuery } from 'querystring'; import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next'; +import Router, { useRouter } from 'next/router'; import { useEffect, useState } from 'react'; -import { useRouter } from 'next/router'; import useTranslation from 'next-translate/useTranslation'; import { AspectHeader, EmptyHeader } from 'components/navigation'; @@ -15,6 +15,7 @@ import { PageProps, getPageProps } from 'lib/page'; import { OrgContext } from 'lib/context/org'; import { db } from 'lib/api/firebase'; import usePage from 'lib/hooks/page'; +import { useUser } from 'lib/context/user'; import { withI18n } from 'lib/intl'; import common from 'locales/en/common.json'; @@ -42,6 +43,18 @@ function SignupPage({ org, ...props }: SignupPageProps): JSX.Element { }); }, [org, query]); + // Redirect to the user's profile page if they're logged in. Temporary fix for + // the revalidation problem that would clear any edits on this signup page. + // @see {@link https://github.com/tutorbookapp/tutorbook/issues/181} + const { loggedIn } = useUser(); + useEffect(() => { + void Router.prefetch('/profile'); + }, []); + useEffect(() => { + if (!loggedIn) return; + void Router.replace('/profile'); + }, [loggedIn]); + return (