From 3a8b28198e51fff0a6f7d97b0ae2f6e0d3b73d47 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 16 Jan 2023 17:06:25 +0700 Subject: Fix delay input form --- src/components/Fields.js | 19 ++- src/pages/my/address/[id]/edit.js | 307 ++++++++++++++++++------------------ src/pages/my/address/create.js | 323 +++++++++++++++++++------------------- 3 files changed, 327 insertions(+), 322 deletions(-) (limited to 'src') diff --git a/src/components/Fields.js b/src/components/Fields.js index 3db8bfdd..586a6a22 100644 --- a/src/components/Fields.js +++ b/src/components/Fields.js @@ -1,20 +1,19 @@ import ReactSelect from "react-select"; const Select = ({ - options, - name, - setFieldValue, - value, - disabled + field, + ...props }) => ( + <> setFieldValue(name, option.value)} - value={value ? options.find(option => option.value === value) : ''} - isDisabled={disabled} + ref={field.ref} + onChange={(option) => field.onChange(option.value)} + value={field.value ? props.options.find(option => option.value === field.value) : ''} + isDisabled={props.disabled} + {...props} /> + ); export { diff --git a/src/pages/my/address/[id]/edit.js b/src/pages/my/address/[id]/edit.js index 98bd05e7..78eef635 100644 --- a/src/pages/my/address/[id]/edit.js +++ b/src/pages/my/address/[id]/edit.js @@ -1,8 +1,9 @@ -import { useFormik } from "formik"; -import * as Yup from "yup"; +import { Controller, useForm } from "react-hook-form" import WithAuth from "../../../../components/WithAuth"; import Layout from "../../../../components/Layout"; import AppBar from "../../../../components/AppBar"; +import { yupResolver } from "@hookform/resolvers/yup"; +import * as Yup from "yup"; import { Select } from "../../../../components/Fields"; import { useEffect, useState } from "react"; import apiOdoo from "../../../../helpers/apiOdoo"; @@ -29,7 +30,7 @@ const types = [ export async function getServerSideProps( context ) { const { id } = context.query; const address = await apiOdoo('GET', `/api/v1/partner/${id}/address`); - let initialValues = { + let defaultValues = { type: address.type, name: address.name, email: address.email, @@ -37,197 +38,199 @@ export async function getServerSideProps( context ) { street: address.street, zip: address.zip, city: address.city?.id, - district: address.district?.id || null, - subDistrict: address.sub_district?.id || null + district: address.district?.id || '', + subDistrict: address.sub_district?.id || '' }; - return { props: { id, initialValues } }; + return { props: { id, defaultValues } }; } -export default function EditAddress({ id, initialValues }) { +export default function CreateAddress({ id, defaultValues }) { const router = useRouter(); - - const onSubmit = async (values) => { - const parameters = { - ...values, - city_id: values.city, - district_id: values.district, - sub_district_id: values.subDistrict - } - - const address = await apiOdoo('PUT', `/api/v1/partner/${id}/address`, parameters); - if (address?.id) { - toast.success('Berhasil mengubah alamat'); - router.back(); - } - }; - - const form = useFormik({ initialValues, validationSchema, onSubmit }); - const { - values, - errors, - touched, - handleChange, + register, + formState: { errors }, handleSubmit, - setFieldValue, - } = form; + watch, + setValue, + control, + } = useForm({ + resolver: yupResolver(validationSchema), + defaultValues + }); const [ cities, setCities ] = useState([]); const [ districts, setDistricts ] = useState([]); const [ subDistricts, setSubDistricts ] = useState([]); - useEffect(() => { - const loadCities = async () => { - let dataCities = await apiOdoo('GET', '/api/v1/city'); + useEffect(() => { + const loadCities = async () => { + let dataCities = await apiOdoo('GET', '/api/v1/city'); dataCities = dataCities.map((city) => ({ value: city.id, label: city.name })); setCities(dataCities); }; loadCities(); }, []); - + + const watchCity = watch('city'); useEffect(() => { - setFieldValue('district', ''); - if (values.city) { + setValue('district', ''); + if (watchCity) { const loadDistricts = async () => { - let dataDistricts = await apiOdoo('GET', `/api/v1/district?city_id=${values.city}`); + let dataDistricts = await apiOdoo('GET', `/api/v1/district?city_id=${watchCity}`); dataDistricts = dataDistricts.map((district) => ({ value: district.id, label: district.name })); setDistricts(dataDistricts); }; loadDistricts(); } - }, [ values.city, setFieldValue ]); - - useEffect(() => { - setFieldValue('subDistrict', ''); - if (values.district) { + }, [ watchCity, setValue ]); + + const watchDistrict = watch('district'); + useEffect(() => { + setValue('subDistrict', ''); + if (watchDistrict) { const loadSubDistricts = async () => { - let dataSubDistricts = await apiOdoo('GET', `/api/v1/sub_district?district_id=${values.district}`); + let dataSubDistricts = await apiOdoo('GET', `/api/v1/sub_district?district_id=${watchDistrict}`); dataSubDistricts = dataSubDistricts.map((district) => ({ value: district.id, label: district.name })); setSubDistricts(dataSubDistricts); }; loadSubDistricts(); } - }, [ values.district, setFieldValue ]); + }, [ watchDistrict, setValue ]) + + const onSubmitHandler = async (values) => { + const parameters = { + ...values, + city_id: values.city, + district_id: values.district, + sub_district_id: values.subDistrict + } + + const address = await apiOdoo('PUT', `/api/v1/partner/${id}/address`, parameters); + if (address?.id) { + toast.success('Berhasil mengubah alamat'); + router.back(); + } + }; return ( -
- - - { errors.name && touched.name && ( -
{ errors.name }
- ) } - - - - { errors.email && touched.email && ( -
{ errors.email }
- ) } - - - - { errors.mobile && touched.mobile && ( -
{ errors.mobile }
- ) } - - - - { errors.street && touched.street && ( -
{ errors.street }
- ) } - - - - { errors.zip && touched.zip && ( -
{ errors.zip }
- ) } - - - - - - } + /> +
{ errors.type?.message }
+ + +
+ + +
{ errors.name?.message }
+
+ +
+ + +
{ errors.email?.message }
+
+ +
+ + +
{ errors.mobile?.message }
+
+ +
+ + +
{ errors.street?.message }
+
+ +
+ + +
{ errors.zip?.message }
+
+ +
+ + + )} + /> +
+ +
+ + ( + - { errors.type && touched.type && ( -
{ errors.type }
- ) } - - - - { errors.name && touched.name && ( -
{ errors.name }
- ) } - - - - { errors.email && touched.email && ( -
{ errors.email }
- ) } - - - - { errors.mobile && touched.mobile && ( -
{ errors.mobile }
- ) } - - - - { errors.street && touched.street && ( -
{ errors.street }
- ) } - - - - { errors.zip && touched.zip && ( -
{ errors.zip }
- ) } - - - - - - } + /> +
{ errors.type?.message }
+
+ +
+ + +
{ errors.name?.message }
+
+ +
+ + +
{ errors.email?.message }
+
+ +
+ + +
{ errors.mobile?.message }
+
+ +
+ + +
{ errors.street?.message }
+
+ +
+ + +
{ errors.zip?.message }
+
+ +
+ + + )} + /> +
+ +
+ + ( +