import {
View,
Text,
SafeAreaView,
ScrollView,
StyleSheet,
StatusBar,
Platform,
Dimensions,
Appearance,
} from "react-native";
import React, { useEffect, useState } from "react";
import { connect } from "react-redux";
import { publicApi, userAddress } from "../../redux/actions";
import { useNavigation } from "@react-navigation/native";
import { asyncAwesomeAlert } from "../../utils/AsyncWrappers";
//components
import Navbar from "../../components/Navbar";
import CustomPressable from "../../components/Pressable";
import CustomTextInput from "../../components/CustomTextInput";
import Select from "../../components/Select";
import MapSelector from "../../components/MapSelector";
//style
import tw from "tailwind-rn";
import Colors from "../../constants/Colors";
import fontSize from "../../constants/fontSize";
const ios = Platform.OS === "ios";
const Address = ({
city,
province,
getCity,
getProvince,
provinces,
cities,
createAddress,
loading,
setPhase,
cityLoading,
}) => {
const colorScheme = Appearance.getColorScheme();
const navigation = useNavigation();
const [addressFields, setAddressFields] = useState({});
const [mapAddress, setMapAddress] = useState(null);
useEffect(() => {
getProvince();
}, []);
useEffect(() => {
if (addressFields?.provinceId) {
getCity({
provinceId: provinces.filter(
(province) => province.name === addressFields?.provinceId
)[0].id,
});
}
}, [addressFields]);
const onChange = (name, value) => {
setAddressFields({ ...addressFields, [name]: value });
};
const submit = () => {
const {
firstName,
lastName,
cellphone,
phone,
address,
provinceId,
cityId,
postalCode,
} = addressFields;
if (!firstName) {
return asyncAwesomeAlert("خطا", "نام خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!lastName) {
return asyncAwesomeAlert("خطا", "نام خانوادگی خود را وارد کنید", {
showCancelButton: false,
});
}
if (!cellphone) {
return asyncAwesomeAlert("خطا", "شماره موبایل خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!phone) {
return asyncAwesomeAlert("خطا", "شماره تلفن خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!provinceId) {
return asyncAwesomeAlert("خطا", "نام استان خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!cityId) {
return asyncAwesomeAlert("خطا", "نام شهرستان خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!postalCode) {
return asyncAwesomeAlert("خطا", "کد پستی خود را وارد کنید.", {
showCancelButton: false,
});
}
if (!mapAddress) {
return asyncAwesomeAlert("خطا", "آدرس خود را از روی نقشه انتخاب کن", {
showCancelButton: false,
});
}
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,
});
navigation.goBack();
};
const Header = ({ title, text }) => {
return (
{title}
{text}
);
};
return (
{/* */}
onChange(name, value)}
autoFocus={false}
regex={(val) => val}
/>
onChange(name, value)}
autoFocus={false}
regex={(val) => val}
/>
onChange(name, value)}
autoFocus={false}
regex={(val) => val}
/>
onChange(name, value)}
autoFocus={false}
regex={(val) => val}
/>
onChange(name, value)}
autoFocus={false}
regex={(val) => val}
/>
navigation.push("Address")}
/>
);
};
const styles = StyleSheet.create({
contentContainerStyle: {
paddingBottom: 100,
},
container: {
width: Dimensions.get("window").width,
backgroundColor: "white",
flexDirection: "column",
paddingVertical: 5,
paddingHorizontal: 16,
},
});
const mapStateToProps = (state) => ({
provinces: state.publicApi.provinces,
province: state.publicApi.province,
cities: state.publicApi.cities,
city: state.publicApi.city,
loading: state.userAddress.loading,
cityLoading: state.publicApi.loading,
});
const mapDispatchToProps = {
getProvince: publicApi.getProvince,
getCity: publicApi.getCity,
createAddress: userAddress.add,
};
export default connect(mapStateToProps, mapDispatchToProps)(Address);