From 8aae174d19d18191607e126ca7bfe4936d53309a Mon Sep 17 00:00:00 2001 From: Reza_ashrafi Date: Thu, 12 May 2022 12:56:29 +0430 Subject: [PATCH] reza fixed edit address --- src/components/CustomTextInput.js | 2 + src/screens/Address/index.js | 164 ++++++++++++------ .../components/ProfileAddress/index.js | 22 ++- 3 files changed, 130 insertions(+), 58 deletions(-) diff --git a/src/components/CustomTextInput.js b/src/components/CustomTextInput.js index e411b0f..1b87b3e 100644 --- a/src/components/CustomTextInput.js +++ b/src/components/CustomTextInput.js @@ -23,6 +23,7 @@ const FloatingLabelInput = ({ keyboardType, multiline, theme, + defaultValue, editable = true, ...props }) => { @@ -89,6 +90,7 @@ const FloatingLabelInput = ({ underlineColorAndroid={"transparent"} maxLength={maxLength} value={value} + defaultValue={defaultValue} onBlur={() => (value ? {} : setIsFocused(() => false))} keyboardType={keyboardType} multiline={multiline} diff --git a/src/screens/Address/index.js b/src/screens/Address/index.js index 8714e14..bd8603d 100644 --- a/src/screens/Address/index.js +++ b/src/screens/Address/index.js @@ -43,6 +43,7 @@ const Address = ({ provinces, cities, createAddress, + editAddress, profileFields, setProfileFields, setProfile, @@ -58,36 +59,65 @@ const Address = ({ const profilePage = React.useMemo(() => { return route.params?.type === "profile"; - },[route]); + }, [route]); + + const editAddressPreviousValue = React.useMemo(() => { + return route.params?.form; + }, [route]); useEffect(() => { getProvince(); - setProfileFields({ - file: user?.picFileId, - picFileId: user?.picFileId, - docFileIds: user?.docFileIds, - cellphone: user?.cellphone, - firstName: user?.firstName, - lastName: user?.lastName, - password: user?.password, - username: user?.username, - address: user?.address, - email: user?.email, - birthDate: user?.birthDate, - gender: user?.gender, - status: user?.status, - nationalId: user?.nationalId, - phone: user?.phone, - schools: user?.schools, - zoneId: user?.zoneId, - schoolId: user?.schoolId, - postalCode: user?.postalCode, - gradeIds: user?.gradeIds, - provinceId: user?.provinceId, - cityId: user?.cityId, - }); + if (profilePage) { + setProfileFields({ + file: user?.picFileId, + picFileId: user?.picFileId, + docFileIds: user?.docFileIds, + cellphone: user?.cellphone, + firstName: user?.firstName, + lastName: user?.lastName, + password: user?.password, + username: user?.username, + address: user?.address, + email: user?.email, + birthDate: user?.birthDate, + gender: user?.gender, + status: user?.status, + nationalId: user?.nationalId, + phone: user?.phone, + schools: user?.schools, + zoneId: user?.zoneId, + schoolId: user?.schoolId, + postalCode: user?.postalCode, + gradeIds: user?.gradeIds, + provinceId: user?.provinceId, + cityId: user?.cityId, + }); + } + if (route.params?.type === "editAddress") { + setAddressFields({ + firstName: editAddressPreviousValue?.firstName, + lastName: editAddressPreviousValue?.lastName, + cellphone: editAddressPreviousValue?.cellphone, + phone: editAddressPreviousValue?.phone, + provinceId: editAddressPreviousValue?.provinceId, + cityId: editAddressPreviousValue?.cityId, + postalCode: editAddressPreviousValue?.postalCode, + id: editAddressPreviousValue?.id, + }); + setMapAddress(editAddressPreviousValue?.address); + } }, []); + useEffect(() => { + if (route.params?.type === "editAddress" && provinces?.length) { + getCity({ + provinceId: provinces?.filter( + (province) => province?.name === editAddressPreviousValue?.provinceId + )[0]?.id, + }); + } + }, [provinces]); + useEffect(() => { StatusBar.setBarStyle( `${theme === "light" ? "dark" : "light"}-content`, @@ -98,9 +128,9 @@ const Address = ({ useEffect(() => { if (addressFields?.provinceId) { getCity({ - provinceId: provinces.filter( - (province) => province.name === addressFields?.provinceId - )[0].id, + provinceId: provinces?.filter( + (province) => province?.name === addressFields?.provinceId + )[0]?.id, }); } }, [addressFields]); @@ -115,10 +145,10 @@ const Address = ({ lastName, cellphone, phone, - address, provinceId, cityId, postalCode, + id } = addressFields; if (!firstName) { @@ -177,12 +207,6 @@ const Address = ({ } ); } - if (phone.length !== 11) { - return asyncAwesomeAlert("خطا", "شماره تلفن میبایست یازده رقم باشد.", { - showCancelButton: false, - confirmText: "باشه", - }); - } } if (!provinceId) { @@ -211,26 +235,43 @@ const Address = ({ } } - if (!mapAddress) { + if (!mapAddress && !editAddressPreviousValue?.address) { return asyncAwesomeAlert("خطا", "آدرس خود را از روی نقشه انتخاب کن", { showCancelButton: false, confirmText: "باشه", }); } - createAddress({ - firstName, - lastName, - cellphone, - phone, - address: mapAddress, - provinceId: provinces.filter( - (province) => province.name === provinceId - )[0].id, - cityId: cities.filter((city) => city.name === cityId)[0]?.id, - postalCode, - location: null, - }); + if (route.params?.type === "editAddress") { + editAddress({ + firstName: firstName, + lastName: lastName, + cellphone: cellphone, + phone: phone, + address: mapAddress, + provinceId: provinces?.filter( + (province) => province?.name === provinceId + )[0]?.id, + cityId: cities?.filter((city) => city?.name === cityId)[0]?.id, + postalCode: postalCode, + location: null, + id, + }); + } else { + createAddress({ + firstName: firstName, + lastName: lastName, + cellphone: cellphone, + phone: phone, + address: mapAddress, + provinceId: provinces?.filter( + (province) => province?.name === provinceId + )[0]?.id, + cityId: cities?.filter((city) => city?.name === cityId)[0]?.id, + postalCode: postalCode, + location: null, + }); + } navigation.goBack(); }; @@ -387,7 +428,9 @@ const Address = ({ name="lastName" maxlength="60" value={ - profilePage ? profileFields?.lastName : addressFields?.lastName + profilePage + ? profileFields?.lastName + : addressFields?.lastName } onChange={(name, value) => profilePage @@ -448,7 +491,9 @@ const Address = ({ textAlign="left" name="phone" maxLength={11} - value={profilePage ? profileFields?.phone : addressFields?.phone} + value={ + profilePage ? profileFields?.phone : addressFields?.phone + } onChange={(name, value) => profilePage ? setProfileFields({ ...profileFields, phone: value }) @@ -468,9 +513,14 @@ const Address = ({ defaultValue={ profilePage ? provinces?.filter( - (province) => province.id === profileFields?.provinceId + (province) => + province?.id === profileFields?.provinceId + )[0]?.name || province[0] + : provinces?.filter( + (province) => + province?.id === + editAddressPreviousValue?.provinceId )[0]?.name || province[0] - : province[0] } onChange={(name, value) => value === "استان" ? {} : onChange(name, value) @@ -485,9 +535,12 @@ const Address = ({ defaultValue={ profilePage ? cities?.filter( - (city) => city.id === profileFields?.cityId + (city) => city?.id === profileFields?.cityId + )[0]?.name || city[0] + : cities?.filter( + (city) => + city?.id === editAddressPreviousValue?.cityId )[0]?.name || city[0] - : city[0] } onChange={(name, value) => value === "شهرستان" ? {} : onChange(name, value) @@ -608,6 +661,7 @@ const mapDispatchToProps = { createAddress: userAddress.add, setProfileFields: user.setProfileFields, setProfile: user.setProfile, + editAddress: userAddress.update, }; export default connect(mapStateToProps, mapDispatchToProps)(Address); diff --git a/src/screens/Profile/components/ProfileAddress/index.js b/src/screens/Profile/components/ProfileAddress/index.js index 019ae56..d71e807 100644 --- a/src/screens/Profile/components/ProfileAddress/index.js +++ b/src/screens/Profile/components/ProfileAddress/index.js @@ -109,7 +109,15 @@ const ProfileAddress = ({ - + + navigation.navigate("CreateAddress", { + type: "editAddress", + form: address, + }) + } + > {!active ? ( - + + navigation.navigate("CreateAddress", { + type: "editAddress", + form: address, + }) + } + > deleteItem({ id : address?.id }), + onConfirm: () => deleteItem({ id: address?.id }), }) } >