From f57989a84f7fda7c9994ef14610d161c9a52bc66 Mon Sep 17 00:00:00 2001 From: Maximilian Harz Date: Fri, 12 Sep 2025 14:28:11 +0200 Subject: [PATCH] Avoid double redeem: --- lib/src/Components/Map/hooks/useRedeemInvite.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/src/Components/Map/hooks/useRedeemInvite.ts b/lib/src/Components/Map/hooks/useRedeemInvite.ts index 1792b2f8..7a403848 100644 --- a/lib/src/Components/Map/hooks/useRedeemInvite.ts +++ b/lib/src/Components/Map/hooks/useRedeemInvite.ts @@ -1,10 +1,11 @@ -import { useEffect } from 'react' +import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' import { useMyProfile } from './useMyProfile' import type { InviteApi } from '#types/InviteApi' +import type { Item } from '#types/Item' export const useRedeemInvite = (inviteApi: InviteApi) => { const inviteCode = localStorage.getItem('inviteCode') @@ -12,10 +13,10 @@ export const useRedeemInvite = (inviteApi: InviteApi) => { const { myProfile } = useMyProfile() const navigate = useNavigate() - useEffect(() => { - async function redeemInvite() { - if (!inviteCode || !myProfile) return + const [isRedeemingDone, setRedeemingDone] = useState(false) + useEffect(() => { + async function redeemInvite(inviteCode: string, myProfile: Item) { const invitingProfileId = await inviteApi.redeemInvite(inviteCode, myProfile.id) if (invitingProfileId) { @@ -27,6 +28,8 @@ export const useRedeemInvite = (inviteApi: InviteApi) => { } } - void redeemInvite() - }, [inviteApi, inviteCode, myProfile, navigate]) + if (!inviteCode || !myProfile || isRedeemingDone) return + void redeemInvite(inviteCode, myProfile) + setRedeemingDone(true) + }, [inviteApi, inviteCode, isRedeemingDone, myProfile, navigate]) }