85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import { useState } from 'react';
|
|
|
|
export interface PostalCodeResult {
|
|
city: string;
|
|
state: string;
|
|
success: boolean;
|
|
}
|
|
|
|
export const usePostalCodeLookup = () => {
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const lookupPostalCode = async (
|
|
postalCode: string,
|
|
countryCode: string
|
|
): Promise<PostalCodeResult> => {
|
|
if (!postalCode || postalCode.length < 3) {
|
|
return { city: '', state: '', success: false };
|
|
}
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
|
if (countryCode.toUpperCase() === 'IN' && postalCode.length === 6) {
|
|
const response = await fetch(
|
|
`https://api.postalpincode.in/pincode/${postalCode}`
|
|
);
|
|
const data = await response.json();
|
|
|
|
if (data[0]?.Status === 'Success' && data[0]?.PostOffice?.length > 0) {
|
|
const { District, State } = data[0].PostOffice[0];
|
|
return {
|
|
city: District,
|
|
state: State,
|
|
success: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
const zipResponse = await fetch(
|
|
`https://api.zippopotam.us/${countryCode.toLowerCase()}/${postalCode}`
|
|
);
|
|
|
|
if (zipResponse.ok) {
|
|
const data = await zipResponse.json();
|
|
|
|
if (data && data.places && data.places.length > 0) {
|
|
let selectedPlace = data.places[0];
|
|
|
|
if (data.places.length > 1) {
|
|
const cityPlace = data.places.find((place: any) => {
|
|
const placeName = place['place name'] || '';
|
|
return placeName.split(' ').length <= 2;
|
|
});
|
|
if (cityPlace) {
|
|
selectedPlace = cityPlace;
|
|
}
|
|
}
|
|
|
|
const city = selectedPlace['place name'] || '';
|
|
const state =
|
|
selectedPlace['state'] || selectedPlace['state abbreviation'] || '';
|
|
|
|
return {
|
|
city,
|
|
state,
|
|
success: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
return { city: '', state: '', success: false };
|
|
|
|
} catch (error) {
|
|
return { city: '', state: '', success: false };
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return {
|
|
lookupPostalCode,
|
|
loading,
|
|
};
|
|
};
|