From 785a08ba2ca5a0a780faca45b038befc231cd06c Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Mon, 5 Jun 2023 15:20:34 +0530 Subject: [PATCH] Fix duplicate query param missing issue (#9328) --- apps/web/lib/hooks/useRouterQuery.ts | 20 ++++++++++++++++++- .../pages/routing-link/[...appPages].tsx | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/apps/web/lib/hooks/useRouterQuery.ts b/apps/web/lib/hooks/useRouterQuery.ts index 155d991cd0..72d1072fcc 100644 --- a/apps/web/lib/hooks/useRouterQuery.ts +++ b/apps/web/lib/hooks/useRouterQuery.ts @@ -5,7 +5,25 @@ export default function useRouterQuery(name: T) { const existingQueryParams = router.asPath.split("?")[1]; const urlParams = new URLSearchParams(existingQueryParams); - const query = Object.fromEntries(urlParams); + const query: Record = {}; + // Following error is thrown by Typescript: + // 'Type 'URLSearchParams' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher' + // We should change the target to higher ES2019 atleast maybe + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + for (const [key, value] of urlParams) { + if (!query[key]) { + query[key] = value; + } else { + let queryValue = query[key]; + if (queryValue instanceof Array) { + queryValue.push(value); + } else { + queryValue = query[key] = [queryValue]; + queryValue.push(value); + } + } + } const setQuery = (newValue: string | number | null | undefined) => { router.replace({ query: { ...router.query, [name]: newValue } }, undefined, { shallow: true }); diff --git a/packages/app-store/routing-forms/pages/routing-link/[...appPages].tsx b/packages/app-store/routing-forms/pages/routing-link/[...appPages].tsx index c63a6f61b4..ab0191e4aa 100644 --- a/packages/app-store/routing-forms/pages/routing-link/[...appPages].tsx +++ b/packages/app-store/routing-forms/pages/routing-link/[...appPages].tsx @@ -82,7 +82,7 @@ function RoutingForm({ form, profile, ...restProps }: Props) { }, [customPageMessage]); const responseMutation = trpc.viewer.appRoutingForms.public.response.useMutation({ - onSuccess: () => { + onSuccess: async () => { const decidedActionWithFormResponse = decidedActionWithFormResponseRef.current; if (!decidedActionWithFormResponse) { return; @@ -98,7 +98,7 @@ function RoutingForm({ form, profile, ...restProps }: Props) { if (decidedAction.type === "customPageMessage") { setCustomPageMessage(decidedAction.value); } else if (decidedAction.type === "eventTypeRedirectUrl") { - router.push(`/${decidedAction.value}?${allURLSearchParams}`); + await router.push(`/${decidedAction.value}?${allURLSearchParams}`); } else if (decidedAction.type === "externalRedirectUrl") { window.parent.location.href = `${decidedAction.value}?${allURLSearchParams}`; }