Skip to content

Commit

Permalink
fix: create a trpc route for fetching country (#7555)
Browse files Browse the repository at this point in the history
* fix: create a trpc route for fetching country

Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in>

* fix: type error

Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in>

---------

Signed-off-by: Udit Takkar <udit.07814802719@cse.mait.ac.in>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
  • Loading branch information
3 people authored Mar 8, 2023
1 parent 84da528 commit 4e1e257
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
20 changes: 0 additions & 20 deletions apps/web/pages/api/countrycode.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/trpc/server/routers/viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ const publicViewerRouter = router({
locale,
};
}),
countryCode: publicProcedure.query(({ ctx }) => {
const { req } = ctx;

const countryCode: string | string[] = req?.headers?.["x-vercel-ip-country"] ?? "";
return { countryCode: Array.isArray(countryCode) ? countryCode[0] : countryCode };
}),
samlTenantProduct: publicProcedure
.input(
z.object({
Expand Down
21 changes: 10 additions & 11 deletions packages/ui/form/PhoneInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isSupportedCountry } from "libphonenumber-js";
import { useEffect, useState } from "react";
import { useState } from "react";
import BasePhoneInput from "react-phone-number-input";
import type { Props, Country } from "react-phone-number-input";
import "react-phone-number-input/style.css";

import { trpc } from "@calcom/trpc/react";

export type PhoneInputProps = Props<{
value: string;
id?: string;
Expand Down Expand Up @@ -34,16 +36,13 @@ function PhoneInput({ name, className = "", onChange, ...rest }: PhoneInputProps

const useDefaultCountry = () => {
const [defaultCountry, setDefaultCountry] = useState<Country>("US");
useEffect(() => {
fetch("/api/countrycode")
.then((res) => res.json())
.then((res) => {
if (isSupportedCountry(res.countryCode)) setDefaultCountry(res.countryCode);
})
.catch((err) => {
console.log(err);
});
}, []);
const query = trpc.viewer.public.countryCode.useQuery(undefined, {
onSuccess: (data) => {
if (isSupportedCountry(data?.countryCode)) {
setDefaultCountry(data.countryCode as Country);
}
},
});

return defaultCountry;
};
Expand Down

1 comment on commit 4e1e257

@vercel
Copy link

@vercel vercel bot commented on 4e1e257 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./apps/storybook

ui-git-main-cal.vercel.app
timelessui.com
ui.cal.com
cal-com-storybook.vercel.app
ui-cal.vercel.app
www.timelessui.com

Please sign in to comment.