From 62b38ac454ae1a1d941bd304a23739cf3b22fea7 Mon Sep 17 00:00:00 2001 From: mohitb35 <44917347+mohitb35@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:43:41 +0530 Subject: [PATCH] feat: remove support link from query params when not applicable --- pages/index.tsx | 67 +++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index 4ef741f4..b26a6311 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -409,34 +409,51 @@ export const getServerSideProps: GetServerSideProps = async (context) => { if (typeof context.query.utm_source === "string") utmSource = context.query.utm_source; - // Set gift details if there is s (support link) in the query params - if ( - projectDetails !== null && - projectDetails.category !== "membership" && - !NON_GIFTABLE_PROJECT_PURPOSES.includes(projectDetails.purpose) && - context.query.s - ) { - try { - const requestParams = { - url: `/app/profiles/${context.query.s}`, - setshowErrorCard, - tenant, - locale, + // Handle s (support link) in the query params + if (typeof context.query.s === "string" && context.query.s.length > 0) { + if ( + projectDetails === null || + projectDetails.category === "membership" || + NON_GIFTABLE_PROJECT_PURPOSES.includes(projectDetails.purpose) + ) { + // If project cannot have direct gift, remove 's' parameter by redirecting + const pathname = context.resolvedUrl.split("?")[0]; + const query = { ...context.query }; + delete query.s; + const queryString = new URLSearchParams( + query as Record + ).toString(); + + return { + redirect: { + destination: `${pathname}${queryString ? `?${queryString}` : ""}`, + permanent: true, + }, }; - const newProfile = await apiRequest(requestParams); - if (newProfile.data.type !== "tpo") { - isGift = true; - giftDetails = { - recipientName: newProfile.data.displayName, - recipientEmail: "", - message: "", - type: "direct", - recipient: newProfile.data.id, - recipientTreecounter: newProfile.data.slug, + } else { + // Set gift details if there is s (support link) in the query params for an eligible project + try { + const requestParams = { + url: `/app/profiles/${context.query.s}`, + setshowErrorCard, + tenant, + locale, }; + const newProfile = await apiRequest(requestParams); + if (newProfile.data.type !== "tpo") { + isGift = true; + giftDetails = { + recipientName: newProfile.data.displayName, + recipientEmail: "", + message: "", + type: "direct", + recipient: newProfile.data.id, + recipientTreecounter: newProfile.data.slug, + }; + } + } catch (err) { + console.log("Error", err); } - } catch (err) { - console.log("Error", err); } }