)}
{/* Saved addresses */}
{addresses.length > 0 && !showNewAddr && (
)}
{/* New address form */}
{(showNewAddr || addresses.length === 0) && (
setNewAddr(a=>({...a,line1:e.target.value}))} placeholder="Flat, building, street"/>
setNewAddr(a=>({...a,line2:e.target.value}))} placeholder="Area, landmark"/>
{
// Use functional update to get fresh state
setNewAddr(a => {
const updated = {...a, city};
// Reverse pincode lookup with current state
if (city && a.state) {
fetch('https://api.postalpincode.in/postoffice/' + encodeURIComponent(city))
.then(r => r.json())
.then(data => {
const offices = data?.[0]?.PostOffice || [];
const match = offices.find(o => o.State === a.state) || offices[0];
if (match?.Pincode) setNewAddr(prev => ({...prev, city, pincode: match.Pincode}));
}).catch(()=>{});
}
return updated;
});
}}
/>
setNewAddr(a => ({
...a,
pincode: pin,
// null means "don't change", '' means "clear", string means "set"
...(autoCity !== null ? { city: autoCity||a.city } : {}),
...(autoState !== null ? { state: autoState||a.state } : {}),
}))}
/>
{showNewAddr &&
}