ci / check (push) Successful in 55s
- 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
73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
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 }
|
|
}
|