- add /cool, /cool?vote=1, /cool/stage and /cool/city on the existing Nuxt/Onyx stack - wire real route/shade metrics and OSM geometry from the route bundle; city-gap data for the city view - show shade % and direct-sun minutes together; recommend Accessible for Ruth with the 15% ramp trade-off - add in-memory time/scene/round state and one anonymous vote per session, polled every 2 s - keep the retail module linked but out of the Cool City story
This commit is contained in:
1 parent
33063afbdf
commit
2bc52a314d
28 files changed
+8633
-45
No files matched your search
@@ -0,0 +1,8 @@
|
||||
import type { CoolCityBundle } from '#shared/cool'
|
||||
|
||||
export async function useCoolCityData() {
|
||||
const { data } = await useFetch<CoolCityBundle>('/cool-city/city-gap.json', {
|
||||
key: 'cool-city-gap',
|
||||
})
|
||||
return { city: data }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { defaultCoolState, emptyCoolTally } from '#shared/cool'
|
||||
import type { CoolDemoState, CoolRound, CoolVoteTally } from '#shared/cool'
|
||||
|
||||
/** State + tally polling for the stage and phone. 2 s is the demo fallback. */
|
||||
export function useCoolDemo(
|
||||
round: Ref<CoolRound>,
|
||||
initialState?: CoolDemoState | null,
|
||||
initialTally?: CoolVoteTally | null,
|
||||
) {
|
||||
const state = ref<CoolDemoState>(initialState ?? defaultCoolState(round.value))
|
||||
const tally = ref<CoolVoteTally>(initialTally ?? emptyCoolTally(round.value))
|
||||
const feedError = ref(false)
|
||||
let timer: ReturnType<typeof setInterval> | undefined
|
||||
|
||||
watch(round, () => {
|
||||
state.value = defaultCoolState(round.value)
|
||||
tally.value = emptyCoolTally(round.value)
|
||||
void poll()
|
||||
})
|
||||
|
||||
async function poll() {
|
||||
const [stateResult, tallyResult] = await Promise.allSettled([
|
||||
$fetch<CoolDemoState>('/api/cool/state', {
|
||||
query: { round: round.value },
|
||||
headers: { accept: 'application/json', 'cache-control': 'no-cache' },
|
||||
}),
|
||||
$fetch<CoolVoteTally>('/api/cool/vote', {
|
||||
query: { round: round.value },
|
||||
headers: { accept: 'application/json', 'cache-control': 'no-cache' },
|
||||
}),
|
||||
])
|
||||
if (stateResult.status === 'fulfilled') state.value = stateResult.value
|
||||
if (tallyResult.status === 'fulfilled') tally.value = tallyResult.value
|
||||
feedError.value = stateResult.status === 'rejected' || tallyResult.status === 'rejected'
|
||||
}
|
||||
|
||||
async function update(patch: Partial<Pick<CoolDemoState, 'time' | 'selectedRoute' | 'scene' | 'voteOpen'>>) {
|
||||
state.value = { ...state.value, ...patch, updatedAt: new Date().toISOString() }
|
||||
try {
|
||||
const next = await $fetch<CoolDemoState>('/api/cool/state', {
|
||||
method: 'POST',
|
||||
body: { round: round.value, ...patch },
|
||||
})
|
||||
state.value = next
|
||||
} catch {
|
||||
feedError.value = true
|
||||
}
|
||||
}
|
||||
|
||||
async function reset() {
|
||||
try {
|
||||
const result = await $fetch<{ state: CoolDemoState; tally: CoolVoteTally }>('/api/cool/reset', {
|
||||
method: 'POST',
|
||||
body: { round: round.value },
|
||||
})
|
||||
state.value = result.state
|
||||
tally.value = result.tally
|
||||
feedError.value = false
|
||||
} catch {
|
||||
feedError.value = true
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
timer = setInterval(() => void poll(), 2000)
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
if (timer) clearInterval(timer)
|
||||
})
|
||||
|
||||
return { state, tally, feedError, update, reset, poll }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { pendingCoolRouteBundle } from '#shared/cool'
|
||||
import type { CoolRouteBundle, CoolRouteId, CoolRouteValues, CoolTime } from '#shared/cool'
|
||||
|
||||
export async function useCoolRouteData() {
|
||||
const { data } = await useFetch<CoolRouteBundle>('/cool-city/route.json', {
|
||||
key: 'cool-route-bundle',
|
||||
})
|
||||
const bundle = computed(() => data.value ?? pendingCoolRouteBundle())
|
||||
const pending = computed(() => bundle.value.status !== 'ready')
|
||||
|
||||
function routeForTime(time: CoolTime | string): CoolRouteId {
|
||||
return bundle.value.recommendationByTime[time] ?? 'shortest'
|
||||
}
|
||||
|
||||
function valuesFor(routeId: CoolRouteId, time: CoolTime | string): CoolRouteValues | null {
|
||||
return bundle.value.routes.find((route) => route.id === routeId)?.values[time] ?? null
|
||||
}
|
||||
|
||||
return { bundle, pending, routeForTime, valuesFor }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { isCoolRouteId } from '#shared/cool'
|
||||
import type { CoolRouteId, CoolRound, CoolVoteResponse, CoolVoteTally } from '#shared/cool'
|
||||
|
||||
const SESSION_KEY = 'cool-city-session-v1'
|
||||
|
||||
/** One anonymous vote per browser and round. No PII. */
|
||||
export function useCoolVote(round: Ref<CoolRound>) {
|
||||
const mounted = ref(false)
|
||||
const sessionId = ref('')
|
||||
const myVote = ref<CoolRouteId | ''>('')
|
||||
const busy = ref(false)
|
||||
const error = ref('')
|
||||
const tally = ref<CoolVoteTally | null>(null)
|
||||
|
||||
function voteKey(value: CoolRound) {
|
||||
return `cool-city-vote-v1:${value}`
|
||||
}
|
||||
|
||||
function makeSessionId(): string {
|
||||
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) return crypto.randomUUID()
|
||||
return `cool_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`
|
||||
}
|
||||
|
||||
function readStoredVote(value: CoolRound): CoolRouteId | '' {
|
||||
const stored = localStorage.getItem(voteKey(value))
|
||||
return isCoolRouteId(stored) ? stored : ''
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
let storedSession = localStorage.getItem(SESSION_KEY)
|
||||
if (!storedSession || storedSession.length < 8) {
|
||||
storedSession = makeSessionId()
|
||||
localStorage.setItem(SESSION_KEY, storedSession)
|
||||
}
|
||||
sessionId.value = storedSession
|
||||
myVote.value = readStoredVote(round.value)
|
||||
mounted.value = true
|
||||
})
|
||||
|
||||
watch(round, (value) => {
|
||||
if (!mounted.value) return
|
||||
myVote.value = readStoredVote(value)
|
||||
tally.value = null
|
||||
error.value = ''
|
||||
})
|
||||
|
||||
async function vote(route: CoolRouteId) {
|
||||
if (!mounted.value || busy.value || myVote.value || !sessionId.value) return
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const response = await $fetch<CoolVoteResponse>('/api/cool/vote', {
|
||||
method: 'POST',
|
||||
body: { route, sessionId: sessionId.value, round: round.value },
|
||||
})
|
||||
myVote.value = response.yourRoute
|
||||
tally.value = response.tally
|
||||
localStorage.setItem(voteKey(round.value), response.yourRoute)
|
||||
} catch (cause) {
|
||||
const status = (cause as { statusCode?: number; status?: number })?.statusCode ??
|
||||
(cause as { status?: number })?.status
|
||||
error.value =
|
||||
status === 409
|
||||
? 'Voting is not open right now.'
|
||||
: 'The vote could not be recorded. Try again.'
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return { mounted, myVote, busy, error, tally, vote }
|
||||
}
|
||||
Reference in new issue
Block a user