Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/api/RestAPI/hooks/useIykHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ interface IYKRefStruct {
uid: string;
isValidRef: boolean;
linkedToken: LinkedToken | undefined;
poapEvents: [
{
id: number;
otp: string;
poapEventId: string;
status: 'active' | 'expired' | 'pending-approval' | 'future' | 'rejected';
}
];
poapEvents:
| [
{
id: number;
otp: string;
poapEventId: string;
status: 'active' | 'expired' | 'pending-approval' | 'future' | 'rejected';
}
]
| [];
}

const httpClient = getCustomClient(`https://api.iyk.app/`, {});
Expand All @@ -37,6 +39,7 @@ export function useIYKRefQuery(ref: string | null) {
select: (data) => {
if (data) {
const { isValidRef, linkedToken, poapEvents } = data;

return { isValidRef, linkedToken, poapEvents };
} else return undefined;
},
Expand All @@ -51,7 +54,11 @@ interface MintIykPoapTokenProps {
deviceId?: string | null;
}

export function useMintIYKPOAPToken() {
interface mintIykParams {
onSuccessfullMint?: (data: any) => void;
}

export function useMintIYKPOAPToken({ onSuccessfullMint }: mintIykParams) {
const getIykData = async ({ otpCode, recipient, deviceId, poapEventId }: MintIykPoapTokenProps) => {
if (!otpCode || !recipient || !poapEventId) return undefined;

Expand All @@ -71,5 +78,5 @@ export function useMintIYKPOAPToken() {
return data;
};

return useMutation(['fetchIykData'], getIykData);
return useMutation(['fetchIykData'], getIykData, { onSuccess: onSuccessfullMint });
}
49 changes: 32 additions & 17 deletions src/pages/NimiPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const eventUrlToThemeMapping = {
export default function NimiPage() {
const { nimiUsername: ensName } = useParams();
const [isMounted, setIsMounted] = useState(false);
const [isMinted, setIsMinted] = useState(false);
// const [autoClaimPOAPLocalStorage, setAutoClaimPOAPLocalStorage] = useAtom(autoClaimPOAPAtom);
const [autoClaimPOAP, setAutoClaimPOAP] = useAtom(autoClaimPOAPAtom);
const [autoClaimPOAPRecentReceiver, setAutoClaimPOAPRecentReciever] = useAtom(autoClaimPOAPRecentReceiverAtom);
Expand All @@ -47,10 +48,12 @@ export default function NimiPage() {
const iykRef = searchParams.get('iykRef');
const event = searchParams.get('event');
const { data: refData, isFetching: isRefDataLoading } = useIYKRefQuery(iykRef);
console.log('refData', refData);
const poapEventId = refData?.poapEvents?.[0]?.poapEventId;

// Retrieve the POAP event data and check if the user has already claimed the POAP
const { data: poapEvent, isFetching: isPoapEventLoading } = usePOAPEventQuery(poapEventId);
console.log('poapEvent', poapEvent);

// Fetch the Nimi data for the ENS name
const { data: initialNimi, isGenerated } = useInitialtNimiData({
Expand All @@ -68,8 +71,12 @@ export default function NimiPage() {
account: debouncedPOAPReciever,
enabled: !!debouncedPOAPReciever,
});

const onSuccessfullMint = () => {
setIsMinted(true);
};
// Mint the POAP token
const { mutateAsync: mintIYKPOAPToken } = useMintIYKPOAPToken();
const { mutateAsync: mintIYKPOAPToken } = useMintIYKPOAPToken({ onSuccessfullMint });

// Debugging
console.log({
Expand All @@ -86,28 +93,36 @@ export default function NimiPage() {
setClaimStep(ClaimModalState.CLAIMED);
return;
}
try {
const mintResponse = await mintIYKPOAPToken({
otpCode: refData?.poapEvents[0].otp,
recipient: debouncedPOAPReciever,
poapEventId: poapEventId!,
});
console.log({ mintResponse });

if (mintResponse.success) {
setClaimStep(ClaimModalState.SUCCESS);
// If the user wants to auto claim the POAP, save the reciever address to local storage
setAutoClaimPOAPRecentReciever(debouncedPOAPReciever);
} else {

if (refData && refData.poapEvents && refData.poapEvents[0] && !isMinted) {
// check if these are defined
try {
const mintResponse = await mintIYKPOAPToken({
otpCode: refData.poapEvents[0].otp,
recipient: debouncedPOAPReciever,
poapEventId: poapEventId!,
});
console.log({ mintResponse });

if (mintResponse.success) {
setClaimStep(ClaimModalState.SUCCESS);
// If the user wants to auto claim the POAP, save the reciever address to local storage
setAutoClaimPOAPRecentReciever(debouncedPOAPReciever);
} else {
setClaimStep(ClaimModalState.ERROR);
}
} catch (error) {
console.error(error);
setClaimStep(ClaimModalState.ERROR);
}
} catch (error) {
console.error(error);
setClaimStep(ClaimModalState.ERROR);
} else {
console.error('refData or refData.poapEvents[0] is not defined.');
return;
}

return;
};

useEffect(() => {
// On first render, attempt to claim the POAP if the user has auto claim enabled
const attemptClaim = async () => {
Expand Down