2026-09-21 22:41:40 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The per-card basket toggle.
|
|
|
|
|
*
|
|
|
|
|
* The links around it are server-rendered, so the section works with JS off;
|
|
|
|
|
* this adds the basket interaction on top rather than being what makes the
|
|
|
|
|
* section function.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useComparisonContext } from '@/context/ComparisonContext';
|
2026-09-22 11:59:27 +01:00
|
|
|
import type { School, NearbySchool } from '@/lib/types';
|
|
|
|
|
import styles from './NearbySchools.module.css';
|
2026-09-21 22:41:40 +01:00
|
|
|
|
2026-09-22 11:59:27 +01:00
|
|
|
export function AddToCompareButton({ school }: { school: NearbySchool }) {
|
2026-09-21 22:41:40 +01:00
|
|
|
const { addSchool, removeSchool, selectedSchools } = useComparisonContext();
|
|
|
|
|
const selected = selectedSchools.some((s) => s.urn === school.urn);
|
|
|
|
|
|
|
|
|
|
const toggle = () => {
|
|
|
|
|
if (selected) {
|
|
|
|
|
removeSchool(school.urn);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// The basket only needs identity and display fields; the compare page
|
|
|
|
|
// fetches everything it renders by URN.
|
|
|
|
|
addSchool({
|
|
|
|
|
urn: school.urn,
|
|
|
|
|
school_name: school.school_name,
|
|
|
|
|
school_type: school.school_type,
|
|
|
|
|
age_range: school.age_range,
|
|
|
|
|
} as School);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className={styles.add}
|
|
|
|
|
onClick={toggle}
|
|
|
|
|
aria-pressed={selected}
|
|
|
|
|
>
|
|
|
|
|
{selected ? '✓ Added to compare' : '+ Add to compare'}
|
|
|
|
|
<span className={styles.srOnly}> — {school.school_name}</span>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|