From ac747014e51d197d04a8a7f9b1d6a4d006350f32 Mon Sep 17 00:00:00 2001 From: Nishan Date: Tue, 14 Nov 2023 15:54:14 +0530 Subject: [PATCH] fixes redirect --- apps/next/src/pages/_app.tsx | 1 - .../crossmint-purchase-redirect.tsx | 19 ------------ .../creator-token/buy-creator-token.tsx | 29 ++++--------------- .../creator-token/creator-tokens-share.tsx | 21 ++++++++++++-- .../creator-token-social-share.tsx | 2 +- 5 files changed, 25 insertions(+), 47 deletions(-) delete mode 100644 apps/next/src/pages/creator-token/crossmint-purchase-redirect.tsx diff --git a/apps/next/src/pages/_app.tsx b/apps/next/src/pages/_app.tsx index c22c2e428..48caa6252 100644 --- a/apps/next/src/pages/_app.tsx +++ b/apps/next/src/pages/_app.tsx @@ -231,7 +231,6 @@ function App({ Component, pageProps, router }: AppProps) { - diff --git a/apps/next/src/pages/creator-token/crossmint-purchase-redirect.tsx b/apps/next/src/pages/creator-token/crossmint-purchase-redirect.tsx deleted file mode 100644 index 00f98c69f..000000000 --- a/apps/next/src/pages/creator-token/crossmint-purchase-redirect.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import { useEffect } from "react"; - -export default function CrossmintPurchaseRedirect() { - useEffect(() => { - const url = new URL(window.location.href); - if (url.searchParams.get("p")) { - const crossmintRes = JSON.parse(url.searchParams.get("p")); - if ( - crossmintRes && - crossmintRes[0] && - crossmintRes[0].type === "purchase.succeeded" - ) { - window.opener.postMessage(crossmintRes[0]); - window.close(); - } - } - }, []); - return
hi
; -} diff --git a/packages/app/components/creator-token/buy-creator-token.tsx b/packages/app/components/creator-token/buy-creator-token.tsx index 6ff9f795c..fb091a68b 100644 --- a/packages/app/components/creator-token/buy-creator-token.tsx +++ b/packages/app/components/creator-token/buy-creator-token.tsx @@ -248,7 +248,8 @@ export const BuyCreatorToken = () => { process.env.NEXT_PUBLIC_STAGE === "production" ? "production" : "staging", successCallbackURL: typeof window !== "undefined" - ? window.location.origin + `/creator-token/crossmint-purchase-redirect` + ? window.location.origin + + `/creator-token/${profileData?.data?.profile.username}/share` : undefined, } as const; @@ -330,7 +331,7 @@ export const BuyCreatorToken = () => { height={44} /> )} - {crossmintConfig.collectionId && selectedAction === "buy" ? ( + {selectedAction === "buy" ? ( {(priceToBuyNext.isLoading && paymentMethod === "USDC") || (ethPriceToBuyNext.isLoading && @@ -469,7 +470,7 @@ export const BuyCreatorToken = () => { - {selectedAction === "buy" ? ( + {selectedAction === "buy" && crossmintConfig.collectionId ? ( <> { e.preventDefault(); return; } - - function listenCrossmintMessage(event: any) { - if (event.data.type === "purchase.succeeded") { - if (profileData?.data?.profile) { - redirectToCreatorTokensShare({ - username: profileData?.data?.profile.username, - type: "collected", - collectedCount: tokenAmount, - }); - router.pop(); - console.log( - `Received message from crossmint window: ${event.data}` - ); - window.removeEventListener( - "message", - listenCrossmintMessage - ); - } - } - } - window.addEventListener("message", listenCrossmintMessage); + router.pop(); }} {...crossmintConfig} /> diff --git a/packages/app/components/creator-token/creator-tokens-share.tsx b/packages/app/components/creator-token/creator-tokens-share.tsx index 7a9084d15..8936e571a 100644 --- a/packages/app/components/creator-token/creator-tokens-share.tsx +++ b/packages/app/components/creator-token/creator-tokens-share.tsx @@ -45,13 +45,30 @@ const { useParam } = createParam<{ username: string; type: TokenShareType; collectedCount: string; + p: string; }>(); export const CreatorTokensShareModal = memo(function CreatorTokens() { const linearOpaticy = useSharedValue(0); const [username] = useParam("username"); - const [collectedCount] = useParam("collectedCount"); - const [type] = useParam("type"); + let [collectedCount] = useParam("collectedCount"); + let [type] = useParam("type"); + + // Redirect from crossmint + // https://docs.crossmint.com/docs/redirect-url#redirection-payload + const [p] = useParam("p"); + + if (p) { + try { + const params = JSON.parse(p); + if (params && params[0] && params[0].type === "purchase.succeeded") { + collectedCount = params?.[0]?.tokenIds?.length; + type = "collected"; + } + } catch (e) { + // noop + } + } const { data: userInfo } = useUserProfile({ address: username }); const profileData = userInfo?.data?.profile; diff --git a/packages/app/screens/creator-token/creator-token-social-share.tsx b/packages/app/screens/creator-token/creator-token-social-share.tsx index b8542cb2f..ece225caa 100644 --- a/packages/app/screens/creator-token/creator-token-social-share.tsx +++ b/packages/app/screens/creator-token/creator-token-social-share.tsx @@ -6,7 +6,7 @@ export const CreatorTokenSocialShareScreen = withModalScreen( CreatorTokenSocialShare, { title: "", - matchingPathname: "/creator-token/[username]/share", + matchingPathname: "/creator-token/[username]/social-share", matchingQueryParam: "creatorTokenSocialShareModal", snapPoints: ["100%"], enableHandlePanningGesture: true,