fix: parse phone number when setting default values
This commit is contained in:
@@ -1,14 +1,15 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { isValidPhoneNumber, parsePhoneNumber } from "libphonenumber-js"
|
||||||
import { useParams } from "next/navigation"
|
import { useParams } from "next/navigation"
|
||||||
import { useFormState as useReactFormState } from "react-dom"
|
import { useFormState as useReactFormState } from "react-dom"
|
||||||
import { FormProvider, useForm } from "react-hook-form"
|
import { FormProvider, useForm } from "react-hook-form"
|
||||||
|
import { usePhoneInput } from "react-international-phone"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
import { profile } from "@/constants/routes/myPages"
|
import { profile } from "@/constants/routes/myPages"
|
||||||
|
|
||||||
import { editProfile } from "@/actions/editProfile"
|
import { editProfile } from "@/actions/editProfile"
|
||||||
import Header from "@/components/Profile/Header"
|
|
||||||
import Button from "@/components/TempDesignSystem/Button"
|
import Button from "@/components/TempDesignSystem/Button"
|
||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Link from "@/components/TempDesignSystem/Link"
|
||||||
import Title from "@/components/TempDesignSystem/Text/Title"
|
import Title from "@/components/TempDesignSystem/Text/Title"
|
||||||
@@ -39,6 +40,15 @@ export default function Form({ user }: EditFormProps) {
|
|||||||
null
|
null
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const { inputValue: phoneInput } = usePhoneInput({
|
||||||
|
defaultCountry:
|
||||||
|
user.phoneNumber && isValidPhoneNumber(user.phoneNumber)
|
||||||
|
? parsePhoneNumber(user.phoneNumber).country?.toLowerCase()
|
||||||
|
: "se",
|
||||||
|
forceDialCode: true,
|
||||||
|
value: user.phoneNumber,
|
||||||
|
})
|
||||||
|
|
||||||
const methods = useForm<EditProfileSchema>({
|
const methods = useForm<EditProfileSchema>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
address: {
|
address: {
|
||||||
@@ -50,7 +60,7 @@ export default function Form({ user }: EditFormProps) {
|
|||||||
dateOfBirth: user.dateOfBirth,
|
dateOfBirth: user.dateOfBirth,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
language: user.language,
|
language: user.language,
|
||||||
phoneNumber: user.phoneNumber,
|
phoneNumber: phoneInput,
|
||||||
currentPassword: "",
|
currentPassword: "",
|
||||||
newPassword: "",
|
newPassword: "",
|
||||||
retypeNewPassword: "",
|
retypeNewPassword: "",
|
||||||
|
|||||||
@@ -54,8 +54,7 @@ export default function Phone({
|
|||||||
? parsePhoneNumber(
|
? parsePhoneNumber(
|
||||||
formState.defaultValues?.phoneNumber
|
formState.defaultValues?.phoneNumber
|
||||||
).country?.toLowerCase()
|
).country?.toLowerCase()
|
||||||
: "sv",
|
: "se",
|
||||||
disableCountryGuess: true,
|
|
||||||
forceDialCode: true,
|
forceDialCode: true,
|
||||||
value: phone,
|
value: phone,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user