|
| 1 | +import { Ionicons } from "@expo/vector-icons"; |
| 2 | +import React, { useEffect, useState } from "react"; |
| 3 | +import { ScrollView, Text, TouchableOpacity, View, Modal, TextInput, ActivityIndicator } from "react-native"; |
| 4 | +import { SafeAreaView } from "react-native-safe-area-context"; |
| 5 | +import Header from "@/components/Header"; |
| 6 | +import { COLORS } from "@/constants"; |
| 7 | +import type { Address } from "@/constants/types"; |
| 8 | +import { dummyAddress } from "@/assets/assets"; |
| 9 | + |
| 10 | +export default function Addresses() { |
| 11 | + const [addresses, setAddresses] = useState<Address[]>([]); |
| 12 | + const [loading, setLoading] = useState(true); |
| 13 | + const [modalVisible, setModalVisible] = useState(false); |
| 14 | + |
| 15 | + // Form state |
| 16 | + const [type, setType] = useState("Home"); |
| 17 | + const [street, setStreet] = useState(""); |
| 18 | + const [city, setCity] = useState(""); |
| 19 | + const [state, setState] = useState(""); |
| 20 | + const [zipCode, setZipCode] = useState(""); |
| 21 | + const [country, setCountry] = useState(""); |
| 22 | + const [isDefault, setIsDefault] = useState(false); |
| 23 | + const [submitting, setSubmitting] = useState(false); |
| 24 | + |
| 25 | + // Edit state |
| 26 | + const [isEditing, setIsEditing] = useState(false); |
| 27 | + const [editingId, setEditingId] = useState<string | null>(null); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + fetchAddresses(); |
| 31 | + }, []); |
| 32 | + |
| 33 | + const fetchAddresses = async () => { |
| 34 | + setAddresses(dummyAddress as any); |
| 35 | + setLoading(false); |
| 36 | + }; |
| 37 | + |
| 38 | + const handleEditSearch = (item: Address) => { |
| 39 | + setIsEditing(true); |
| 40 | + setEditingId(item._id); |
| 41 | + setType(item.type); |
| 42 | + setStreet(item.street); |
| 43 | + setCity(item.city); |
| 44 | + setState(item.state); |
| 45 | + setZipCode(item.zipCode); |
| 46 | + setCountry(item.country); |
| 47 | + setIsDefault(item.isDefault); |
| 48 | + setModalVisible(true); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleSaveAddress = async () => { |
| 52 | + setModalVisible(false); |
| 53 | + resetForm(); |
| 54 | + fetchAddresses(); |
| 55 | + }; |
| 56 | + |
| 57 | + const handleDeleteAddress = async (id: string) => { |
| 58 | + |
| 59 | + }; |
| 60 | + |
| 61 | + const resetForm = () => { |
| 62 | + setStreet(""); |
| 63 | + setCity(""); |
| 64 | + setState(""); |
| 65 | + setZipCode(""); |
| 66 | + setCountry(""); |
| 67 | + setType("Home"); |
| 68 | + setIsDefault(false); |
| 69 | + setIsEditing(false); |
| 70 | + setEditingId(null); |
| 71 | + }; |
| 72 | + |
| 73 | + const openAddModal = () => { |
| 74 | + resetForm(); |
| 75 | + setModalVisible(true); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <SafeAreaView className="flex-1 bg-surface" edges={['top']}> |
| 80 | + <Header title="Shipping Addresses" showBack /> |
| 81 | + |
| 82 | + {loading ? ( |
| 83 | + <View className="flex-1 justify-center items-center"> |
| 84 | + <ActivityIndicator size="large" color={COLORS.primary} /> |
| 85 | + </View> |
| 86 | + ) : ( |
| 87 | + <ScrollView className="flex-1 px-4 pt-4"> |
| 88 | + {addresses.length === 0 ? ( |
| 89 | + <Text className="text-center text-secondary mt-10">No addresses found</Text> |
| 90 | + ) : ( |
| 91 | + addresses.map((item) => ( |
| 92 | + <View key={item._id} className="bg-white p-4 rounded-xl mb-4 shadow-sm"> |
| 93 | + <View className="flex-row items-center justify-between mb-2"> |
| 94 | + <View className="flex-row items-center"> |
| 95 | + <Ionicons |
| 96 | + name={item.type === "Home" ? "home-outline" : "briefcase-outline"} |
| 97 | + size={20} |
| 98 | + color={COLORS.primary} |
| 99 | + /> |
| 100 | + <Text className="text-base font-bold text-primary ml-2">{item.type}</Text> |
| 101 | + {item.isDefault && ( |
| 102 | + <View className="bg-primary/10 px-2 py-1 rounded ml-2"> |
| 103 | + <Text className="text-primary text-xs font-bold">Default</Text> |
| 104 | + </View> |
| 105 | + )} |
| 106 | + </View> |
| 107 | + <View className="flex-row items-center gap-4"> |
| 108 | + <TouchableOpacity onPress={() => handleEditSearch(item)}> |
| 109 | + <Ionicons name="pencil-outline" size={20} color={COLORS.secondary} /> |
| 110 | + </TouchableOpacity> |
| 111 | + <TouchableOpacity onPress={() => handleDeleteAddress(item._id)}> |
| 112 | + <Ionicons name="trash-outline" size={20} color={COLORS.error || '#ff4444'} /> |
| 113 | + </TouchableOpacity> |
| 114 | + </View> |
| 115 | + </View> |
| 116 | + <Text className="text-secondary leading-5 ml-7"> |
| 117 | + {item.street}, {item.city}, {item.state} {item.zipCode}, {item.country} |
| 118 | + </Text> |
| 119 | + </View> |
| 120 | + )) |
| 121 | + )} |
| 122 | + |
| 123 | + <TouchableOpacity className="flex-row items-center justify-center p-4 border border-dashed border-gray-300 rounded-xl mt-2 mb-8" onPress={openAddModal}> |
| 124 | + <Ionicons name="add" size={24} color={COLORS.secondary} /> |
| 125 | + <Text className="text-secondary font-medium ml-2">Add New Address</Text> |
| 126 | + </TouchableOpacity> |
| 127 | + </ScrollView> |
| 128 | + )} |
| 129 | + |
| 130 | + {/* Add Address Modal */} |
| 131 | + <Modal animationType="slide" transparent={true} visible={modalVisible} onRequestClose={() => setModalVisible(false)}> |
| 132 | + <View className="flex-1 justify-end bg-black/50"> |
| 133 | + <View className="bg-white rounded-t-3xl p-6 h-[85%]"> |
| 134 | + <View className="flex-row justify-between items-center mb-6"> |
| 135 | + <Text className="text-xl font-bold text-primary">{isEditing ? "Edit Address" : "Add New Address"}</Text> |
| 136 | + <TouchableOpacity onPress={() => setModalVisible(false)}> |
| 137 | + <Ionicons name="close" size={24} color={COLORS.primary} /> |
| 138 | + </TouchableOpacity> |
| 139 | + </View> |
| 140 | + |
| 141 | + <ScrollView showsVerticalScrollIndicator={false}> |
| 142 | + <Text className="text-primary font-medium mb-2">Label</Text> |
| 143 | + <View className="flex-row gap-3 mb-4"> |
| 144 | + {["Home", "Work", "Other"].map((t) => ( |
| 145 | + <TouchableOpacity key={t} onPress={() => setType(t)} className={`px-4 py-2 rounded-full border ${type === t ? 'bg-primary border-primary' : 'bg-white border-gray-300'}`}> |
| 146 | + <Text className={type === t ? 'text-white' : 'text-primary'}>{t}</Text> |
| 147 | + </TouchableOpacity> |
| 148 | + ))} |
| 149 | + </View> |
| 150 | + |
| 151 | + <Text className="text-primary font-medium mb-2">Street Address</Text> |
| 152 | + <TextInput className="bg-surface p-4 rounded-xl text-primary mb-4" placeholder="123 Main St" value={street} onChangeText={setStreet} /> |
| 153 | + |
| 154 | + <View className="flex-row gap-4 mb-4"> |
| 155 | + <View className="flex-1"> |
| 156 | + <Text className="text-primary font-medium mb-2">City</Text> |
| 157 | + <TextInput className="bg-surface p-4 rounded-xl text-primary" placeholder="New York" value={city} onChangeText={setCity} /> |
| 158 | + </View> |
| 159 | + <View className="flex-1"> |
| 160 | + <Text className="text-primary font-medium mb-2">State</Text> |
| 161 | + <TextInput className="bg-surface p-4 rounded-xl text-primary" placeholder="NY" value={state} onChangeText={setState} /> |
| 162 | + </View> |
| 163 | + </View> |
| 164 | + |
| 165 | + <View className="flex-row gap-4 mb-4"> |
| 166 | + <View className="flex-1"> |
| 167 | + <Text className="text-primary font-medium mb-2">Zip Code</Text> |
| 168 | + <TextInput className="bg-surface p-4 rounded-xl text-primary" placeholder="10001" value={zipCode} onChangeText={setZipCode} keyboardType="numeric" /> |
| 169 | + </View> |
| 170 | + <View className="flex-1"> |
| 171 | + <Text className="text-primary font-medium mb-2">Country</Text> |
| 172 | + <TextInput className="bg-surface p-4 rounded-xl text-primary" placeholder="USA" value={country} onChangeText={setCountry} /> |
| 173 | + </View> |
| 174 | + </View> |
| 175 | + |
| 176 | + <TouchableOpacity className="flex-row items-center mb-8" onPress={() => setIsDefault(!isDefault)}> |
| 177 | + <View className={`w-5 h-5 border rounded mr-2 items-center justify-center ${isDefault ? 'bg-primary border-primary' : 'border-gray-300'}`}> |
| 178 | + {isDefault && <Ionicons name="checkmark" size={14} color="white" />} |
| 179 | + </View> |
| 180 | + <Text className="text-primary">Set as default address</Text> |
| 181 | + </TouchableOpacity> |
| 182 | + |
| 183 | + <TouchableOpacity className="w-full bg-primary py-4 rounded-full items-center mb-10" onPress={handleSaveAddress} disabled={submitting} > |
| 184 | + {submitting ? ( |
| 185 | + <ActivityIndicator color="white" /> |
| 186 | + ) : ( |
| 187 | + <Text className="text-white font-bold text-lg">Save Address</Text> |
| 188 | + )} |
| 189 | + </TouchableOpacity> |
| 190 | + </ScrollView> |
| 191 | + </View> |
| 192 | + </View> |
| 193 | + </Modal> |
| 194 | + </SafeAreaView> |
| 195 | + ); |
| 196 | +} |
0 commit comments