diff --git a/src/lib/components/ui/tabs/index.ts b/src/lib/components/ui/tabs/index.ts new file mode 100644 index 0000000..f1ab372 --- /dev/null +++ b/src/lib/components/ui/tabs/index.ts @@ -0,0 +1,18 @@ +import { Tabs as TabsPrimitive } from "bits-ui"; +import Content from "./tabs-content.svelte"; +import List from "./tabs-list.svelte"; +import Trigger from "./tabs-trigger.svelte"; + +const Root = TabsPrimitive.Root; + +export { + Root, + Content, + List, + Trigger, + // + Root as Tabs, + Content as TabsContent, + List as TabsList, + Trigger as TabsTrigger, +}; diff --git a/src/lib/components/ui/tabs/tabs-content.svelte b/src/lib/components/ui/tabs/tabs-content.svelte new file mode 100644 index 0000000..1758c7f --- /dev/null +++ b/src/lib/components/ui/tabs/tabs-content.svelte @@ -0,0 +1,21 @@ + + + diff --git a/src/lib/components/ui/tabs/tabs-list.svelte b/src/lib/components/ui/tabs/tabs-list.svelte new file mode 100644 index 0000000..1585d01 --- /dev/null +++ b/src/lib/components/ui/tabs/tabs-list.svelte @@ -0,0 +1,19 @@ + + + diff --git a/src/lib/components/ui/tabs/tabs-trigger.svelte b/src/lib/components/ui/tabs/tabs-trigger.svelte new file mode 100644 index 0000000..ef2e218 --- /dev/null +++ b/src/lib/components/ui/tabs/tabs-trigger.svelte @@ -0,0 +1,21 @@ + + + diff --git a/src/routes/api/rahvatarkus/pool/+server.ts b/src/routes/api/rahvatarkus/pool/+server.ts new file mode 100644 index 0000000..0be893f --- /dev/null +++ b/src/routes/api/rahvatarkus/pool/+server.ts @@ -0,0 +1,16 @@ +import { json } from '@sveltejs/kit'; +import { db } from '$lib/server/db'; +import { questions } from '$lib/server/db/schema'; +import { sql } from 'drizzle-orm'; + +export async function GET() { + const results = await db + .select({ + poolSize: sql`COUNT(CASE WHEN answer_count < 5 THEN 1 END)` + }) + .from(questions); + + return json({ + size: Number(results[0].poolSize) + }); +} diff --git a/src/routes/vinge/rahvatarkus/+layout.server.ts b/src/routes/vinge/rahvatarkus/+layout.server.ts index 29f066d..249dd79 100644 --- a/src/routes/vinge/rahvatarkus/+layout.server.ts +++ b/src/routes/vinge/rahvatarkus/+layout.server.ts @@ -8,36 +8,47 @@ import { zod } from 'sveltekit-superforms/adapters'; import { nanoid } from 'nanoid'; export const load: LayoutServerLoad = async ({ fetch, locals }) => { - const { session } = locals; + const { session } = locals; - if (!session?.data?.userId) { - await session.setData({ userId: nanoid() }); - await session.save(); - } + if (!session?.data?.userId) { + await session.setData({ userId: nanoid() }); + await session.save(); + } - const user = session.data.userId; + const user = session.data.userId; - const res = await fetch('/api/rahvatarkus/question') - .then(async (res) => { - const data = await res.json(); - return { ok: res.ok, data: data }; - }) - .then((data) => { - return data; - }); + let question: Question | undefined = undefined; - let question: Question | undefined = undefined; + const poolSize = await fetch('/api/rahvatarkus/pool') + .then((res) => { + return res.json(); + }) + .then((data) => { + return data.size; + }); - if (res.ok) { - question = res.data; - } + if (poolSize !== 0) { + const res = await fetch('/api/rahvatarkus/question') + .then(async (res) => { + const data = await res.json(); + return { ok: res.ok, data: data }; + }) + .then((data) => { + return data; + }); - return { - user: user, - question: question, - question_form: await superValidate(zod(questionSchema)), - answer_form: await superValidate({ questionId: question?.id }, zod(answerSchema), { - errors: false - }) - }; + if (res.ok) { + question = res.data; + } + } + + return { + user: user, + question: question, + poolSize, + question_form: await superValidate(zod(questionSchema)), + answer_form: await superValidate({ questionId: question?.id }, zod(answerSchema), { + errors: false + }) + }; }; diff --git a/src/routes/vinge/rahvatarkus/+layout.svelte b/src/routes/vinge/rahvatarkus/+layout.svelte index 5c8d3b8..87c2d5b 100644 --- a/src/routes/vinge/rahvatarkus/+layout.svelte +++ b/src/routes/vinge/rahvatarkus/+layout.svelte @@ -1,13 +1,27 @@ - - + + + Vasta + Küsi + + + + + + + + {@render children()} diff --git a/src/routes/vinge/rahvatarkus/+page.svelte b/src/routes/vinge/rahvatarkus/+page.svelte index 9ad0fc3..711e636 100644 --- a/src/routes/vinge/rahvatarkus/+page.svelte +++ b/src/routes/vinge/rahvatarkus/+page.svelte @@ -1,65 +1,90 @@ -{#await data.streamed.archive} -

loading

-{:then archive} - - {#each archive.data as question} - 0)} value={question.id}> - {question.content}? - -
    +
    +

    + Mida rahvas teab? +

    +
    + +
    + {#await data.streamed.archive} +

    loading

    + {:then archive} + + {#each archive.data as question} + 0)} value={question.id}> + {question.content}? + {#each question.answers as answer} -
  1. +
    {answer.content} -
  2. + {/each} -
-
-
- {/each} -
- { - goto(`?leht=${value}`); - }} - count={archive.meta.total} - perPage={data.pageSize} - page={data.page} - > - {#snippet children({ pages, currentPage })} - - - - - {#each pages as page (page.key)} - {#if page.type === 'ellipsis'} - - - - {:else} - - - {page.value} - - - {/if} - {/each} - - - - - {/snippet} - -{/await} + + + {/each} + + { + goto(`?leht=${value}`); + }} + count={archive.meta.total} + perPage={data.pageSize} + page={data.page} + {siblingCount} + class="my-8" + > + {#snippet children({ pages, currentPage })} + + + + + + +
+ {#each pages as page (page.key)} + {#if page.type === 'ellipsis'} + + + + {:else} + + + {page.value} + + + {/if} + {/each} +
+ + + + + +
+ {/snippet} +
+ {/await} + diff --git a/src/routes/vinge/rahvatarkus/answer-form.svelte b/src/routes/vinge/rahvatarkus/answer-form.svelte index 82bfdb1..499f67e 100644 --- a/src/routes/vinge/rahvatarkus/answer-form.svelte +++ b/src/routes/vinge/rahvatarkus/answer-form.svelte @@ -6,11 +6,15 @@ import { zodClient } from 'sveltekit-superforms/adapters'; import { type SuperValidated, type Infer, superForm } from 'sveltekit-superforms'; + import * as Card from '$lib/components/ui/card/index.js'; import * as Form from '$lib/components/ui/form/index.js'; import { Input } from '$lib/components/ui/input/index.js'; - let { data }: { data: { answer_form: SuperValidated>; question: Question } } = - $props(); + let { + data + }: { + data: { answer_form: SuperValidated>; question: Question; poolSize: number }; + } = $props(); const form = superForm(data.answer_form, { validators: zodClient(formSchema), @@ -27,25 +31,53 @@ const { form: formData, enhance } = form; -{#if data.question} -
- - - {#snippet children({ props })} - {data.question.content}? - - {/snippet} - - - - - - {#snippet children({ props })} - - {/snippet} - - - - Vasta -
-{/if} + + + Vasta vajajale + + {#if !data.question} + {#if data.poolSize === 0} + Rahval said kõik küsimused otsa! + {:else} + Oled kõigile vastanud, kuid enda küsimustel vastused puudu. + {/if} + {:else} + Tänutäheks saad vastu ühe küsimuse küsida. + {/if} + + + {#if !data.question} + + {#if data.poolSize === 0} +

Sellise erandjuhuga saad ühe korra niisama küsida!

+ {:else} +

Äkki tahaksid su sõbrad vastata või on meil mingi küsimus?

+ {/if} +
+ {:else} +
+ + + + {#snippet children({ props })} + {data.question.content}? + + {/snippet} + + + + + + {#snippet children({ props })} + + {/snippet} + + + + + + Vasta + +
+ {/if} +
diff --git a/src/routes/vinge/rahvatarkus/question-form.svelte b/src/routes/vinge/rahvatarkus/question-form.svelte index 780a555..8f377e7 100644 --- a/src/routes/vinge/rahvatarkus/question-form.svelte +++ b/src/routes/vinge/rahvatarkus/question-form.svelte @@ -1,14 +1,24 @@ -
- - - {#snippet children({ props })} - Uus küsimus - - {/snippet} - - Küsi ükskõik mida sellelt kollektiiv intelektilt. - - - Küsi -
+ + + Küsi rahvalt + Sul on alles 0 küsimust. Vasta teistele kõigepealt! + +
+ + + + {#snippet children({ props })} + Küsimus rahvale + + {/snippet} + + + + + + Küsi + +
+