import type { FC } from '../../lib/teact/teact'; import React, { memo, useCallback, useEffect, useRef, } from '../../lib/teact/teact'; import type { ApiCountry } from '../../api/types'; import type { FormEditDispatch, FormState } from '../../hooks/reducers/usePaymentReducer'; import useFocusAfterAnimation from '../../hooks/useFocusAfterAnimation'; import useLang from '../../hooks/useLang'; import Checkbox from '../ui/Checkbox'; import InputText from '../ui/InputText'; import Select from '../ui/Select'; import './ShippingInfo.scss'; export type OwnProps = { state: FormState; needEmail: boolean; needPhone: boolean; needName: boolean; needAddress: boolean; countryList: ApiCountry[]; dispatch: FormEditDispatch; }; const ShippingInfo: FC = ({ state, needEmail, needPhone, needName, needAddress, countryList, dispatch, }) => { // eslint-disable-next-line no-null/no-null const inputRef = useRef(null); // eslint-disable-next-line no-null/no-null const phoneRef = useRef(null); // eslint-disable-next-line no-null/no-null const selectCountryRef = useRef(null); useEffect(() => { if (selectCountryRef.current && selectCountryRef.current.value !== state.countryIso2) { selectCountryRef.current.value = state.countryIso2; } }, [state.countryIso2]); const lang = useLang(); useFocusAfterAnimation(inputRef); const handleAddress1Change = useCallback((e) => { dispatch({ type: 'changeAddress1', payload: e.target.value }); }, [dispatch]); const handleAddress2Change = useCallback((e) => { dispatch({ type: 'changeAddress2', payload: e.target.value }); }, [dispatch]); const handleCityChange = useCallback((e) => { dispatch({ type: 'changeCity', payload: e.target.value }); }, [dispatch]); const handleStateChange = useCallback((e) => { dispatch({ type: 'changeState', payload: e.target.value }); }, [dispatch]); const handleCountryChange = useCallback((e) => { dispatch({ type: 'changeCountry', payload: countryList.find((country) => country.iso2 === e.target.value) }); }, [countryList, dispatch]); const handlePostCodeChange = useCallback((e) => { dispatch({ type: 'changePostCode', payload: e.target.value }); }, [dispatch]); const handleFullNameChange = useCallback((e) => { dispatch({ type: 'changeFullName', payload: e.target.value }); }, [dispatch]); const handleEmailChange = useCallback((e) => { dispatch({ type: 'changeEmail', payload: e.target.value }); }, [dispatch]); const handlePhoneChange = useCallback((e) => { let { value } = e.target; value = `+${value.replace(/\D/g, '')}`; if (phoneRef.current) { phoneRef.current.value = value; } dispatch({ type: 'changePhone', payload: value }); }, [dispatch]); const handleSaveInfoChange = useCallback((e) => { dispatch({ type: 'changeSaveInfo', payload: e.target.value }); }, [dispatch]); const { formErrors } = state; return (
{needAddress ? (
{lang('PaymentShippingAddress')}
) : undefined} { needName || needEmail || needPhone ? (
{lang('PaymentShippingReceiver')}
) : undefined } { needName && ( ) } { needEmail && ( ) } { needPhone && ( ) }
); }; export default memo(ShippingInfo);