feat: get country code from ip geolocation (#6880)
* feat: get coutnry code from ip geolocation Signed-off-by: Udit Takkar <[email protected]> * fix: create new api route for fetching code Signed-off-by: Udit Takkar <[email protected]> * chore: replace city with country Signed-off-by: Udit Takkar <[email protected]> * refactor: create hook for country Signed-off-by: Udit Takkar <[email protected]> --------- Signed-off-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
8c63b42cec
commit
c20835a4c8
2 files changed
+40
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { isSupportedCountry } from "libphonenumber-js";
|
||||
import { useEffect, useState } from "react";
|
||||
import type { Props } from "react-phone-number-input/react-hook-form";
|
||||
import BasePhoneInput from "react-phone-number-input/react-hook-form";
|
||||
import "react-phone-number-input/style.css";
|
||||
@@ -19,10 +21,12 @@ function PhoneInput<FormValues>({
|
||||
onChange,
|
||||
...rest
|
||||
}: PhoneInputProps<FormValues>) {
|
||||
const defaultCountry = useDefaultCountry();
|
||||
return (
|
||||
<BasePhoneInput
|
||||
{...rest}
|
||||
international
|
||||
defaultCountry={defaultCountry}
|
||||
name={name}
|
||||
control={control}
|
||||
onChange={onChange}
|
||||
@@ -35,4 +39,20 @@ function PhoneInput<FormValues>({
|
||||
);
|
||||
}
|
||||
|
||||
const useDefaultCountry = () => {
|
||||
const [defaultCountry, setDefaultCountry] = useState("US");
|
||||
useEffect(() => {
|
||||
fetch("/api/countrycode")
|
||||
.then((res) => res.json())
|
||||
.then((res) => {
|
||||
if (isSupportedCountry(res.countryCode)) setDefaultCountry(res.countryCode);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}, []);
|
||||
|
||||
return defaultCountry;
|
||||
};
|
||||
|
||||
export default PhoneInput;
|
||||
Reference in new issue
Block a user