diff --git a/lib/src/Components/Onboarding/InvitePage.tsx b/lib/src/Components/Onboarding/InvitePage.tsx index bc5aa5a2..0e9d04a5 100644 --- a/lib/src/Components/Onboarding/InvitePage.tsx +++ b/lib/src/Components/Onboarding/InvitePage.tsx @@ -28,22 +28,16 @@ export function InvitePage({ inviteApi, itemsApi }: Props) { if (!id) throw new Error('Invite ID is required') const [invitingProfile, setInvitingProfile] = useState(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 = () => {