M2-T09: build data visualization UI (heatmap map as home page)

- self-contained RecordsMap (only module importing leaflet/react-leaflet/
  leaflet.heat/leaflet.markercluster); OSM tiles, swappable behind clean props
- heatmap layers for location + poo (primary); time-range selector fetches
  only the window (locations server-filtered; poo client-filtered)
- toggleable scatter layer with marker clustering; point-select reuses T10's
  edit/delete modals + hooks; query-key prefixes refresh map on mutation
- pure map logic isolated + unit-tested; leaflet mocked in component tests
- responsive layout; typed client only
This commit is contained in:
2026-06-13 15:20:50 +02:00
parent 0d988a9b28
commit 32d93bba2a
10 changed files with 1536 additions and 10 deletions
+274
View File
@@ -0,0 +1,274 @@
/**
* HomePage tests — M2-T09.
*
* Leaflet is mocked so jsdom doesn't choke on DOM APIs it doesn't support.
* We verify:
* 1. Controls render (time range inputs, layer toggles, apply button).
* 2. Point-select: when onSelectLocation is called, EditLocationModal opens.
* 3. Point-select: when onSelectPoo is called, EditPooModal opens.
* 4. The map component is rendered (mocked).
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { MantineProvider } from '@mantine/core'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { MemoryRouter } from 'react-router-dom'
import type { ReactNode } from 'react'
// ---------------------------------------------------------------------------
// Mock leaflet / react-leaflet before any component imports them.
// ---------------------------------------------------------------------------
vi.mock('leaflet', () => ({
default: {},
Icon: { Default: { prototype: {}, mergeOptions: vi.fn() } },
DivIcon: vi.fn(() => ({})),
heatLayer: vi.fn(() => ({ setLatLngs: vi.fn(), addTo: vi.fn() })),
markerClusterGroup: vi.fn(() => ({ addLayer: vi.fn(), clearLayers: vi.fn() })),
marker: vi.fn(() => ({
bindTooltip: vi.fn().mockReturnThis(),
on: vi.fn().mockReturnThis(),
})),
tileLayer: vi.fn(),
map: vi.fn(),
}))
vi.mock('leaflet.heat', () => ({}))
vi.mock('leaflet.markercluster', () => ({}))
vi.mock('react-leaflet', () => ({
MapContainer: ({ children }: { children: ReactNode }) => (
<div data-testid="records-map">{children}</div>
),
TileLayer: () => null,
useMap: () => ({
addLayer: vi.fn(),
removeLayer: vi.fn(),
hasLayer: vi.fn(() => false),
}),
}))
// Mock leaflet image imports
vi.mock('leaflet/dist/images/marker-icon-2x.png', () => ({ default: 'marker-icon-2x.png' }))
vi.mock('leaflet/dist/images/marker-icon.png', () => ({ default: 'marker-icon.png' }))
vi.mock('leaflet/dist/images/marker-shadow.png', () => ({ default: 'marker-shadow.png' }))
// Mock leaflet CSS
vi.mock('leaflet/dist/leaflet.css', () => ({}))
vi.mock('leaflet.markercluster/dist/MarkerCluster.css', () => ({}))
vi.mock('leaflet.markercluster/dist/MarkerCluster.Default.css', () => ({}))
// ---------------------------------------------------------------------------
// Mock RecordsMap to capture onSelectLocation / onSelectPoo callbacks
// ---------------------------------------------------------------------------
import type { RecordsMapProps } from '../map/RecordsMap'
let capturedOnSelectLocation: RecordsMapProps['onSelectLocation'] | undefined
let capturedOnSelectPoo: RecordsMapProps['onSelectPoo'] | undefined
vi.mock('../map/RecordsMap', () => ({
RecordsMap: (props: RecordsMapProps) => {
capturedOnSelectLocation = props.onSelectLocation
capturedOnSelectPoo = props.onSelectPoo
return <div data-testid="records-map-mock" />
},
}))
// ---------------------------------------------------------------------------
// Mock apiClient — return minimal data so queries resolve
// ---------------------------------------------------------------------------
vi.mock('../api/client', () => ({
default: {
GET: vi.fn(async (path: string) => {
if (path === '/api/locations') {
return {
data: {
items: [
{
person: 'alice',
datetime: '2026-01-15T10:00:00Z',
latitude: 39.9,
longitude: 116.4,
altitude: null,
},
],
limit: 5000,
offset: 0,
},
}
}
if (path === '/api/poo') {
return {
data: {
items: [
{
timestamp: '2026-01-20T09:00:00Z',
status: 'done',
latitude: 39.91,
longitude: 116.41,
},
],
limit: 1000,
offset: 0,
},
}
}
return { data: null }
}),
},
}))
// ---------------------------------------------------------------------------
// Now import components under test (after mocks are registered)
// ---------------------------------------------------------------------------
import { HomePage } from './HomePage'
// ---------------------------------------------------------------------------
// Test wrapper
// ---------------------------------------------------------------------------
function makeQC() {
return new QueryClient({ defaultOptions: { queries: { retry: false } } })
}
function Wrapper({ qc, children }: { qc: QueryClient; children: ReactNode }) {
return (
<MantineProvider>
<QueryClientProvider client={qc}>
<MemoryRouter>{children}</MemoryRouter>
</QueryClientProvider>
</MantineProvider>
)
}
// Helper: render HomePage and wait for queries to resolve
async function renderHomePage() {
const qc = makeQC()
const utils = render(
<Wrapper qc={qc}>
<HomePage />
</Wrapper>,
)
// Wait for the map mock to appear (data loaded)
await waitFor(() => screen.getByTestId('records-map-mock'))
return utils
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe('HomePage', () => {
beforeEach(() => {
capturedOnSelectLocation = undefined
capturedOnSelectPoo = undefined
})
it('renders time-range controls', async () => {
await renderHomePage()
expect(screen.getByTestId('time-start-input')).toBeTruthy()
expect(screen.getByTestId('time-end-input')).toBeTruthy()
expect(screen.getByTestId('apply-window-button')).toBeTruthy()
})
it('renders layer toggle switches', async () => {
await renderHomePage()
expect(screen.getByTestId('toggle-location-heat')).toBeTruthy()
expect(screen.getByTestId('toggle-poo-heat')).toBeTruthy()
expect(screen.getByTestId('toggle-scatter')).toBeTruthy()
})
it('renders the RecordsMap component', async () => {
await renderHomePage()
expect(screen.getByTestId('records-map-mock')).toBeTruthy()
})
it('opens EditLocationModal when onSelectLocation is called with a location record', async () => {
await renderHomePage()
// Simulate clicking a location scatter point
const record = {
person: 'alice',
datetime: '2026-01-15T10:00:00Z',
latitude: 39.9,
longitude: 116.4,
altitude: null,
}
expect(capturedOnSelectLocation).toBeDefined()
capturedOnSelectLocation!(record)
// EditLocationModal should appear
await waitFor(() => screen.getByTestId('edit-location-modal'))
expect(screen.getByTestId('edit-location-modal')).toBeTruthy()
})
it('opens EditPooModal when onSelectPoo is called with a poo record', async () => {
await renderHomePage()
const record = {
timestamp: '2026-01-20T09:00:00Z',
status: 'done',
latitude: 39.91,
longitude: 116.41,
}
expect(capturedOnSelectPoo).toBeDefined()
capturedOnSelectPoo!(record)
await waitFor(() => screen.getByTestId('edit-poo-modal'))
expect(screen.getByTestId('edit-poo-modal')).toBeTruthy()
})
it('closes EditLocationModal when Cancel is clicked', async () => {
await renderHomePage()
const record = {
person: 'alice',
datetime: '2026-01-15T10:00:00Z',
latitude: 39.9,
longitude: 116.4,
altitude: null,
}
capturedOnSelectLocation!(record)
await waitFor(() => screen.getByTestId('edit-location-modal'))
fireEvent.click(screen.getByTestId('edit-location-cancel'))
await waitFor(() => expect(screen.queryByTestId('edit-location-modal')).toBeNull())
})
it('closes EditPooModal when Cancel is clicked', async () => {
await renderHomePage()
const record = {
timestamp: '2026-01-20T09:00:00Z',
status: 'done',
latitude: 39.91,
longitude: 116.41,
}
capturedOnSelectPoo!(record)
await waitFor(() => screen.getByTestId('edit-poo-modal'))
fireEvent.click(screen.getByTestId('edit-poo-cancel'))
await waitFor(() => expect(screen.queryByTestId('edit-poo-modal')).toBeNull())
})
it('time-range inputs have default values', async () => {
await renderHomePage()
const startInput = screen.getByTestId('time-start-input') as HTMLInputElement
const endInput = screen.getByTestId('time-end-input') as HTMLInputElement
expect(startInput.value).toBeTruthy()
expect(endInput.value).toBeTruthy()
})
it('Apply button re-triggers data fetch with new window', async () => {
await renderHomePage()
const startInput = screen.getByTestId('time-start-input') as HTMLInputElement
fireEvent.change(startInput, { target: { value: '2026-01-01T00:00' } })
fireEvent.click(screen.getByTestId('apply-window-button'))
// Just verify no crash; data refresh happens async via React Query.
await waitFor(() => screen.getByTestId('records-map-mock'))
})
})
+314 -10
View File
@@ -1,19 +1,323 @@
/**
* HomePage — placeholder for M2-T09.
* HomePage — data-visualization map view (M2-T09).
*
* T09 replaces this with the real home view: Leaflet map, heatmap layer,
* time-range selector, scatter-point layer, and poo overlay.
* Renders a heat map of location records (where you've been) and poo records
* (where the dog poops), plus a toggleable scatter layer for point-select
* edit/delete (reusing T10's modals + hooks).
*
* Data fetching and all state live here; the map itself is fully isolated in
* src/map/RecordsMap.tsx (the ONLY place that imports leaflet).
*/
import { Container, Title, Text } from '@mantine/core'
import { useState, useMemo } from 'react'
import { useQuery } from '@tanstack/react-query'
import {
Stack,
Group,
Switch,
TextInput,
Button,
Paper,
Text,
Box,
Loader,
Alert,
Badge,
} from '@mantine/core'
import apiClient from '../api/client'
import {
locationsToHeatPoints,
pooToHeatPoints,
locationsToMapPoints,
pooToMapPoints,
filterPooByTimeWindow,
daysAgoISO,
nowISO,
} from '../map'
import { RecordsMap } from '../map'
import {
EditLocationModal,
EditPooModal,
ConfirmDeleteModal,
useDeleteLocation,
useDeletePoo,
} from '../records'
import type { LocationRecord, PooRecord } from '../records'
// ---------------------------------------------------------------------------
// Data hooks (query-key prefix: ['locations', ...] / ['poo', ...])
// ---------------------------------------------------------------------------
function useLocations(start: string | null, end: string | null) {
return useQuery({
queryKey: ['locations', { start, end, limit: 5000 }],
queryFn: async () => {
const res = await apiClient.GET('/api/locations', {
params: {
query: {
limit: 5000,
offset: 0,
...(start ? { start } : {}),
...(end ? { end } : {}),
},
},
})
return res.data?.items ?? []
},
})
}
/**
* Poo endpoint has no server-side time filter — fetch a large page (max 1000)
* and client-filter by timestamp below.
*/
function usePoo() {
return useQuery({
queryKey: ['poo', { limit: 1000 }],
queryFn: async () => {
const res = await apiClient.GET('/api/poo', {
params: { query: { limit: 1000, offset: 0 } },
})
return res.data?.items ?? []
},
})
}
// ---------------------------------------------------------------------------
// Point-select state (which record is selected + which modal to show)
// ---------------------------------------------------------------------------
type SelectionState =
| { kind: 'none' }
| { kind: 'editLocation'; record: LocationRecord }
| { kind: 'deleteLocation'; record: LocationRecord }
| { kind: 'editPoo'; record: PooRecord }
| { kind: 'deletePoo'; record: PooRecord }
// ---------------------------------------------------------------------------
// HomePage
// ---------------------------------------------------------------------------
export function HomePage() {
// ------ Time-window state -----------------------------------------------
// Default: last 30 days → now
const [startInput, setStartInput] = useState(() => {
const d = new Date()
d.setUTCDate(d.getUTCDate() - 30)
return d.toISOString().slice(0, 16) // "YYYY-MM-DDTHH:MM"
})
const [endInput, setEndInput] = useState(() => nowISO().slice(0, 16))
// Applied (committed) window — updated on button click
const [appliedStart, setAppliedStart] = useState<string | null>(() => daysAgoISO(30))
const [appliedEnd, setAppliedEnd] = useState<string | null>(() => nowISO())
// ------ Layer toggle state -----------------------------------------------
const [showLocationHeat, setShowLocationHeat] = useState(true)
const [showPooHeat, setShowPooHeat] = useState(true)
const [showScatter, setShowScatter] = useState(false)
// ------ Data fetching ----------------------------------------------------
const locationsQuery = useLocations(appliedStart, appliedEnd)
const pooQuery = usePoo()
// Client-side time-filter for poo (server has no filter)
const filteredPoo = useMemo(
() => filterPooByTimeWindow(pooQuery.data ?? [], appliedStart, appliedEnd),
[pooQuery.data, appliedStart, appliedEnd],
)
// Derived map data
const locationHeatPoints = useMemo(
() => locationsToHeatPoints(locationsQuery.data ?? []),
[locationsQuery.data],
)
const pooHeatPoints = useMemo(
() => pooToHeatPoints(filteredPoo),
[filteredPoo],
)
const locationScatterPoints = useMemo(
() => locationsToMapPoints(locationsQuery.data ?? []),
[locationsQuery.data],
)
const pooScatterPoints = useMemo(
() => pooToMapPoints(filteredPoo),
[filteredPoo],
)
// ------ Point-select state -----------------------------------------------
const [selection, setSelection] = useState<SelectionState>({ kind: 'none' })
const deleteLocationMut = useDeleteLocation()
const deletePooMut = useDeletePoo()
// Handlers
function handleSelectLocation(record: LocationRecord) {
setSelection({ kind: 'editLocation', record })
}
function handleSelectPoo(record: PooRecord) {
setSelection({ kind: 'editPoo', record })
}
function applyWindow() {
// Convert local datetime-local inputs (which have no TZ) to ISO8601
// by appending :00Z if needed. Input is "YYYY-MM-DDTHH:MM".
const toISO = (s: string) => (s ? s + ':00Z' : null)
setAppliedStart(toISO(startInput))
setAppliedEnd(toISO(endInput))
}
// ------ Render -----------------------------------------------------------
const isLoading = locationsQuery.isLoading || pooQuery.isLoading
const isError = locationsQuery.isError || pooQuery.isError
return (
<Container pt="xl">
<Title order={2}>Home</Title>
<Text c="dimmed" mt="sm">
Map / heatmap visualisation implemented in M2-T09.
</Text>
</Container>
<Box style={{ height: 'calc(100vh - 52px)', display: 'flex', flexDirection: 'column' }}>
{/* Controls bar */}
<Paper
shadow="xs"
p="xs"
style={{ zIndex: 1000, flexShrink: 0 }}
data-testid="map-controls"
>
<Stack gap="xs">
{/* Time-range row */}
<Group gap="xs" align="flex-end" wrap="wrap">
<TextInput
label="From"
type="datetime-local"
value={startInput}
onChange={(e) => setStartInput(e.currentTarget.value)}
size="xs"
style={{ minWidth: 180 }}
data-testid="time-start-input"
/>
<TextInput
label="To"
type="datetime-local"
value={endInput}
onChange={(e) => setEndInput(e.currentTarget.value)}
size="xs"
style={{ minWidth: 180 }}
data-testid="time-end-input"
/>
<Button size="xs" onClick={applyWindow} data-testid="apply-window-button">
Apply
</Button>
{isLoading && <Loader size="xs" />}
</Group>
{/* Layer toggles row */}
<Group gap="md" wrap="wrap">
<Switch
label={
<Group gap={4}>
<Text size="xs">Location heat</Text>
<Badge size="xs" color="blue" variant="light">
{locationsQuery.data?.length ?? 0}
</Badge>
</Group>
}
checked={showLocationHeat}
onChange={(e) => setShowLocationHeat(e.currentTarget.checked)}
size="xs"
data-testid="toggle-location-heat"
/>
<Switch
label={
<Group gap={4}>
<Text size="xs">Poo heat</Text>
<Badge size="xs" color="orange" variant="light">
{filteredPoo.length}
</Badge>
</Group>
}
checked={showPooHeat}
onChange={(e) => setShowPooHeat(e.currentTarget.checked)}
size="xs"
data-testid="toggle-poo-heat"
/>
<Switch
label={<Text size="xs">Scatter (click to edit)</Text>}
checked={showScatter}
onChange={(e) => setShowScatter(e.currentTarget.checked)}
size="xs"
data-testid="toggle-scatter"
/>
</Group>
{/* Error banner */}
{isError && (
<Alert color="red" data-testid="map-error-alert">
Failed to load data. Check connection and refresh.
</Alert>
)}
</Stack>
</Paper>
{/* Map fills remaining height */}
<Box style={{ flex: 1, minHeight: 0 }}>
<RecordsMap
locationHeatPoints={locationHeatPoints}
pooHeatPoints={pooHeatPoints}
locationScatterPoints={locationScatterPoints}
pooScatterPoints={pooScatterPoints}
showLocationHeat={showLocationHeat}
showPooHeat={showPooHeat}
showScatter={showScatter}
onSelectLocation={handleSelectLocation}
onSelectPoo={handleSelectPoo}
height="100%"
/>
</Box>
{/* ---------- Point-select modals ---------- */}
{selection.kind === 'editLocation' && (
<EditLocationModal
record={selection.record}
onClose={() => setSelection({ kind: 'none' })}
onSaved={() => setSelection({ kind: 'none' })}
/>
)}
{selection.kind === 'deleteLocation' && (
<ConfirmDeleteModal
message={`Delete location record for ${selection.record.person} at ${selection.record.datetime}?`}
loading={deleteLocationMut.isPending}
onConfirm={async () => {
await deleteLocationMut.mutateAsync({
person: selection.record.person,
datetime: selection.record.datetime,
})
setSelection({ kind: 'none' })
}}
onCancel={() => setSelection({ kind: 'none' })}
/>
)}
{selection.kind === 'editPoo' && (
<EditPooModal
record={selection.record}
onClose={() => setSelection({ kind: 'none' })}
onSaved={() => {
// After saving, optionally switch to delete prompt or just close.
setSelection({ kind: 'none' })
}}
/>
)}
{selection.kind === 'deletePoo' && (
<ConfirmDeleteModal
message={`Delete poo record at ${selection.record.timestamp}?`}
loading={deletePooMut.isPending}
onConfirm={async () => {
await deletePooMut.mutateAsync(selection.record.timestamp)
setSelection({ kind: 'none' })
}}
onCancel={() => setSelection({ kind: 'none' })}
/>
)}
</Box>
)
}