diff --git a/src/components/CustomBottomSheet/GiftBottomSheet.tsx b/src/components/CustomBottomSheet/GiftBottomSheet.tsx index 0a66906..efa8623 100644 --- a/src/components/CustomBottomSheet/GiftBottomSheet.tsx +++ b/src/components/CustomBottomSheet/GiftBottomSheet.tsx @@ -73,8 +73,6 @@ export function GiftBottomSheet({ giftForm.setFieldValue("receiverPhone", phone)} propName="receiverPhone" label={t("address.receiverPhone")} /> @@ -109,8 +107,6 @@ export function GiftBottomSheet({ giftForm.setFieldValue("senderPhone", phone)} propName="senderPhone" label={t("address.receiverPhone")} /> diff --git a/src/components/ProPhoneInput.tsx b/src/components/ProPhoneInput.tsx index b150ae0..c86270b 100644 --- a/src/components/ProPhoneInput.tsx +++ b/src/components/ProPhoneInput.tsx @@ -5,15 +5,21 @@ import { useTranslation } from "react-i18next"; import PhoneInput from "react-phone-input-2"; import { useAppSelector } from "redux/hooks"; import { ProBlack1 } from "ThemeConstants"; +import { PhoneNumberUtil } from "google-libphonenumber"; +import useFormInstance from "antd/es/form/hooks/useFormInstance"; interface ProPhoneInput extends TitleProps { - phone: string; - setPhone: (phone: string) => void; - propName?: string - label?: string + propName?: string; + label?: string; + getValueCallback?: (value: string) => void; } -const ProPhoneInput: FunctionComponent = ({ phone, setPhone, propName, label }) => { +const ProPhoneInput: FunctionComponent = ({ + propName, + label, + getValueCallback, +}) => { + const form = useFormInstance(); const { t } = useTranslation(); const { themeName } = useAppSelector((state) => state.theme); const { isRTL } = useAppSelector((state) => state.locale); @@ -23,25 +29,30 @@ const ProPhoneInput: FunctionComponent = ({ phone, setPhone, prop name={propName} label={label} rules={[ - { required: true, message: "" }, + { required: true, message: t("validation.phoneRequired") }, { validator: (_, value) => { - if (!value || value.length <= 3) { - return Promise.reject(new Error("")); - } + // Initialize the PhoneNumberUtil instance + const phoneUtil = PhoneNumberUtil.getInstance(); + if ( + value && + !phoneUtil.isValidNumber( + phoneUtil.parseAndKeepRawInput(`+${value}`), + ) + ) + return Promise.reject(new Error(t("validation.invalidPhone"))); + return Promise.resolve(); }, }, ]} - normalize={(value) => { - if (value && setPhone) { - setPhone(value); - } - return value; - }} > { + form.setFieldValue(propName, value); + getValueCallback?.(value); + }} + phone={form.getFieldValue(propName)} themeName={themeName} isRTL={isRTL} placeholder={t("login.mobileNumber")} @@ -52,8 +63,17 @@ const ProPhoneInput: FunctionComponent = ({ phone, setPhone, prop ); }; -export const PhoneInputWrapper = ({ phone, themeName, isRTL, placeholder, value, onChange, propName, label }: { - phone: string; +export const PhoneInputWrapper = ({ + phone, + themeName, + isRTL, + placeholder, + value, + onChange, + propName, + label, +}: { + phone?: string; themeName: string; isRTL: boolean; placeholder: string; @@ -106,7 +126,6 @@ export const PhoneInputWrapper = ({ phone, themeName, isRTL, placeholder, value, autoComplete: "tel", type: "tel", inputMode: "numeric", - pattern: "[0-9]*", }} /> diff --git a/src/pages/checkout/components/phoneCard.tsx b/src/pages/checkout/components/phoneCard.tsx index b1598ac..9ce23b2 100644 --- a/src/pages/checkout/components/phoneCard.tsx +++ b/src/pages/checkout/components/phoneCard.tsx @@ -1,23 +1,19 @@ import ProInputCard from "components/ProInputCard/ProInputCard.tsx"; import ProPhoneInput from "components/ProPhoneInput"; -import { selectCart, updatePhone } from "features/order/orderSlice"; +import { selectCart } from "features/order/orderSlice"; import { useTranslation } from "react-i18next"; -import { useAppDispatch, useAppSelector } from "redux/hooks"; +import { useAppSelector } from "redux/hooks"; import { OrderType } from "pages/checkout/hooks/types.ts"; export default function PhoneCard() { const { t } = useTranslation(); - const dispatch = useAppDispatch(); - const { phone, orderType } = useAppSelector(selectCart); + const { orderType } = useAppSelector(selectCart); return ( orderType !== OrderType.Gift && ( <> - dispatch(updatePhone(phone))} - /> + ) diff --git a/src/pages/login/page.tsx b/src/pages/login/page.tsx index 3870731..15f5cdc 100644 --- a/src/pages/login/page.tsx +++ b/src/pages/login/page.tsx @@ -23,19 +23,22 @@ export default function LoginPage() { const [sendOtp, { isLoading }] = useSendOtpMutation(); const { subdomain } = useParams(); - const [phone, setPhone] = useState(""); + // const [phone, setPhone] = useState(""); const [selectedDate, setSelectedDate] = useState(""); const [isOpen, setIsOpen] = useState(false); const navigate = useNavigate(); const handleLogin = async () => { - localStorage.setItem("userPhone", form.getFieldValue("phone")); - if (form.getFieldsValue()) - sendOtp(form.getFieldsValue()).then((response: any) => { - message.info(t("login.OTPSentToYourPhoneNumber")); - navigate(`/${subdomain}/otp`); - localStorage.setItem("otp", response.data.result.otp); - }); + form.validateFields().then(() => { + if (form.getFieldsValue()) { + localStorage.setItem("userPhone", form.getFieldValue("phone")); + sendOtp(form.getFieldsValue()).then((response: any) => { + message.info(t("login.OTPSentToYourPhoneNumber")); + navigate(`/${subdomain}/otp`); + localStorage.setItem("otp", response.data.result.otp); + }); + } + }); }; return ( @@ -94,7 +97,11 @@ export default function LoginPage() { /> - + - +