mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Add brewery discovery & detail pages with location-based search (#253)
This commit is contained in:
@@ -7,13 +7,24 @@ interface ApiResponse<T> {
|
||||
payload: T;
|
||||
}
|
||||
|
||||
export interface BreweryCoordinates {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
|
||||
export interface BreweryLocation {
|
||||
breweryPostLocationId: string;
|
||||
breweryPostId: string;
|
||||
cityId: string;
|
||||
cityName: string;
|
||||
stateProvinceName: string;
|
||||
stateProvinceCode: string;
|
||||
countryName: string;
|
||||
countryCode: string;
|
||||
addressLine1: string;
|
||||
addressLine2: string | null;
|
||||
postalCode: string;
|
||||
coordinates: BreweryCoordinates | null;
|
||||
}
|
||||
|
||||
export interface Brewery {
|
||||
@@ -26,6 +37,13 @@ export interface Brewery {
|
||||
location: BreweryLocation | null;
|
||||
}
|
||||
|
||||
export interface SimplifiedBrewery {
|
||||
breweryPostId: string;
|
||||
breweryName: string;
|
||||
location: BreweryLocation | null;
|
||||
distanceMetres: number | null;
|
||||
}
|
||||
|
||||
const fetchApi = async (path: string): Promise<Response> => {
|
||||
try {
|
||||
return await fetch(`${API_BASE_URL}${path}`);
|
||||
@@ -55,6 +73,44 @@ export const getBreweries = async (limit?: number, offset?: number): Promise<Bre
|
||||
return body.payload;
|
||||
};
|
||||
|
||||
export const getBreweryLocations = async (): Promise<SimplifiedBrewery[]> => {
|
||||
const res = await fetchApi('/api/brewery/locations');
|
||||
|
||||
if (!res.ok) {
|
||||
throw data(`Failed to load brewery locations (${res.status}).`, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
});
|
||||
}
|
||||
|
||||
const body: ApiResponse<SimplifiedBrewery[]> = await res.json();
|
||||
return body.payload;
|
||||
};
|
||||
|
||||
export const getBreweryLocationsNearby = async (
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
rangeInMetres: number,
|
||||
): Promise<SimplifiedBrewery[]> => {
|
||||
const params = new URLSearchParams({
|
||||
latitude: String(latitude),
|
||||
longitude: String(longitude),
|
||||
rangeInMetres: String(rangeInMetres),
|
||||
});
|
||||
|
||||
const res = await fetchApi(`/api/brewery/locations/nearby?${params}`);
|
||||
|
||||
if (!res.ok) {
|
||||
throw data(`Failed to load nearby breweries (${res.status}).`, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
});
|
||||
}
|
||||
|
||||
const body: ApiResponse<SimplifiedBrewery[]> = await res.json();
|
||||
return body.payload;
|
||||
};
|
||||
|
||||
export const getBreweryById = async (id: string): Promise<Brewery | null> => {
|
||||
const res = await fetchApi(`/api/brewery/${encodeURIComponent(id)}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user