feat: display country code from IP (#19788)
* feat: display country code from IP * chore: remove * fix: unit test
This commit is contained in:
1 parent
1a96149ce3
commit
d59fdf21fd
11 files changed
+44
-41
No files matched your search
@@ -5,7 +5,6 @@ vi.mock("@calcom/ui", async (originalImport) => {
|
||||
// Dynamic imports of Components are not supported in Vitest. So, we use the non-lazy version of the components
|
||||
return {
|
||||
...original,
|
||||
PhoneInput: original.PhoneInputNonLazy,
|
||||
AddressInput: original.AddressInputNonLazy,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import PhoneInput from "react-phone-input-2";
|
||||
import "react-phone-input-2/lib/style.css";
|
||||
|
||||
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
export type PhoneInputProps = {
|
||||
value?: string;
|
||||
id?: string;
|
||||
placeholder?: string;
|
||||
required?: boolean;
|
||||
className?: string;
|
||||
name?: string;
|
||||
disabled?: boolean;
|
||||
onChange: (value: string) => void;
|
||||
defaultCountry?: string;
|
||||
};
|
||||
|
||||
function BasePhoneInput({
|
||||
name,
|
||||
className = "",
|
||||
onChange,
|
||||
value,
|
||||
defaultCountry = "us",
|
||||
...rest
|
||||
}: PhoneInputProps) {
|
||||
const isPlatform = useIsPlatform();
|
||||
|
||||
if (!isPlatform) {
|
||||
return (
|
||||
<BasePhoneInputWeb name={name} className={className} onChange={onChange} value={value} {...rest} />
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<PhoneInput
|
||||
{...rest}
|
||||
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
|
||||
enableSearch
|
||||
disableSearchIcon
|
||||
country={defaultCountry}
|
||||
inputProps={{
|
||||
name: name,
|
||||
required: rest.required,
|
||||
placeholder: rest.placeholder,
|
||||
}}
|
||||
onChange={(value) => {
|
||||
onChange(`+${value}`);
|
||||
}}
|
||||
containerClass={classNames(
|
||||
"hover:border-emphasis dark:focus:border-emphasis border-default !bg-default rounded-md border focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default disabled:cursor-not-allowed",
|
||||
className
|
||||
)}
|
||||
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
|
||||
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
|
||||
searchClass="!text-default !bg-default hover:!bg-emphasis"
|
||||
dropdownClass="!text-default !bg-default"
|
||||
inputStyle={{ width: "inherit", border: 0 }}
|
||||
searchStyle={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
padding: "6px 12px",
|
||||
gap: "8px",
|
||||
width: "296px",
|
||||
height: "28px",
|
||||
marginLeft: "-4px",
|
||||
}}
|
||||
dropdownStyle={{ width: "max-content" }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function BasePhoneInputWeb({
|
||||
name,
|
||||
className = "",
|
||||
onChange,
|
||||
value,
|
||||
...rest
|
||||
}: Omit<PhoneInputProps, "defaultCountry">) {
|
||||
return (
|
||||
<PhoneInput
|
||||
{...rest}
|
||||
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
|
||||
enableSearch
|
||||
disableSearchIcon
|
||||
inputProps={{
|
||||
name: name,
|
||||
required: rest.required,
|
||||
placeholder: rest.placeholder,
|
||||
}}
|
||||
onChange={(value) => {
|
||||
onChange(`+${value}`);
|
||||
}}
|
||||
containerClass={classNames(
|
||||
"hover:border-emphasis dark:focus:border-emphasis border-default !bg-default rounded-md border focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default disabled:cursor-not-allowed",
|
||||
className
|
||||
)}
|
||||
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
|
||||
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
|
||||
searchClass="!text-default !bg-default hover:!bg-emphasis"
|
||||
dropdownClass="!text-default !bg-default"
|
||||
inputStyle={{ width: "inherit", border: 0 }}
|
||||
searchStyle={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
padding: "6px 12px",
|
||||
gap: "8px",
|
||||
width: "296px",
|
||||
height: "28px",
|
||||
marginLeft: "-4px",
|
||||
}}
|
||||
dropdownStyle={{ width: "max-content" }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Function not in use anywhere.
|
||||
// const useDefaultCountry = () => {
|
||||
// const [defaultCountry, setDefaultCountry] = useState("us");
|
||||
// const query = trpc.viewer.public.countryCode.useQuery(undefined, {
|
||||
// refetchOnWindowFocus: false,
|
||||
// refetchOnReconnect: false,
|
||||
// retry: false,
|
||||
// });
|
||||
|
||||
// useEffect(
|
||||
// function refactorMeWithoutEffect() {
|
||||
// const data = query.data;
|
||||
// if (!data?.countryCode) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// isSupportedCountry(data?.countryCode)
|
||||
// ? setDefaultCountry(data.countryCode.toLowerCase())
|
||||
// : setDefaultCountry(navigator.language.split("-")[1]?.toLowerCase() || "us");
|
||||
// },
|
||||
// [query.data]
|
||||
// );
|
||||
|
||||
// return defaultCountry;
|
||||
// };
|
||||
|
||||
export default BasePhoneInput;
|
||||
@@ -1,8 +0,0 @@
|
||||
import dynamic from "next/dynamic";
|
||||
|
||||
/** These are like 40kb that not every user needs */
|
||||
const PhoneInputLazy = dynamic(
|
||||
() => import("./PhoneInput")
|
||||
) as unknown as typeof import("./PhoneInput").default;
|
||||
|
||||
export default PhoneInputLazy;
|
||||
@@ -121,9 +121,7 @@ export { ShellSubHeading } from "./components/layout";
|
||||
|
||||
/** ⬇️ TODO - Move these to components */
|
||||
export { default as AddressInput } from "./form/AddressInputLazy";
|
||||
export { default as PhoneInput } from "./form/PhoneInputLazy";
|
||||
export { default as MultiEmail } from "./form/MultiEmailLazy";
|
||||
export { default as PhoneInputNonLazy } from "./form/PhoneInput";
|
||||
export { default as AddressInputNonLazy } from "./form/AddressInput";
|
||||
|
||||
export { UnstyledSelect } from "./form/Select";
|
||||
|
||||
Reference in new issue
Block a user