feat: add location-based search, redesign station cards, and implement URL state management
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

- Support geolocation search (lat/lng) as alternative to postcode with automatic fallback
- Redesign StationCard with expanded layout showing address, distance in miles, reliability status, directions link, and optional remove button
- Add directions integration with Google Maps including origin parameter support
- Persist search parameters (postcode/coords, fuel type, radius, sort) in URL query and hydrate on mount
- Implement compact map markers with inline directions link and click-to-zoom behavior
- Auto-trigger search when filters change (fuel type, radius, sort) if search already performed
- Add removable prop to StationCard for watchlist integration
- Display reliability status (Current/Stale/Outdated) with color-coded pricing
- Remove 2-mile radius option from search filters
This commit is contained in:
Ovidiu U
2026-04-20 15:51:02 +01:00
parent d29f3e6487
commit 7dc41ba9ee
5 changed files with 236 additions and 86 deletions

View File

@@ -40,7 +40,6 @@
import {ref, watch, onMounted, onUnmounted, nextTick} from 'vue' import {ref, watch, onMounted, onUnmounted, nextTick} from 'vue'
import L from 'leaflet' import L from 'leaflet'
import 'leaflet/dist/leaflet.css' import 'leaflet/dist/leaflet.css'
import {zoom} from "leaflet/src/control/Control.Zoom.js";
const CLASSIFICATION_COLOURS = { const CLASSIFICATION_COLOURS = {
current: '#22c55e', current: '#22c55e',
@@ -56,37 +55,28 @@ const CLASSIFICATION_BORDER_COLOURS = {
outdated: '#dc2626', outdated: '#dc2626',
} }
function buildMarkerHtml(station, index, colour, borderColour) { function buildDirectionsUrl(station, origin) {
const base = `https://www.google.com/maps/dir/?api=1&destination=${station.lat},${station.lng}`
if (origin?.lat != null && origin?.lng != null) {
return `${base}&origin=${origin.lat},${origin.lng}`
}
return base
}
function buildMarkerHtml(station, index, colour, borderColour, origin) {
const isFirst = index === 0 const isFirst = index === 0
const w = isFirst ? 63 : 59 const h = isFirst ? 20 : 18
const h = isFirst ? 58 : 51 const fontSize = isFirst ? 11 : 10
const bw = isFirst ? 56 : 52 const iconSize = isFirst ? 11 : 10
const bh = isFirst ? 43 : 38 const star = isFirst
const br = isFirst ? 17 : 15 ? `<span style="margin-right:2px;color:#facc15;font-size:10px;line-height:1;">★</span>`
const tailTop = isFirst ? 45 : 40 : ''
const tailW = isFirst ? 9 : 7
const tailH = isFirst ? 11 : 9
const badgeSize = isFirst ? 18 : 16
const badgeFontSize = isFirst ? 10 : 8
const priceFontSize = isFirst ? 12 : 11
const initial = escHtml((station.brand || station.name || '?')[0].toUpperCase()) const directionsUrl = escHtml(buildDirectionsUrl(station, origin))
const navSvg = `<svg xmlns="http://www.w3.org/2000/svg" width="${iconSize}" height="${iconSize}" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><polygon points="3 11 22 2 13 21 11 13 3 11"/></svg>`
const badge = isFirst return `<div style="display:inline-flex;align-items:center;height:${h}px;padding:0 4px 0 6px;background:${colour};color:#fff;font-weight:700;font-size:${fontSize}px;line-height:1;letter-spacing:-0.2px;border-radius:10px;border:1.5px solid ${borderColour};box-shadow:0 1px 3px rgba(0,0,0,0.25);white-space:nowrap;gap:3px;">
? `<div style="position:absolute;top:-4px;right:-4px;width:${badgeSize}px;height:${badgeSize}px;background:#facc15;border-radius:50%;display:flex;align-items:center;justify-content:center;border:2px solid white;box-shadow:0 2px 4px rgba(0,0,0,0.2);z-index:30;"><span style="font-size:${badgeFontSize}px;color:#713f12;">★</span></div>` ${star}<span>${Number(station.price).toFixed(1)}</span><a data-directions href="${directionsUrl}" target="_blank" rel="noopener" aria-label="Directions" style="display:inline-flex;align-items:center;justify-content:center;width:${h - 6}px;height:${h - 6}px;margin-left:1px;border-radius:50%;background:rgba(255,255,255,0.22);color:#fff;text-decoration:none;">${navSvg}</a>
: `<div style="position:absolute;top:-3px;right:-3px;width:${badgeSize}px;height:${badgeSize}px;background:#374151;border-radius:50%;display:flex;align-items:center;justify-content:center;border:1px solid white;box-shadow:0 1px 3px rgba(0,0,0,0.2);z-index:30;"><span style="font-size:${badgeFontSize}px;font-weight:bold;color:white;">${index + 1}</span></div>`
return `<div style="position:relative;width:${w}px;height:${h}px;">
${badge}
<div style="position:absolute;top:6px;left:50%;transform:translateX(-50%);width:${bw}px;height:${bh}px;background:${colour};border-radius:${br}px;border:3px solid ${borderColour};box-shadow:0 2px 6px rgba(0,0,0,0.24),inset 0 1px 0 rgba(255,255,255,0.22);overflow:hidden;z-index:10;">
<div style="position:absolute;top:0;left:0;right:0;height:11px;background:rgba(15,23,42,0.20);border-bottom:1px solid rgba(255,255,255,0.24);display:flex;align-items:center;justify-content:center;">
<span style="font-size:9px;font-weight:700;letter-spacing:-0.05px;color:rgba(255,255,255,0.84);text-transform:uppercase;line-height:1;">${initial}</span>
</div>
<div style="position:absolute;left:3px;right:3px;top:12px;bottom:2px;display:flex;align-items:center;justify-content:center;text-align:center;">
<span style="display:inline-block;max-width:${bw - 13}px;color:#ffffff;font-size:${priceFontSize}px;font-weight:800;letter-spacing:-0.1px;line-height:1.12;white-space:nowrap;text-shadow:0 1px 1px rgba(0,0,0,0.42);">${Number(station.price).toFixed(1)}p</span>
</div>
</div>
<div style="position:absolute;top:${tailTop}px;left:50%;transform:translateX(-50%);width:0;height:0;border-left:${tailW}px solid transparent;border-right:${tailW}px solid transparent;border-top:${tailH}px solid ${colour};z-index:5;"></div>
</div>` </div>`
} }
@@ -102,6 +92,7 @@ const props = defineProps({
stations: {type: Array, required: true}, stations: {type: Array, required: true},
defaultOpen: {type: Boolean, default: false}, defaultOpen: {type: Boolean, default: false},
radiusMiles: {type: Number, default: 10}, radiusMiles: {type: Number, default: 10},
origin: {type: Object, default: null},
}) })
const mapContainer = ref(null) const mapContainer = ref(null)
@@ -216,22 +207,29 @@ function renderMarkers() {
` `
const isFirst = index === 0 const isFirst = index === 0
const w = isFirst ? 63 : 59 const w = isFirst ? 65 : 56
const h = isFirst ? 58 : 51 const h = isFirst ? 20 : 18
const icon = L.divIcon({ const icon = L.divIcon({
className: '', className: '',
iconSize: [w, h], iconSize: [w, h],
iconAnchor: [w / 2, h], iconAnchor: [w / 2, h / 2],
html: buildMarkerHtml(station, index, colour, borderColour), html: buildMarkerHtml(station, index, colour, borderColour, props.origin),
}) })
const marker = L.marker([station.lat, station.lng], {icon}).bindPopup(popup) const marker = L.marker([station.lat, station.lng], {icon}).bindPopup(popup)
marker.on('click', () => {
const target = Math.max(mapInstance.getZoom(), 16)
mapInstance.setView([station.lat, station.lng], target, {animate: true})
})
markersLayer.addLayer(marker) markersLayer.addLayer(marker)
bounds.push([station.lat, station.lng]) bounds.push([station.lat, station.lng])
}) })
const zoom = getZoomForRadius(props.radiusMiles)
if (bounds.length === 1) { if (bounds.length === 1) {
mapInstance.setView(bounds[0], zoom) mapInstance.setView(bounds[0], zoom)
} else { } else {

View File

@@ -20,14 +20,14 @@
id="postcode-input" id="postcode-input"
v-model="postcode" v-model="postcode"
type="text" type="text"
placeholder="Enter postcode, e.g. SW1A 1AA" :placeholder="coords ? 'Using your current location' : 'Enter postcode, e.g. SW1A 1AA'"
class="w-full h-14 pr-28 pl-4 bg-white border border-zinc-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-accent shadow-inner text-base" class="w-full h-14 pr-28 pl-4 bg-white border border-zinc-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-accent shadow-inner text-base"
@keyup.enter="onSearch" @keyup.enter="onSearch"
/> />
</div> </div>
<button <button
@click="onSearch" @click="onSearch"
:disabled="!postcode.trim()" :disabled="!postcode.trim() && !coords"
class="h-14 px-8 bg-primary text-white rounded-xl font-bold text-base shadow-xl hover:bg-primary-dark transition-all disabled:cursor-not-allowed" class="h-14 px-8 bg-primary text-white rounded-xl font-bold text-base shadow-xl hover:bg-primary-dark transition-all disabled:cursor-not-allowed"
> >
Find Prices Find Prices
@@ -50,7 +50,8 @@
aria-label="Search radius" aria-label="Search radius"
class="min-w-0 h-10 px-2 bg-white border border-zinc-300 rounded-xl text-sm font-medium text-zinc-700 focus:outline-none focus:ring-2 focus:ring-accent" class="min-w-0 h-10 px-2 bg-white border border-zinc-300 rounded-xl text-sm font-medium text-zinc-700 focus:outline-none focus:ring-2 focus:ring-accent"
> >
<option :value="2">2 miles</option> <!-- <option :value="2">2 miles</option> -->
<option :value="5">5 miles</option> <option :value="5">5 miles</option>
<option :value="10">10 miles</option> <option :value="10">10 miles</option>
<option :value="20">20 miles</option> <option :value="20">20 miles</option>
@@ -71,32 +72,51 @@
</template> </template>
<script setup> <script setup>
import { ref } from 'vue' import { ref, watch, nextTick } from 'vue'
import { FUEL_TYPES } from '../constants/fuelTypes.js' import { FUEL_TYPES } from '../constants/fuelTypes.js'
const props = defineProps({
initial: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['search']) const emit = defineEmits(['search'])
const postcode = ref('') const postcode = ref('')
const coords = ref(null)
const fuelType = ref('e10') const fuelType = ref('e10')
const radius = ref(10) const radius = ref(10)
const sort = ref('reliable') const sort = ref('reliable')
const locating = ref(false) const locating = ref(false)
let hydrating = false
watch(() => props.initial, (v) => {
if (!v) return
hydrating = true
if (typeof v.postcode === 'string') postcode.value = v.postcode
if (v.lat != null && v.lng != null) coords.value = { lat: v.lat, lng: v.lng }
if (v.fuelType) fuelType.value = v.fuelType
if (v.radius != null) radius.value = Number(v.radius)
if (v.sort) sort.value = v.sort
nextTick(() => { hydrating = false })
}, { immediate: true, deep: true })
watch(postcode, () => { coords.value = null })
watch([fuelType, radius, sort], () => {
if (hydrating) return
if (postcode.value.trim() || coords.value) onSearch()
})
function useMyLocation() { function useMyLocation() {
if (!navigator.geolocation) return if (!navigator.geolocation) return
locating.value = true locating.value = true
navigator.geolocation.getCurrentPosition( navigator.geolocation.getCurrentPosition(
async ({ coords }) => { ({ coords: c }) => {
try { coords.value = { lat: c.latitude, lng: c.longitude }
const res = await fetch(`https://api.postcodes.io/postcodes?lon=${coords.longitude}&lat=${coords.latitude}&limit=1`) postcode.value = ''
const json = await res.json() locating.value = false
if (json.result?.[0]?.postcode) { onSearch()
postcode.value = json.result[0].postcode
onSearch()
}
} finally {
locating.value = false
}
}, },
() => { locating.value = false }, () => { locating.value = false },
{ timeout: 8000, enableHighAccuracy: false, maximumAge: 30000 }, { timeout: 8000, enableHighAccuracy: false, maximumAge: 30000 },
@@ -104,9 +124,14 @@ function useMyLocation() {
} }
function onSearch() { function onSearch() {
if (!postcode.value.trim()) return const hasPostcode = postcode.value.trim().length > 0
const hasCoords = coords.value !== null
if (!hasPostcode && !hasCoords) return
emit('search', { emit('search', {
postcode: postcode.value.trim(), postcode: hasPostcode ? postcode.value.trim() : null,
lat: hasCoords ? coords.value.lat : null,
lng: hasCoords ? coords.value.lng : null,
fuelType: fuelType.value, fuelType: fuelType.value,
radius: radius.value, radius: radius.value,
sort: sort.value, sort: sort.value,

View File

@@ -1,20 +1,60 @@
<template> <template>
<div class="flex items-center justify-between p-4 bg-white rounded-xl border border-zinc-300 hover:border-accent transition-colors"> <div class="bg-zinc-50 p-4 rounded-xl border border-zinc-300 shadow-sm flex flex-col gap-4">
<div class="flex items-center gap-3 min-w-0"> <div class="flex justify-between items-start gap-3">
<div class="w-10 h-10 rounded-lg bg-accent/10 flex items-center justify-center flex-shrink-0"> <div class="space-y-0.5 min-w-0 flex-1">
<iconify-icon <h4 class="font-bold text-lg text-zinc-800 truncate">{{ station.name }}</h4>
:icon="station.is_supermarket ? 'lucide:shopping-cart' : 'lucide:fuel'" <p class="text-xs text-zinc-500 flex items-center gap-1">
style="font-size:1.25rem" <iconify-icon class="text-xs" icon="lucide:map-pin"></iconify-icon>
class="text-accent" <span class="truncate">{{ locationLine }}</span>
></iconify-icon> </p>
</div> </div>
<div class="min-w-0"> <a
<p class="font-bold text-zinc-800 truncate">{{ station.name }}</p> :href="directionsUrl"
<p class="text-xs text-zinc-500">{{ station.distance_km.toFixed(1) }} km away · Updated {{ updatedAgo }}</p> aria-label="Directions"
class="hidden md:inline-flex w-10 h-10 items-center justify-center rounded-lg bg-accent/10 text-accent active:bg-accent/20 flex-shrink-0"
rel="noopener"
target="_blank"
>
<iconify-icon class="text-lg" icon="lucide:navigation"></iconify-icon>
</a>
<div class="text-right flex-shrink-0">
<div :class="priceColor" class="text-xl font-black">
{{ station.price }}<span class="text-sm font-bold uppercase ml-0.5">p</span>
</div>
<p :class="priceColor" class="text-[10px] font-bold uppercase tracking-wider">
{{ statusLabel }}
</p>
</div> </div>
</div> </div>
<div class="text-right flex-shrink-0 ml-4"> <div class="flex items-center justify-end gap-2 md:hidden">
<p class="text-xl font-black" :class="priceColor">{{ station.price }}p</p> <button
v-if="removable"
aria-label="Remove"
class="w-10 h-10 flex items-center justify-center rounded-lg bg-zinc-200 text-zinc-500 active:bg-zinc-300"
type="button"
@click="emit('remove', station)"
>
<iconify-icon class="text-lg" icon="lucide:trash-2"></iconify-icon>
</button>
<a
:href="directionsUrl"
class="flex-1 h-10 flex items-center justify-center gap-2 rounded-lg bg-accent/10 text-accent font-bold text-sm active:bg-accent/20"
rel="noopener"
target="_blank"
>
<iconify-icon class="text-lg" icon="lucide:navigation"></iconify-icon>
Directions
</a>
</div>
<div v-if="removable" class="hidden md:flex justify-end">
<button
aria-label="Remove"
class="w-10 h-10 flex items-center justify-center rounded-lg bg-zinc-200 text-zinc-500 active:bg-zinc-300"
type="button"
@click="emit('remove', station)"
>
<iconify-icon class="text-lg" icon="lucide:trash-2"></iconify-icon>
</button>
</div> </div>
</div> </div>
</template> </template>
@@ -25,21 +65,42 @@ import { computed } from 'vue'
const props = defineProps({ const props = defineProps({
station: { type: Object, required: true }, station: { type: Object, required: true },
lowestPrice: { type: Number, default: null }, lowestPrice: { type: Number, default: null },
removable: { type: Boolean, default: false },
origin: { type: Object, default: null },
}) })
const emit = defineEmits(['remove'])
const RELIABILITY_MAP = {
reliable: { label: 'Current', color: 'text-status-good' },
stale: { label: 'Stale', color: 'text-status-warn' },
outdated: { label: 'Outdated', color: 'text-status-bad' },
}
const reliabilityInfo = computed(() => RELIABILITY_MAP[props.station.reliability] ?? RELIABILITY_MAP.reliable)
const priceColor = computed(() => { const priceColor = computed(() => {
if (!props.lowestPrice) return 'text-zinc-800' if (props.lowestPrice && props.station.price_pence === props.lowestPrice) {
if (props.station.price_pence === props.lowestPrice) return 'text-status-good' return 'text-status-good'
if (props.station.price_pence > props.lowestPrice + 500) return 'text-status-bad' }
return 'text-zinc-800' return reliabilityInfo.value.color
}) })
const updatedAgo = computed(() => { const statusLabel = computed(() => reliabilityInfo.value.label)
const updated = new Date(props.station.price_updated_at)
const diff = Math.floor((Date.now() - updated) / 60000) const distanceMiles = computed(() => (props.station.distance_km * 0.621371).toFixed(1))
if (diff < 60) return `${diff}m ago`
const hours = Math.floor(diff / 60) const locationLine = computed(() => {
if (hours < 24) return `${hours}h ago` const parts = [props.station.address, `${distanceMiles.value} mi`].filter(Boolean)
return `${Math.floor(hours / 24)}d ago` return parts.join(' • ')
})
const directionsUrl = computed(() => {
const { lat, lng } = props.station
const base = `https://www.google.com/maps/dir/?api=1&destination=${lat},${lng}`
if (props.origin?.lat != null && props.origin?.lng != null) {
return `${base}&origin=${props.origin.lat},${props.origin.lng}`
}
return base
}) })
</script> </script>

View File

@@ -34,6 +34,7 @@
v-for="station in reliable" v-for="station in reliable"
:key="station.station_id" :key="station.station_id"
:lowest-price="lowestPrice" :lowest-price="lowestPrice"
:origin="origin"
:station="station" :station="station"
/> />
</section> </section>
@@ -80,6 +81,7 @@
:key="station.station_id" :key="station.station_id"
:station="station" :station="station"
:lowest-price="lowestPrice" :lowest-price="lowestPrice"
:origin="origin"
/> />
</div> </div>
</div> </div>
@@ -92,6 +94,7 @@ import StationCard from './StationCard.vue'
const props = defineProps({ const props = defineProps({
stations: { type: Array, required: true }, stations: { type: Array, required: true },
currentSort: { type: String, default: 'reliable' }, currentSort: { type: String, default: 'reliable' },
origin: { type: Object, default: null },
}) })
const emit = defineEmits(['sort']) const emit = defineEmits(['sort'])

View File

@@ -44,7 +44,7 @@
Join 50,000+ UK drivers using real-time insights to find the cheapest petrol and time their fill-ups perfectly. Join 50,000+ UK drivers using real-time insights to find the cheapest petrol and time their fill-ups perfectly.
</p> </p>
<SearchBar @search="onSearch" /> <SearchBar :initial="searchInitial" @search="onSearch" />
<div class="flex items-center gap-4 pt-4"> <div class="flex items-center gap-4 pt-4">
<div class="flex -space-x-2"> <div class="flex -space-x-2">
@@ -118,8 +118,8 @@
<span class="font-medium">No stations found near you. Try a different postcode or increase the radius.</span> <span class="font-medium">No stations found near you. Try a different postcode or increase the radius.</span>
</div> </div>
<template v-else> <template v-else>
<LeafletMap :default-open="true" :radius-miles="radiusMiles" :stations="stations" /> <LeafletMap :default-open="true" :origin="searchOrigin" :radius-miles="radiusMiles" :stations="stations" />
<StationList :stations="stations" :current-sort="sort" @sort="onSort" /> <StationList :current-sort="sort" :origin="searchOrigin" :stations="stations" @sort="onSort" />
</template> </template>
</template> </template>
@@ -427,8 +427,8 @@
</template> </template>
<script setup> <script setup>
import { ref } from 'vue' import { ref, computed, watch } from 'vue'
import { RouterLink } from 'vue-router' import { RouterLink, useRoute, useRouter } from 'vue-router'
import { useAuth } from '../composables/useAuth.js' import { useAuth } from '../composables/useAuth.js'
import { useStations } from '../composables/useStations.js' import { useStations } from '../composables/useStations.js'
import SearchBar from '../components/SearchBar.vue' import SearchBar from '../components/SearchBar.vue'
@@ -471,14 +471,63 @@ const PRICES = {
annual: { basic: '£9.90', plus: '£24.90', pro: '£39.90' }, annual: { basic: '£9.90', plus: '£24.90', pro: '£39.90' },
} }
const PRICE_SUFFIX = { monthly: '/mo', annual: '/yr' } const PRICE_SUFFIX = { monthly: '/mo', annual: '/yr' }
const { stations, loading, error, search } = useStations() const { stations, meta, loading, error, search } = useStations()
const searchOrigin = computed(() => {
if (meta.value?.lat != null && meta.value?.lng != null) {
return { lat: meta.value.lat, lng: meta.value.lng }
}
return null
})
const route = useRoute()
const router = useRouter()
const sort = ref('reliable') const sort = ref('reliable')
const lastParams = ref(null) const lastParams = ref(null)
const searchAttempted = ref(false) const searchAttempted = ref(false)
const radiusMiles = ref(10) const radiusMiles = ref(10)
async function onSearch(params) { const searchInitial = computed(() => ({
postcode: route.query.postcode ?? '',
lat: route.query.lat ? Number(route.query.lat) : null,
lng: route.query.lng ? Number(route.query.lng) : null,
fuelType: route.query.fuel_type ?? 'e10',
radius: route.query.radius ? Number(route.query.radius) : 10,
sort: route.query.sort ?? 'reliable',
}))
function paramsFromQuery(query) {
const hasPostcode = typeof query.postcode === 'string' && query.postcode.trim().length > 0
const hasCoords = query.lat && query.lng
if (!hasPostcode && !hasCoords) return null
return {
postcode: hasPostcode ? query.postcode.trim() : null,
lat: hasCoords ? Number(query.lat) : null,
lng: hasCoords ? Number(query.lng) : null,
fuelType: query.fuel_type ?? 'e10',
radius: query.radius ? Number(query.radius) : 10,
sort: query.sort ?? 'reliable',
}
}
function queryFromParams(params) {
const q = {
fuel_type: params.fuelType,
radius: String(params.radius),
sort: params.sort,
}
if (params.postcode) {
q.postcode = params.postcode
} else if (params.lat && params.lng) {
q.lat = String(params.lat)
q.lng = String(params.lng)
}
return q
}
async function runSearch(params) {
lastParams.value = params lastParams.value = params
sort.value = params.sort ?? sort.value sort.value = params.sort ?? sort.value
radiusMiles.value = params.radius ?? radiusMiles.value radiusMiles.value = params.radius ?? radiusMiles.value
@@ -486,10 +535,24 @@ async function onSearch(params) {
await search(params) await search(params)
} }
async function onSort(newSort) { async function onSearch(params) {
sort.value = newSort await router.push({ query: queryFromParams(params) })
if (lastParams.value) { await runSearch(params)
await search({ ...lastParams.value, sort: newSort })
}
} }
async function onSort(newSort) {
if (!lastParams.value) return
const next = { ...lastParams.value, sort: newSort }
await router.push({ query: queryFromParams(next) })
await runSearch(next)
}
watch(() => route.query, (query) => {
const params = paramsFromQuery(query)
if (!params) return
const sameAsLast = lastParams.value
&& JSON.stringify(queryFromParams(lastParams.value)) === JSON.stringify(queryFromParams(params))
if (sameAsLast) return
runSearch(params)
}, { immediate: true })
</script> </script>