Obanana_test/app/pages/checkout/AddressSelection.jsx

336 lines
9.0 KiB
React
Raw Normal View History

2023-09-26 14:33:01 +08:00
import { faArrowLeft, faCheck } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-native-fontawesome";
2024-02-12 08:58:57 +08:00
import AsyncStorage from "@react-native-async-storage/async-storage";
import {
useIsFocused,
useNavigation,
useRoute,
} from "@react-navigation/native";
import React, { useEffect, useState } from "react";
2023-09-26 14:33:01 +08:00
import {
Dimensions,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
2024-02-12 08:58:57 +08:00
import { useToast } from "react-native-toast-notifications";
2023-09-26 14:33:01 +08:00
import Accordion from "../../components/checkout/Accordion";
2024-02-12 08:58:57 +08:00
import { get_customer } from "../../services/api/controllers/customers";
// import { user } from "../../constants/user";
2023-09-26 14:33:01 +08:00
const width = Dimensions.get("window").width;
const AddressSelection = () => {
const navigation = useNavigation();
const [selected, setselected] = useState(0);
const route = useRoute();
2024-02-12 08:58:57 +08:00
const { product, shipMethod, payMethod, orderId } = route.params;
const [user, setuser] = useState([]);
const [shippingAddress, setshippingAddress] = useState([]);
const toast = useToast();
const isFocused = useIsFocused();
useEffect(() => {
/* ---------------Check for the user saved email--------------- */
AsyncStorage.getItem("userData")
.then((pass) => {
const userDataArray = JSON.parse(pass);
// setuser(userDataArray);
const ship = userDataArray[0]?.address.find(
(address) => address?.shipping === true
);
setshippingAddress(ship);
get_customer({ id: userDataArray[0]?._id })
.then((result) => {
// console.log(result.data);
if (result.error) {
seterror(result.error);
} else {
setuser(result.data);
console.log(result.data);
// setaddreses(result.data.address);
}
})
.catch((error) => {
// setError(error.message);
console.log(error.message);
})
.finally(() => {
// setprodIsLoading(false); // Set loading to false when done loading
});
console.log(pass);
})
.catch((error) => {
console.log(error + "weeewwww");
});
}, []);
useEffect(() => {
/* ---------------Check for the user saved email--------------- */
AsyncStorage.getItem("userData")
.then((pass) => {
const userDataArray = JSON.parse(pass);
const ship = userDataArray[0]?.address.find(
(address) => address?.shipping === true
);
setshippingAddress(ship);
get_customer({ id: userDataArray[0]?._id })
.then((result) => {
// console.log(result.data);
if (result.error) {
seterror(result.error);
} else {
setuser(result.data);
// setaddreses(result.data.address);
}
})
.catch((error) => {
// setError(error.message);
console.log(error.message);
})
.finally(() => {
// setprodIsLoading(false); // Set loading to false when done loading
});
console.log(pass);
})
.catch((error) => {
console.log(error + "weeewwww");
});
}, [isFocused]);
2023-09-26 14:33:01 +08:00
const Address = [
{
address: "Blk 2 Wall St. Harang, Makati City",
recipient: "Juan Dela Cruz",
number: "09123456789",
},
{
address: "123 Wait St. Dahil, Makati City",
recipient: "San Dali Lang",
number: "09234567890",
},
];
2024-02-12 08:58:57 +08:00
// const shippingAddress = user[0].address.find(
// (address) => address.shipping === true
// );
// const billingAddress = user[0].address.find((address) => address.billing === true);
console.log(shippingAddress);
2023-09-26 14:33:01 +08:00
return (
<View style={styles.container}>
<View style={styles.header}>
<TouchableOpacity
onPress={() =>
navigation.navigate("CheckOut", {
shipMethod: shipMethod,
product,
payMethod,
2024-02-12 08:58:57 +08:00
orderId: orderId,
2023-09-26 14:33:01 +08:00
})
}
style={styles.backIcon}
>
<FontAwesomeIcon icon={faArrowLeft} color={"#d4c600"} size={25} />
</TouchableOpacity>
<Text style={styles.headerText}>Select Address</Text>
</View>
<View style={styles.wrapper}>
2024-02-12 08:58:57 +08:00
{user?.address?.map((add, i) => (
2023-09-26 14:33:01 +08:00
<TouchableOpacity
key={i}
style={styles.listItem}
onPress={() => setselected(i)}
>
<View style={styles.listleft}>
<View style={styles.listtop}>
<Text style={styles.listItemText}>Recipient: </Text>
<Text style={styles.listItemTextPrice}>
2024-02-12 08:58:57 +08:00
{add.first_name} {add.last_name}, {add.phone}
2023-09-26 14:33:01 +08:00
</Text>
</View>
<Text style={styles.listItemText}>Address:</Text>
<Text style={styles.listItemTexteta} numberOfLines={4}>
{" "}
2024-02-12 08:58:57 +08:00
{add.address_1 +
" " +
add.address_2 +
", " +
add.city +
", " +
add.province +
" "}
{add.country === "PH" ? "Philippines" : add.country}
2023-09-26 14:33:01 +08:00
</Text>
</View>
{selected === i ? (
<FontAwesomeIcon icon={faCheck} color={"#d4c600"} size={25} />
) : null}
</TouchableOpacity>
))}
2024-02-12 08:58:57 +08:00
<TouchableOpacity
style={styles.footerBtn1}
onPress={() => {
// toast.show("Successfully Changed Address!", {
// type: "success",
// placement: "top",
// duration: 2000,
// offset: 30,
// animationType: "slide-in",
// });
navigation.navigate("AddressCreate1", {
// isrefresh: !refresh,
address: user?.address,
// shipMethod: shipMethod,
// product,
// payMethod,
// orderId: orderId,
});
}}
>
<Text style={styles.footerBtnText1}>Add New Address +</Text>
</TouchableOpacity>
2023-09-26 14:33:01 +08:00
</View>
<View style={styles.footer}>
<TouchableOpacity
style={styles.footerBtn}
2024-02-12 08:58:57 +08:00
onPress={() => {
toast.show("Successfully Changed Address!", {
type: "success",
placement: "top",
duration: 2000,
offset: 30,
animationType: "slide-in",
});
2023-09-26 14:33:01 +08:00
navigation.navigate("CheckOut", {
2024-02-12 08:58:57 +08:00
shipMethod: shipMethod,
2023-09-26 14:33:01 +08:00
product,
payMethod,
2024-02-12 08:58:57 +08:00
address: user.address[selected],
orderId: orderId,
});
}}
2023-09-26 14:33:01 +08:00
>
<Text style={styles.footerBtnText}>Confirm</Text>
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: "#fafafa",
height: "100%",
width: width,
},
header: {
alignItems: "center",
width: "100%",
top: 0,
paddingLeft: 15,
backgroundColor: "#ffffff",
flexDirection: "row",
borderColor: "#ddd",
paddingBottom: 15,
borderBottomWidth: 1,
},
headerText: {
fontSize: 16,
fontWeight: "600",
marginLeft: 25,
},
headerWrap: {
justifyContent: "center",
alignItems: "center",
width: "100%",
height: "100%",
// flexDirection: "row",
},
footer: {
position: "absolute",
bottom: 0,
width: "100%",
justifyContent: "center",
alignItems: "center",
paddingVertical: 10,
},
footerBtn: {
backgroundColor: "#ff5e00",
2024-02-12 08:58:57 +08:00
borderRadius: 20,
2023-09-26 14:33:01 +08:00
width: "90%",
paddingVertical: 10,
justifyContent: "center",
alignItems: "center",
},
footerBtnText: {
color: "#fff",
fontSize: 16,
},
2024-02-12 08:58:57 +08:00
footerBtn1: {
// backgroundColor: "",
borderRadius: 10,
borderWidth: 1,
borderColor: "#ff5e00",
width: "90%",
paddingVertical: 15,
marginTop: 10,
justifyContent: "center",
alignItems: "center",
},
footerBtnText1: {
color: "#ff5e00",
fontSize: 16,
},
2023-09-26 14:33:01 +08:00
wrapper: {
// height: "85%",
paddingTop: 25,
width: "100%",
// justifyContent: "center",
alignItems: "center",
// height: "100%",
},
content: {
width: "100%",
justifyContent: "center",
alignItems: "center",
},
listItem: {
backgroundColor: "#ffffff",
width: "100%",
borderWidth: 1,
borderColor: "#ececec",
padding: 15,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
},
listleft: {},
listtop: {
flexDirection: "row",
paddingBottom: 10,
},
listItemText: {
fontSize: 16,
fontWeight: "600",
},
listItemTextPrice: {
fontSize: 16,
fontWeight: "600",
color: "#696969",
marginLeft: 10,
},
listItemTexteta: {
fontSize: 14,
marginTop: 5,
fontWeight: "500",
color: "#818181",
marginLeft: 10,
},
});
export default AddressSelection;