it now also adds the awnsers to the db

This commit is contained in:
RezHackXYZ 2025-06-02 15:51:42 +05:30
parent 757f3906c4
commit 9da2ddc123
5 changed files with 58 additions and 5 deletions

View file

@ -16,4 +16,4 @@
<QuestionsList />
<div class="flex h-full w-[54%] flex-col gap-2"><HowTheQuestionWillLook /><Buttons /></div>
<QuestionOptions />
</div>
</div>

View file

@ -1,6 +1,7 @@
<script>
import { DefaultQuestions } from "$lib/config.js";
import { QuestionsData } from "./create.svelte";
import { createGame } from "./createGame";
</script>
<div class="flex w-full justify-center gap-2 overflow-y-auto rounded border-2 p-3 pr-5">
@ -10,7 +11,7 @@
<button class="btn flex items-center gap-1">
<i class="nf nf-md-cpu_64_bit"></i> Generate Questions Using AI
</button>
<button class="btn flex items-center gap-1">
<button class="btn flex items-center gap-1" onclick={() => createGame()}>
<i class="nf nf-md-play"></i> Start Game
</button>
</div>

View file

@ -0,0 +1,51 @@
import { supabase } from "$lib/supabase.js";
import { QuestionsData } from "./create.svelte.js";
import toast from "svelte-5-french-toast";
export async function createGame() {
let GamesData;
try {
GamesData = await toast.promise(
(async () => {
let { data, error } = await supabase.from("games").insert([{}]).select("id,gamepin");
if (error) {
throw new Error(error.message);
}
return data;
})(),
{
loading: "Adding Game...",
success: "Game added!",
error: (err) => `Could not add game.\nError: ${err.message}\n\n Please try again.`,
},
);
await toast.promise(
(async () => {
const { data, error } = await supabase
.from("questions")
.insert(
QuestionsData.v.map((_, index) => ({
...QuestionsData.v[index],
gameid: GamesData[0].id,
})),
)
.select();
if (error) {
throw new Error(error.message);
}
return data;
})(),
{
loading: "Adding Questions and Answers...",
success: "Questions and Answers added!",
error: (err) =>
`Could not add Questions and Answers.\nError: ${err.message}\n\n Please try again.`,
},
);
} catch (error) {
return;
}
toast.success(`Game created! Your game pin is: ${GamesData[0].gamepin}`);
}