handle sign up race conditions with more hooks
This commit is contained in:
parent
895c2076e1
commit
f88be8b66b
|
@ -2,7 +2,7 @@ import { LinkIcon } from '@heroicons/react/solid'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { PrivateUser, User } from 'common/user'
|
import { PrivateUser, User } from 'common/user'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { MouseEventHandler, ReactNode, useState } from 'react'
|
import { MouseEventHandler, ReactNode, useEffect, useState } from 'react'
|
||||||
|
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
import { Button } from 'web/components/button'
|
import { Button } from 'web/components/button'
|
||||||
|
@ -17,11 +17,7 @@ import { Title } from 'web/components/title'
|
||||||
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
||||||
import { useTracking } from 'web/hooks/use-tracking'
|
import { useTracking } from 'web/hooks/use-tracking'
|
||||||
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
||||||
import {
|
import { firebaseLogin, updatePrivateUser } from 'web/lib/firebase/users'
|
||||||
firebaseLogin,
|
|
||||||
getUserAndPrivateUser,
|
|
||||||
updatePrivateUser,
|
|
||||||
} from 'web/lib/firebase/users'
|
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import {
|
import {
|
||||||
getDockURLForUser,
|
getDockURLForUser,
|
||||||
|
@ -40,23 +36,32 @@ function ButtonGetStarted(props: {
|
||||||
const { user, privateUser, buttonClass, spinnerClass } = props
|
const { user, privateUser, buttonClass, spinnerClass } = props
|
||||||
|
|
||||||
const [isLoading, setLoading] = useState(false)
|
const [isLoading, setLoading] = useState(false)
|
||||||
|
|
||||||
const needsRelink =
|
const needsRelink =
|
||||||
privateUser?.twitchInfo?.twitchName &&
|
privateUser?.twitchInfo?.twitchName &&
|
||||||
privateUser?.twitchInfo?.needsRelinking
|
privateUser?.twitchInfo?.needsRelinking
|
||||||
|
|
||||||
|
const [waitingForUser, setWaitingForUser] = useState(false)
|
||||||
|
useEffect(() => {
|
||||||
|
if (waitingForUser && user && privateUser) {
|
||||||
|
setWaitingForUser(false)
|
||||||
|
|
||||||
|
if (privateUser.twitchInfo?.twitchName) return // If we've already linked Twitch, no need to do so again
|
||||||
|
|
||||||
|
setLoading(true)
|
||||||
|
|
||||||
|
linkTwitchAccountRedirect(user, privateUser).then(() => {
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [user, privateUser, waitingForUser])
|
||||||
|
|
||||||
const callback =
|
const callback =
|
||||||
user && privateUser
|
user && privateUser
|
||||||
? () => linkTwitchAccountRedirect(user, privateUser)
|
? () => linkTwitchAccountRedirect(user, privateUser)
|
||||||
: async () => {
|
: async () => {
|
||||||
const result = await firebaseLogin()
|
await firebaseLogin()
|
||||||
|
setWaitingForUser(true)
|
||||||
const userId = result.user.uid
|
|
||||||
const { user, privateUser } = await getUserAndPrivateUser(userId)
|
|
||||||
if (!user || !privateUser) return
|
|
||||||
|
|
||||||
if (privateUser.twitchInfo?.twitchName) return // If we've already linked Twitch, no need to do so again
|
|
||||||
|
|
||||||
await linkTwitchAccountRedirect(user, privateUser)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getStarted = async () => {
|
const getStarted = async () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user