2026-02-02 20:34:35 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* HomeView Component
|
|
|
|
|
|
* Client-side home page view with search and filtering
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
'use client';
|
|
|
|
|
|
|
2026-03-05 09:33:47 +00:00
|
|
|
|
import { useState, useEffect } from 'react';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
import { useSearchParams } from 'next/navigation';
|
|
|
|
|
|
import { FilterBar } from './FilterBar';
|
2026-03-23 22:32:33 +00:00
|
|
|
|
import { SchoolRow } from './SchoolRow';
|
2026-02-04 10:05:31 +00:00
|
|
|
|
import { SchoolMap } from './SchoolMap';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
import { Pagination } from './Pagination';
|
|
|
|
|
|
import { EmptyState } from './EmptyState';
|
|
|
|
|
|
import { useComparisonContext } from '@/context/ComparisonContext';
|
2026-02-04 10:05:31 +00:00
|
|
|
|
import type { SchoolsResponse, Filters, School } from '@/lib/types';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
import styles from './HomeView.module.css';
|
|
|
|
|
|
|
|
|
|
|
|
interface HomeViewProps {
|
|
|
|
|
|
initialSchools: SchoolsResponse;
|
|
|
|
|
|
filters: Filters;
|
2026-03-23 21:31:28 +00:00
|
|
|
|
totalSchools?: number | null;
|
2026-02-02 20:34:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
|
export function HomeView({ initialSchools, filters, totalSchools }: HomeViewProps) {
|
2026-02-02 20:34:35 +00:00
|
|
|
|
const searchParams = useSearchParams();
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const { addSchool, removeSchool, selectedSchools } = useComparisonContext();
|
2026-02-04 10:05:31 +00:00
|
|
|
|
const [resultsView, setResultsView] = useState<'list' | 'map'>('list');
|
2026-03-05 09:33:47 +00:00
|
|
|
|
const [selectedMapSchool, setSelectedMapSchool] = useState<School | null>(null);
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const [sortOrder, setSortOrder] = useState<string>('default');
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
const hasSearch = searchParams.get('search') || searchParams.get('postcode');
|
|
|
|
|
|
const isLocationSearch = !!searchParams.get('postcode');
|
2026-03-05 13:00:34 +00:00
|
|
|
|
const isSearchActive = !!(hasSearch || searchParams.get('local_authority') || searchParams.get('school_type'));
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
2026-03-05 09:33:47 +00:00
|
|
|
|
// Close bottom sheet if we change views or search
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
setSelectedMapSchool(null);
|
|
|
|
|
|
}, [resultsView, searchParams]);
|
|
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const sortedSchools = [...initialSchools.schools].sort((a, b) => {
|
|
|
|
|
|
if (sortOrder === 'rwm_desc') return (b.rwm_expected_pct ?? -Infinity) - (a.rwm_expected_pct ?? -Infinity);
|
|
|
|
|
|
if (sortOrder === 'rwm_asc') return (a.rwm_expected_pct ?? Infinity) - (b.rwm_expected_pct ?? Infinity);
|
|
|
|
|
|
if (sortOrder === 'distance') return (a.distance ?? Infinity) - (b.distance ?? Infinity);
|
|
|
|
|
|
if (sortOrder === 'name_asc') return a.school_name.localeCompare(b.school_name);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-02 20:34:35 +00:00
|
|
|
|
return (
|
|
|
|
|
|
<div className={styles.homeView}>
|
2026-02-04 09:54:27 +00:00
|
|
|
|
{/* Combined Hero + Search and Filters */}
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{!isSearchActive && (
|
|
|
|
|
|
<div className={styles.heroSection}>
|
|
|
|
|
|
<h1 className={styles.heroTitle}>Compare Primary School Performance</h1>
|
|
|
|
|
|
<p className={styles.heroDescription}>Search and compare KS2 results for thousands of schools across England</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-02-04 09:54:27 +00:00
|
|
|
|
<FilterBar
|
|
|
|
|
|
filters={filters}
|
2026-03-05 13:00:34 +00:00
|
|
|
|
isHero={!isSearchActive}
|
2026-02-04 09:54:27 +00:00
|
|
|
|
/>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{/* Discovery section shown on landing page before any search */}
|
|
|
|
|
|
{!isSearchActive && initialSchools.schools.length === 0 && (
|
|
|
|
|
|
<div className={styles.discoverySection}>
|
|
|
|
|
|
{totalSchools && <p className={styles.discoveryCount}><strong>{totalSchools.toLocaleString()}+</strong> primary schools across England</p>}
|
|
|
|
|
|
<p className={styles.discoveryHints}>Try searching for a school name, or enter a postcode to find schools near you.</p>
|
|
|
|
|
|
<div className={styles.quickSearches}>
|
|
|
|
|
|
<span className={styles.quickSearchLabel}>Quick searches:</span>
|
|
|
|
|
|
{['Manchester', 'Bristol', 'Leeds', 'Birmingham'].map(city => (
|
|
|
|
|
|
<a key={city} href={`/?search=${city}`} className={styles.quickSearchChip}>{city}</a>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-02-04 10:05:31 +00:00
|
|
|
|
{/* Location Info Banner with View Toggle */}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
{isLocationSearch && initialSchools.location_info && (
|
2026-02-04 10:05:31 +00:00
|
|
|
|
<div className={styles.locationBannerWrapper}>
|
|
|
|
|
|
<div className={styles.locationBanner}>
|
|
|
|
|
|
<span>
|
|
|
|
|
|
Showing schools within {(initialSchools.location_info.radius / 1.60934).toFixed(1)} miles of{' '}
|
|
|
|
|
|
<strong>{initialSchools.location_info.postcode}</strong>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{initialSchools.schools.length > 0 && (
|
|
|
|
|
|
<div className={styles.viewToggle}>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={`${styles.viewToggleBtn} ${resultsView === 'list' ? styles.active : ''}`}
|
|
|
|
|
|
onClick={() => setResultsView('list')}
|
|
|
|
|
|
>
|
|
|
|
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16">
|
|
|
|
|
|
<line x1="8" y1="6" x2="21" y2="6"/>
|
|
|
|
|
|
<line x1="8" y1="12" x2="21" y2="12"/>
|
|
|
|
|
|
<line x1="8" y1="18" x2="21" y2="18"/>
|
|
|
|
|
|
<line x1="3" y1="6" x2="3.01" y2="6"/>
|
|
|
|
|
|
<line x1="3" y1="12" x2="3.01" y2="12"/>
|
|
|
|
|
|
<line x1="3" y1="18" x2="3.01" y2="18"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
List
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={`${styles.viewToggleBtn} ${resultsView === 'map' ? styles.active : ''}`}
|
|
|
|
|
|
onClick={() => setResultsView('map')}
|
|
|
|
|
|
>
|
|
|
|
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16">
|
|
|
|
|
|
<path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/>
|
|
|
|
|
|
<circle cx="12" cy="10" r="3"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
Map
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* Results Section */}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
<section className={`${styles.results} ${resultsView === 'map' && isLocationSearch ? styles.mapViewResults : ''}`}>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
{!hasSearch && initialSchools.schools.length > 0 && (
|
|
|
|
|
|
<div className={styles.sectionHeader}>
|
|
|
|
|
|
<h2>Featured Schools</h2>
|
|
|
|
|
|
<p className={styles.sectionDescription}>
|
|
|
|
|
|
Explore schools from across England
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-02-04 10:05:31 +00:00
|
|
|
|
{hasSearch && resultsView === 'list' && (
|
2026-02-02 20:34:35 +00:00
|
|
|
|
<div className={styles.resultsHeader}>
|
2026-03-25 20:28:03 +00:00
|
|
|
|
<h2 aria-live="polite" aria-atomic="true">
|
2026-02-02 20:34:35 +00:00
|
|
|
|
{initialSchools.total.toLocaleString()} school
|
|
|
|
|
|
{initialSchools.total !== 1 ? 's' : ''} found
|
|
|
|
|
|
</h2>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
<select value={sortOrder} onChange={e => setSortOrder(e.target.value)} className={styles.sortSelect}>
|
|
|
|
|
|
<option value="default">Sort: Relevance</option>
|
2026-03-23 22:32:33 +00:00
|
|
|
|
<option value="rwm_desc">Highest R, W & M %</option>
|
|
|
|
|
|
<option value="rwm_asc">Lowest R, W & M %</option>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{isLocationSearch && <option value="distance">Nearest first</option>}
|
|
|
|
|
|
<option value="name_asc">Name A–Z</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{isSearchActive && (
|
|
|
|
|
|
<div className={styles.activeFilters}>
|
|
|
|
|
|
{searchParams.get('search') && <span className={styles.filterChip}>Search: {searchParams.get('search')}<a href="/" className={styles.chipRemove} onClick={e => { e.preventDefault(); }}>×</a></span>}
|
|
|
|
|
|
{searchParams.get('local_authority') && <span className={styles.filterChip}>{searchParams.get('local_authority')}</span>}
|
|
|
|
|
|
{searchParams.get('school_type') && <span className={styles.filterChip}>{searchParams.get('school_type')}</span>}
|
2026-03-23 22:39:50 +00:00
|
|
|
|
{searchParams.get('postcode') && <span className={styles.filterChip}>Near {searchParams.get('postcode')} ({parseFloat(searchParams.get('radius') || '1')} mi)</span>}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-03-05 13:00:34 +00:00
|
|
|
|
{initialSchools.schools.length === 0 && isSearchActive ? (
|
2026-02-02 20:34:35 +00:00
|
|
|
|
<EmptyState
|
2026-03-05 13:00:34 +00:00
|
|
|
|
title="No schools found"
|
|
|
|
|
|
message="Try adjusting your search criteria or filters to find schools."
|
|
|
|
|
|
action={{
|
|
|
|
|
|
label: 'Clear all filters',
|
|
|
|
|
|
onClick: () => {
|
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
|
},
|
|
|
|
|
|
}}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
/>
|
2026-03-05 13:00:34 +00:00
|
|
|
|
) : initialSchools.schools.length > 0 && resultsView === 'map' && isLocationSearch ? (
|
2026-02-04 10:05:31 +00:00
|
|
|
|
/* Map View Layout */
|
|
|
|
|
|
<div className={styles.mapViewContainer}>
|
|
|
|
|
|
<div className={styles.mapContainer}>
|
|
|
|
|
|
<SchoolMap
|
|
|
|
|
|
schools={initialSchools.schools}
|
|
|
|
|
|
center={initialSchools.location_info?.coordinates}
|
2026-03-05 09:33:47 +00:00
|
|
|
|
onMarkerClick={setSelectedMapSchool}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.compactList}>
|
|
|
|
|
|
{initialSchools.schools.map((school) => (
|
2026-03-23 21:31:28 +00:00
|
|
|
|
<div
|
|
|
|
|
|
key={school.urn}
|
2026-03-05 09:33:47 +00:00
|
|
|
|
className={`${styles.listItemWrapper} ${selectedMapSchool?.urn === school.urn ? styles.highlightedItem : ''}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<CompactSchoolItem
|
|
|
|
|
|
school={school}
|
|
|
|
|
|
onAddToCompare={addSchool}
|
|
|
|
|
|
isInCompare={selectedSchools.some(s => s.urn === school.urn)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2026-02-04 10:05:31 +00:00
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
2026-03-05 09:33:47 +00:00
|
|
|
|
|
|
|
|
|
|
{/* Mobile Bottom Sheet for Selected Map Pin */}
|
|
|
|
|
|
{selectedMapSchool && (
|
|
|
|
|
|
<div className={styles.bottomSheetWrapper}>
|
|
|
|
|
|
<div className={styles.bottomSheet}>
|
|
|
|
|
|
<button className={styles.closeSheetBtn} onClick={() => setSelectedMapSchool(null)}>×</button>
|
|
|
|
|
|
<CompactSchoolItem
|
|
|
|
|
|
school={selectedMapSchool}
|
|
|
|
|
|
onAddToCompare={addSchool}
|
|
|
|
|
|
isInCompare={selectedSchools.some(s => s.urn === selectedMapSchool.urn)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
</div>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
) : (
|
2026-02-04 10:05:31 +00:00
|
|
|
|
/* List View Layout */
|
2026-02-02 20:34:35 +00:00
|
|
|
|
<>
|
2026-03-23 22:32:33 +00:00
|
|
|
|
<div className={styles.schoolList}>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{sortedSchools.map((school) => (
|
2026-03-23 22:32:33 +00:00
|
|
|
|
<SchoolRow
|
2026-02-02 20:34:35 +00:00
|
|
|
|
key={school.urn}
|
|
|
|
|
|
school={school}
|
2026-03-23 22:32:33 +00:00
|
|
|
|
isLocationSearch={isLocationSearch}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
onAddToCompare={addSchool}
|
2026-03-23 21:31:28 +00:00
|
|
|
|
onRemoveFromCompare={removeSchool}
|
2026-03-05 09:33:47 +00:00
|
|
|
|
isInCompare={selectedSchools.some(s => s.urn === school.urn)}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
/>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{initialSchools.total_pages > 1 && (
|
|
|
|
|
|
<Pagination
|
|
|
|
|
|
currentPage={initialSchools.page}
|
|
|
|
|
|
totalPages={initialSchools.total_pages}
|
|
|
|
|
|
total={initialSchools.total}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</section>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
/* Compact School Item for Map View */
|
|
|
|
|
|
interface CompactSchoolItemProps {
|
|
|
|
|
|
school: School;
|
|
|
|
|
|
onAddToCompare: (school: School) => void;
|
|
|
|
|
|
isInCompare: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function CompactSchoolItem({ school, onAddToCompare, isInCompare }: CompactSchoolItemProps) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={styles.compactItem}>
|
|
|
|
|
|
<div className={styles.compactItemContent}>
|
|
|
|
|
|
<div className={styles.compactItemHeader}>
|
|
|
|
|
|
<a href={`/school/${school.urn}`} className={styles.compactItemName}>
|
|
|
|
|
|
{school.school_name}
|
|
|
|
|
|
</a>
|
|
|
|
|
|
{school.distance !== undefined && school.distance !== null && (
|
|
|
|
|
|
<span className={styles.distanceBadge}>
|
2026-03-23 22:39:50 +00:00
|
|
|
|
{school.distance.toFixed(1)} mi
|
2026-02-04 10:05:31 +00:00
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.compactItemMeta}>
|
|
|
|
|
|
{school.school_type && <span>{school.school_type}</span>}
|
|
|
|
|
|
{school.local_authority && <span>{school.local_authority}</span>}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.compactItemStats}>
|
|
|
|
|
|
<span className={styles.compactStat}>
|
|
|
|
|
|
<strong>{school.rwm_expected_pct !== null ? `${school.rwm_expected_pct}%` : '-'}</strong> RWM
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className={styles.compactStat}>
|
|
|
|
|
|
<strong>{school.total_pupils || '-'}</strong> pupils
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.compactItemActions}>
|
|
|
|
|
|
<button
|
2026-03-25 20:28:03 +00:00
|
|
|
|
className={isInCompare ? 'btn btn-active btn-sm' : 'btn btn-secondary btn-sm'}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
onClick={() => onAddToCompare(school)}
|
|
|
|
|
|
>
|
2026-03-25 20:28:03 +00:00
|
|
|
|
{isInCompare ? '✓ Comparing' : '+ Compare'}
|
2026-02-04 10:05:31 +00:00
|
|
|
|
</button>
|
2026-03-25 20:28:03 +00:00
|
|
|
|
<a href={`/school/${school.urn}`} className="btn btn-tertiary btn-sm">
|
|
|
|
|
|
View
|
2026-02-04 10:05:31 +00:00
|
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|