2026-02-02 20:34:35 +00:00
|
|
|
/**
|
|
|
|
|
* Home Page (SSR)
|
|
|
|
|
* Main landing page with school search and browsing
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
import { fetchSchools, fetchFilters, fetchDataInfo } from '@/lib/api';
|
2026-02-02 20:34:35 +00:00
|
|
|
import { HomeView } from '@/components/HomeView';
|
|
|
|
|
|
|
|
|
|
interface HomePageProps {
|
2026-02-02 22:09:17 +00:00
|
|
|
searchParams: Promise<{
|
2026-02-02 20:34:35 +00:00
|
|
|
search?: string;
|
|
|
|
|
local_authority?: string;
|
|
|
|
|
school_type?: string;
|
|
|
|
|
page?: string;
|
|
|
|
|
postcode?: string;
|
|
|
|
|
radius?: string;
|
2026-02-02 22:09:17 +00:00
|
|
|
}>;
|
2026-02-02 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const metadata = {
|
|
|
|
|
title: 'Home',
|
|
|
|
|
description: 'Search and compare primary school KS2 performance across England',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Force dynamic rendering (no static generation at build time)
|
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
|
|
|
|
|
export default async function HomePage({ searchParams }: HomePageProps) {
|
2026-02-02 22:09:17 +00:00
|
|
|
// Await search params (Next.js 15 requirement)
|
|
|
|
|
const params = await searchParams;
|
|
|
|
|
|
2026-02-02 20:34:35 +00:00
|
|
|
// Parse search params
|
2026-02-02 22:09:17 +00:00
|
|
|
const page = parseInt(params.page || '1');
|
2026-02-02 22:21:55 +00:00
|
|
|
const radius = params.radius ? parseFloat(params.radius) : undefined;
|
|
|
|
|
|
|
|
|
|
// Check if user has performed a search
|
|
|
|
|
const hasSearchParams = !!(
|
|
|
|
|
params.search ||
|
|
|
|
|
params.local_authority ||
|
|
|
|
|
params.school_type ||
|
|
|
|
|
params.postcode
|
|
|
|
|
);
|
2026-02-02 20:34:35 +00:00
|
|
|
|
2026-02-02 21:28:50 +00:00
|
|
|
// Fetch data on server with error handling
|
|
|
|
|
try {
|
2026-03-23 21:31:28 +00:00
|
|
|
const [filtersData, dataInfo] = await Promise.all([fetchFilters(), fetchDataInfo().catch(() => null)]);
|
2026-02-02 22:21:55 +00:00
|
|
|
|
|
|
|
|
// Only fetch schools if there are search parameters
|
|
|
|
|
let schoolsData;
|
|
|
|
|
if (hasSearchParams) {
|
|
|
|
|
schoolsData = await fetchSchools({
|
2026-02-02 22:09:17 +00:00
|
|
|
search: params.search,
|
|
|
|
|
local_authority: params.local_authority,
|
|
|
|
|
school_type: params.school_type,
|
|
|
|
|
postcode: params.postcode,
|
2026-02-02 21:28:50 +00:00
|
|
|
radius,
|
|
|
|
|
page,
|
|
|
|
|
page_size: 50,
|
2026-02-02 22:21:55 +00:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Empty state by default
|
|
|
|
|
schoolsData = { schools: [], page: 1, page_size: 50, total: 0, total_pages: 0 };
|
|
|
|
|
}
|
2026-02-02 21:28:50 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<HomeView
|
|
|
|
|
initialSchools={schoolsData}
|
2026-02-02 21:51:08 +00:00
|
|
|
filters={filtersData || { local_authorities: [], school_types: [], years: [] }}
|
2026-03-23 21:31:28 +00:00
|
|
|
totalSchools={dataInfo?.total_schools ?? null}
|
2026-02-02 21:28:50 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching data for home page:', error);
|
|
|
|
|
|
|
|
|
|
// Return error state with empty data
|
|
|
|
|
return (
|
|
|
|
|
<HomeView
|
2026-02-02 21:34:24 +00:00
|
|
|
initialSchools={{ schools: [], page: 1, page_size: 50, total: 0, total_pages: 0 }}
|
2026-02-02 21:28:50 +00:00
|
|
|
filters={{ local_authorities: [], school_types: [], years: [] }}
|
2026-03-23 21:31:28 +00:00
|
|
|
totalSchools={null}
|
2026-02-02 21:28:50 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-02-02 20:34:35 +00:00
|
|
|
}
|