mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2025-12-13 07:46:10 +00:00
Avoid multiple redeems and validations because of hook behavior
This commit is contained in:
parent
3eb6d8a76b
commit
338849c7ab
@ -28,22 +28,16 @@ export function InvitePage({ inviteApi, itemsApi }: Props) {
|
||||
if (!id) throw new Error('Invite ID is required')
|
||||
|
||||
const [invitingProfile, setInvitingProfile] = useState<Item | null>(null)
|
||||
const [isRedeemingDone, setRedeemingDone] = useState(false)
|
||||
const [isValidationDone, setValidationDone] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
async function redeemInvite() {
|
||||
if (!id) throw new Error('Invite ID is required')
|
||||
|
||||
if (!isMyProfileLoaded) return
|
||||
|
||||
if (!myProfile) {
|
||||
toast.error('Could not find your profile to redeem the invite.')
|
||||
return
|
||||
}
|
||||
|
||||
const invitingProfileId = await inviteApi.redeemInvite(id, myProfile.id)
|
||||
async function redeemInvite(id: string, myProfileId: string) {
|
||||
const invitingProfileId = await inviteApi.redeemInvite(id, myProfileId)
|
||||
|
||||
if (invitingProfileId) {
|
||||
toast.success('Invite redeemed successfully!')
|
||||
setRedeemingDone(true)
|
||||
navigate(`/item/${invitingProfileId}`)
|
||||
} else {
|
||||
toast.error('Failed to redeem invite')
|
||||
@ -51,13 +45,12 @@ export function InvitePage({ inviteApi, itemsApi }: Props) {
|
||||
}
|
||||
}
|
||||
|
||||
async function validateInvite() {
|
||||
if (!id) throw new Error('Invite ID is required')
|
||||
|
||||
async function validateInvite(id: string) {
|
||||
const invitingProfileId = await inviteApi.validateInvite(id)
|
||||
|
||||
if (!invitingProfileId) {
|
||||
toast.error('Invalid invite code')
|
||||
setValidationDone(true)
|
||||
navigate('/')
|
||||
return
|
||||
}
|
||||
@ -66,22 +59,35 @@ export function InvitePage({ inviteApi, itemsApi }: Props) {
|
||||
|
||||
if (!invitingProfile) {
|
||||
toast.error('Inviting profile not found')
|
||||
setValidationDone(true)
|
||||
navigate('/')
|
||||
return
|
||||
}
|
||||
|
||||
setInvitingProfile(invitingProfile)
|
||||
setValidationDone(true)
|
||||
}
|
||||
|
||||
if (!id) throw new Error('Invite ID is required')
|
||||
|
||||
if (!isAuthenticationInitialized) return
|
||||
|
||||
if (isAuthenticated) {
|
||||
void redeemInvite()
|
||||
if (!isMyProfileLoaded || isRedeemingDone) return
|
||||
|
||||
if (!myProfile) {
|
||||
toast.error('Could not find your profile to redeem the invite.')
|
||||
} else {
|
||||
void redeemInvite(id, myProfile.id)
|
||||
}
|
||||
setRedeemingDone(true)
|
||||
} else {
|
||||
if (isValidationDone) return
|
||||
|
||||
// Save invite code in local storage
|
||||
localStorage.setItem('inviteCode', id)
|
||||
|
||||
void validateInvite()
|
||||
void validateInvite(id)
|
||||
}
|
||||
}, [
|
||||
id,
|
||||
@ -92,6 +98,8 @@ export function InvitePage({ inviteApi, itemsApi }: Props) {
|
||||
myProfile,
|
||||
isMyProfileLoaded,
|
||||
itemsApi,
|
||||
isRedeemingDone,
|
||||
isValidationDone,
|
||||
])
|
||||
|
||||
const goToSignup = () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user