From 9126d4afadefdd8dcfe7f5766649404fb6a4aa4c Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Wed, 2 Aug 2023 12:54:31 +0200 Subject: [PATCH] random questions --- app/routes/random.tsx | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/routes/random.tsx diff --git a/app/routes/random.tsx b/app/routes/random.tsx new file mode 100644 index 00000000..f7a33f2a --- /dev/null +++ b/app/routes/random.tsx @@ -0,0 +1,60 @@ +import {useEffect} from 'react' +import type {LoaderFunction} from '@remix-run/cloudflare' +import {useOutletContext, useLoaderData} from '@remix-run/react' +import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache' +import {loadAllQuestions, QuestionState, QuestionStatus} from '~/server-utils/stampy' +import {Header, Footer} from '~/components/layouts' +import {Question} from '~/routes/questions/$question' +import useQuestionStateInUrl from '~/hooks/useQuestionStateInUrl' + +function randomItem(arr: any[]) { + return arr[Math.floor(Math.random() * arr.length)] +} + +export const loader = async ({request}: Parameters[0]) => { + try { + const {data} = await loadAllQuestions(request) + return { + ...randomItem(data.filter((q) => q.status == QuestionStatus.LIVE_ON_SITE)), + questionState: QuestionState.OPEN, + } + } catch (e) { + console.error(e) + } +} + +export default function App() { + const minLogo = useOutletContext() + const {initialQuestionsData} = useLoaderData>() + const {data: initialQuestions = [], timestamp} = initialQuestionsData ?? {} + + useEffect(() => { + if (timestamp) { + reloadInBackgroundIfNeeded('', timestamp) + } + }, [timestamp]) + + const {toggleQuestion, onLazyLoadQuestion, selectQuestion, glossary} = useQuestionStateInUrl( + minLogo, + initialQuestions + ) + + const question = useLoaderData>() + + return ( + <> +
+
+ +
+ +