mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Add brewery location query endpoints
This commit is contained in:
@@ -37,6 +37,12 @@ export interface Brewery {
|
||||
location: BreweryLocation | null;
|
||||
}
|
||||
|
||||
export interface SimplifiedBrewery {
|
||||
breweryPostId: string;
|
||||
breweryName: string;
|
||||
location: BreweryLocation | null;
|
||||
}
|
||||
|
||||
const fetchApi = async (path: string): Promise<Response> => {
|
||||
try {
|
||||
return await fetch(`${API_BASE_URL}${path}`);
|
||||
@@ -66,6 +72,20 @@ 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 getBreweryById = async (id: string): Promise<Brewery | null> => {
|
||||
const res = await fetchApi(`/api/brewery/${encodeURIComponent(id)}`);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import RouteErrorState from '../../../components/ui/error/RouteErrorState';
|
||||
import ClientOnly from '../../../components/ClientOnly';
|
||||
import BreweryCard from '../components/BreweryCard';
|
||||
import { getBreweries, type Brewery } from '../breweries.server';
|
||||
import { getBreweries, getBreweryLocations, type SimplifiedBrewery } from '../breweries.server';
|
||||
import type { Route } from './+types/breweries';
|
||||
import { lazy, Suspense, useEffect, useRef, useState } from 'react';
|
||||
import { useFetcher } from 'react-router';
|
||||
@@ -18,13 +18,16 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
|
||||
const url = new URL(request.url);
|
||||
const limit = Number(url.searchParams.get('limit')) || PAGE_SIZE;
|
||||
const offset = Number(url.searchParams.get('offset')) || 0;
|
||||
const breweries = await getBreweries(limit, offset);
|
||||
return { breweries, limit, offset };
|
||||
const [breweries, locations] = await Promise.all([
|
||||
getBreweries(limit, offset),
|
||||
getBreweryLocations(),
|
||||
]);
|
||||
return { breweries, locations, limit, offset };
|
||||
};
|
||||
|
||||
const BreweryMap = lazy(() => import('../components/BreweryMap'));
|
||||
|
||||
const toPins = (breweries: Brewery[]): BreweryMapPin[] =>
|
||||
const toPins = (breweries: SimplifiedBrewery[]): BreweryMapPin[] =>
|
||||
breweries
|
||||
.filter((brewery) => brewery.location?.coordinates)
|
||||
.map((brewery) => ({
|
||||
@@ -64,7 +67,7 @@ const Breweries = ({ loaderData }: Route.ComponentProps) => {
|
||||
return () => observer.disconnect();
|
||||
}, [hasMore, offset, fetcher]);
|
||||
|
||||
const pins = toPins(allBreweries);
|
||||
const pins = toPins(loaderData.locations);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-base-200">
|
||||
|
||||
Reference in New Issue
Block a user