Skip to content

Commit

Permalink
fix: add onboarding check to appDir pages (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnugent authored Feb 13, 2024
1 parent 4dc3f41 commit 862760a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/(default)/components/PageFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Logo, LogoSize } from '../header'
*/
export const PageFooter: React.FC = () => {
return (
<footer className='relative footer p-10 bg-base-200 bg-base-content text-base-100'>
<footer className='relative footer p-10 bg-base-content text-base-100'>
<aside>
<div className='border-2 border-accent py-3 pl-2 pr-4 rounded-full'><Logo size={LogoSize.md} className='fill-accent' /></div>
<p><span className='font-semibold text-lg'>OpenBeta</span><br /><span className='tracking-tight font-sm'>Free climbing database built & run by climbers.</span></p>
Expand Down
2 changes: 2 additions & 0 deletions src/app/(default)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PageFooter } from './components/PageFooter'
import { NextAuthProvider } from '@/components/auth/NextAuthProvider'
import { ReactToastifyProvider } from './components/ReactToastifyProvider'
import { BlockingAlertUploadingInProgress } from './components/ui/GlobalAlerts'
import { OnboardingCheck } from '@/components/auth/OnboardingCheck'

export const metadata: Metadata = {
title: 'OpenBeta',
Expand Down Expand Up @@ -33,6 +34,7 @@ export default function RootLayout ({
<div>
{children}
</div>
<OnboardingCheck />
</NextAuthProvider>
<PageFooter />
<ReactToastifyProvider />
Expand Down
2 changes: 2 additions & 0 deletions src/app/(maps)/components/ProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { SessionProvider } from 'next-auth/react'
import { House } from '@phosphor-icons/react/dist/ssr'
import AuthenticatedProfileNavButton from '@/components/AuthenticatedProfileNavButton'
import { OnboardingCheck } from '@/components/auth/OnboardingCheck'

export const ProfileMenu: React.FC = () => {
return (
Expand All @@ -12,6 +13,7 @@ export const ProfileMenu: React.FC = () => {
<AuthenticatedProfileNavButton isMobile={false} />
</nav>
</div>
<OnboardingCheck />
</SessionProvider>
)
}
14 changes: 14 additions & 0 deletions src/components/auth/OnboardingCheck.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client'
import useUsernameCheck, { useUsernameCheckAppDir } from '@/js/hooks/useUsernameCheck'

/**
* A wrapper component to imperatively check if the user has completed onboarding.
*/
export const OnboardingCheck: React.FC<{ isAppDir?: boolean }> = ({ isAppDir = true }) => {
if (isAppDir) {
useUsernameCheckAppDir()
} else {
useUsernameCheck()
}
return null
}
25 changes: 25 additions & 0 deletions src/js/hooks/useUsernameCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from 'react'
import { useRouter as useRouterAppDir, usePathname } from 'next/navigation'
import { useRouter } from 'next/router'
import { useSession } from 'next-auth/react'
import useUserProfileCmd from './useUserProfileCmd'
Expand All @@ -25,3 +26,27 @@ export default function useUsernameCheck (): void {
}
}, [status])
}

/**
* A global hook that forces new users to go to the user name screen. Use this version for pages in the `/app` directory (Next v13).
*/
export function useUsernameCheckAppDir (): void {
const router = useRouterAppDir()
const pathname = usePathname()
const { data, status } = useSession()
const { getUsernameById } = useUserProfileCmd({ accessToken: data?.accessToken as string })

useEffect(() => {
const uuid = data?.user.metadata.uuid
if (pathname.startsWith('/auth/')) {
return
}
if (status === 'authenticated' && uuid != null) {
void getUsernameById({ userUuid: uuid }).then(usernameInfo => {
if (usernameInfo === null) {
void router.push('/account/changeUsername')
}
})
}
}, [status])
}
13 changes: 2 additions & 11 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import 'react-toastify/dist/ReactToastify.min.css'

import '../styles/global.css'
import '../../public/fonts/fonts.css'
import useUsernameCheck from '../js/hooks/useUsernameCheck'
import { useUserGalleryStore } from '../js/stores/useUserGalleryStore'
import { BlockingAlert } from '../components/ui/micro/AlertDialogue'
import { XMarkIcon } from '@heroicons/react/24/outline'
import { OnboardingCheck } from '@/components/auth/OnboardingCheck'

Router.events.on('routeChangeStart', () => NProgress.start())
Router.events.on('routeChangeComplete', () => NProgress.done())
Expand Down Expand Up @@ -42,7 +42,7 @@ export default function MyApp ({ Component, pageProps: { session, ...pageProps }
</>
)
}
<NewUserCheck />
<OnboardingCheck isAppDir={false} />
</SessionProvider>
<ToastContainer
position='bottom-right'
Expand Down Expand Up @@ -83,15 +83,6 @@ const Auth: React.FC<any> = ({ children }) => {
return children
}

/**
* A wrapper component so that we can call the username check hook
* inside SessionProvider.
*/
const NewUserCheck: React.FC = () => {
useUsernameCheck()
return null
}

const ToastCloseButton: React.FC<any> = ({ closeToast }) => (
<button className='self-center btn btn-square btn-outline' onClick={closeToast}>
<XMarkIcon
Expand Down

0 comments on commit 862760a

Please sign in to comment.