import { ProfileTypeEnum, SerializableValue, ShippingAddress } from '@bit-ui-libs/common'; import { yupResolver } from '@hookform/resolvers/yup'; import { useQuery } from '@tanstack/react-query'; import { useCallback, useMemo } from 'react'; import { FormProvider, useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { ScrollView, TouchableOpacity, View } from 'react-native'; import { mixed, object, string } from 'yup'; import { Button, Icon, Loading, RadioButton, RadioButtonGroup, Text } from '../../../components'; import { tw } from '../../../core'; import { BottomModal } from '../../../layouts'; import { useServicesStore } from '../../../stores'; import { flattenFieldErrors } from '../../../utils'; import { useGetDefaultProfile } from '../../profile'; const schema = object().shape({ addressId: string().optional().nullable(), type: mixed().required('Address Type is required').nullable(), name: string().required('Name is required').nullable(), phoneNumber: string().required('Phone Number is required').nullable(), address: string().required('Address is required').nullable(), city: string().required('City is required').nullable(), state: string().required('State is required').nullable(), zip: string() .min(4, 'The zip must be greater than or equal to 4') .max(10, 'The zip must be less than or equal to 10') .required('Zip is required') .nullable(), country: string().required('Country is required').nullable(), }); interface CalculateCostModalProps { visible?: boolean; onAddNewAddress?: () => void; onSubmit?: (data: ShippingAddress) => void; onDismiss?: () => void; } export function CalculateCostModal(props: CalculateCostModalProps) { const { visible, onAddNewAddress, onSubmit, onDismiss } = props; const { t } = useTranslation(); const seller = useServicesStore((s) => s.seller); const { data: profile, isLoading } = useGetDefaultProfile({ profileType: ProfileTypeEnum.Seller }); const { data: addresses } = useQuery({ queryKey: ['shipping-addresses', profile?.id], queryFn: () => seller.shippingAddress.listShippingAddresses(profile!.id, {}), enabled: !!profile?.id, }); const form = useForm({ mode: 'all', resolver: yupResolver(schema) }); const errors = flattenFieldErrors(form.formState.errors); const onAddressSelect = useCallback( (addressId: SerializableValue) => { const address = addresses?.find((a) => a.id === addressId); if (!address) return; form.reset({ ...address, addressId: address.id }); }, [form, addresses] ); const addressData = useMemo( () => [ { label: 'Country', key: 'country' }, { label: 'State', key: 'state' }, { label: 'City', key: 'city' }, { label: 'Zip Code', key: 'zip' }, ], [] ); if (isLoading) return ( Loading addresses... ); return ( {t`marketplace.selectShipFormAddress`} {t`marketplace.shipFrom`} {t`marketplace.selectAddress`} {addresses?.map((address, i) => ( Edit {t`common.delete`} {addressData.map((value, index) => ( {value.label}: {address[value.key as keyof ShippingAddress]} ))} ))} {t`marketplace.addNewAddress`} {errors.map((err, i) => ( {err} ))} ); }