reza fixed edit address

master
Reza_ashrafi 3 years ago
parent be780c908e
commit 8aae174d19
  1. 2
      src/components/CustomTextInput.js
  2. 106
      src/screens/Address/index.js
  3. 20
      src/screens/Profile/components/ProfileAddress/index.js

@ -23,6 +23,7 @@ const FloatingLabelInput = ({
keyboardType, keyboardType,
multiline, multiline,
theme, theme,
defaultValue,
editable = true, editable = true,
...props ...props
}) => { }) => {
@ -89,6 +90,7 @@ const FloatingLabelInput = ({
underlineColorAndroid={"transparent"} underlineColorAndroid={"transparent"}
maxLength={maxLength} maxLength={maxLength}
value={value} value={value}
defaultValue={defaultValue}
onBlur={() => (value ? {} : setIsFocused(() => false))} onBlur={() => (value ? {} : setIsFocused(() => false))}
keyboardType={keyboardType} keyboardType={keyboardType}
multiline={multiline} multiline={multiline}

@ -43,6 +43,7 @@ const Address = ({
provinces, provinces,
cities, cities,
createAddress, createAddress,
editAddress,
profileFields, profileFields,
setProfileFields, setProfileFields,
setProfile, setProfile,
@ -60,8 +61,13 @@ const Address = ({
return route.params?.type === "profile"; return route.params?.type === "profile";
}, [route]); }, [route]);
const editAddressPreviousValue = React.useMemo(() => {
return route.params?.form;
}, [route]);
useEffect(() => { useEffect(() => {
getProvince(); getProvince();
if (profilePage) {
setProfileFields({ setProfileFields({
file: user?.picFileId, file: user?.picFileId,
picFileId: user?.picFileId, picFileId: user?.picFileId,
@ -86,8 +92,32 @@ const Address = ({
provinceId: user?.provinceId, provinceId: user?.provinceId,
cityId: user?.cityId, 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(() => { useEffect(() => {
StatusBar.setBarStyle( StatusBar.setBarStyle(
`${theme === "light" ? "dark" : "light"}-content`, `${theme === "light" ? "dark" : "light"}-content`,
@ -98,9 +128,9 @@ const Address = ({
useEffect(() => { useEffect(() => {
if (addressFields?.provinceId) { if (addressFields?.provinceId) {
getCity({ getCity({
provinceId: provinces.filter( provinceId: provinces?.filter(
(province) => province.name === addressFields?.provinceId (province) => province?.name === addressFields?.provinceId
)[0].id, )[0]?.id,
}); });
} }
}, [addressFields]); }, [addressFields]);
@ -115,10 +145,10 @@ const Address = ({
lastName, lastName,
cellphone, cellphone,
phone, phone,
address,
provinceId, provinceId,
cityId, cityId,
postalCode, postalCode,
id
} = addressFields; } = addressFields;
if (!firstName) { if (!firstName) {
@ -177,12 +207,6 @@ const Address = ({
} }
); );
} }
if (phone.length !== 11) {
return asyncAwesomeAlert("خطا", "شماره تلفن میبایست یازده رقم باشد.", {
showCancelButton: false,
confirmText: "باشه",
});
}
} }
if (!provinceId) { if (!provinceId) {
@ -211,26 +235,43 @@ const Address = ({
} }
} }
if (!mapAddress) { if (!mapAddress && !editAddressPreviousValue?.address) {
return asyncAwesomeAlert("خطا", "آدرس خود را از روی نقشه انتخاب کن", { return asyncAwesomeAlert("خطا", "آدرس خود را از روی نقشه انتخاب کن", {
showCancelButton: false, showCancelButton: false,
confirmText: "باشه", confirmText: "باشه",
}); });
} }
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({ createAddress({
firstName, firstName: firstName,
lastName, lastName: lastName,
cellphone, cellphone: cellphone,
phone, phone: phone,
address: mapAddress, address: mapAddress,
provinceId: provinces.filter( provinceId: provinces?.filter(
(province) => province.name === provinceId (province) => province?.name === provinceId
)[0].id, )[0]?.id,
cityId: cities.filter((city) => city.name === cityId)[0]?.id, cityId: cities?.filter((city) => city?.name === cityId)[0]?.id,
postalCode, postalCode: postalCode,
location: null, location: null,
}); });
}
navigation.goBack(); navigation.goBack();
}; };
@ -387,7 +428,9 @@ const Address = ({
name="lastName" name="lastName"
maxlength="60" maxlength="60"
value={ value={
profilePage ? profileFields?.lastName : addressFields?.lastName profilePage
? profileFields?.lastName
: addressFields?.lastName
} }
onChange={(name, value) => onChange={(name, value) =>
profilePage profilePage
@ -448,7 +491,9 @@ const Address = ({
textAlign="left" textAlign="left"
name="phone" name="phone"
maxLength={11} maxLength={11}
value={profilePage ? profileFields?.phone : addressFields?.phone} value={
profilePage ? profileFields?.phone : addressFields?.phone
}
onChange={(name, value) => onChange={(name, value) =>
profilePage profilePage
? setProfileFields({ ...profileFields, phone: value }) ? setProfileFields({ ...profileFields, phone: value })
@ -468,9 +513,14 @@ const Address = ({
defaultValue={ defaultValue={
profilePage profilePage
? provinces?.filter( ? 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] )[0]?.name || province[0]
: province[0]
} }
onChange={(name, value) => onChange={(name, value) =>
value === "استان" ? {} : onChange(name, value) value === "استان" ? {} : onChange(name, value)
@ -485,9 +535,12 @@ const Address = ({
defaultValue={ defaultValue={
profilePage profilePage
? cities?.filter( ? 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] )[0]?.name || city[0]
: city[0]
} }
onChange={(name, value) => onChange={(name, value) =>
value === "شهرستان" ? {} : onChange(name, value) value === "شهرستان" ? {} : onChange(name, value)
@ -608,6 +661,7 @@ const mapDispatchToProps = {
createAddress: userAddress.add, createAddress: userAddress.add,
setProfileFields: user.setProfileFields, setProfileFields: user.setProfileFields,
setProfile: user.setProfile, setProfile: user.setProfile,
editAddress: userAddress.update,
}; };
export default connect(mapStateToProps, mapDispatchToProps)(Address); export default connect(mapStateToProps, mapDispatchToProps)(Address);

@ -109,7 +109,15 @@ const ProfileAddress = ({
</Text> </Text>
</View> </View>
<View style={tw("flex flex-row-reverse")}> <View style={tw("flex flex-row-reverse")}>
<Pressable style={tw("ml-4")}> <Pressable
style={tw("ml-4")}
onPress={() =>
navigation.navigate("CreateAddress", {
type: "editAddress",
form: address,
})
}
>
<Ionicons <Ionicons
name="ios-pencil-sharp" name="ios-pencil-sharp"
size={20} size={20}
@ -227,7 +235,15 @@ const ProfileAddress = ({
</View> </View>
{!active ? ( {!active ? (
<View style={tw("flex flex-row-reverse")}> <View style={tw("flex flex-row-reverse")}>
<Pressable style={tw("ml-4")}> <Pressable
style={tw("ml-4")}
onPress={() =>
navigation.navigate("CreateAddress", {
type: "editAddress",
form: address,
})
}
>
<Ionicons <Ionicons
name="ios-pencil-sharp" name="ios-pencil-sharp"
size={20} size={20}

Loading…
Cancel
Save