fixed that for somereson, the svelte toast when i wraped the insert game and other stuff in a svelte toast promise, it send the request twice that coused a issue so i have made it so the whole thing is in 1 promise!

This commit is contained in:
RezHackXYZ 2025-06-11 08:08:07 +05:30
parent 8ba9640398
commit 72a71d616c
No known key found for this signature in database
GPG key ID: C4C90E569C9669E2

View file

@ -12,6 +12,7 @@ export async function createGame() {
return; return;
} }
wait.v = true; wait.v = true;
const gamePin = Math.floor(Math.random() * 1000000) const gamePin = Math.floor(Math.random() * 1000000)
.toString() .toString()
.padStart(6, "0"); .padStart(6, "0");
@ -24,42 +25,30 @@ export async function createGame() {
media: q.hasMedia ? q.mediaURL : null, media: q.hasMedia ? q.mediaURL : null,
})); }));
const insertGamePromise = supabase.from("games").insert({ const gameCreationPromise = async () => {
const { data: gameData, error: gameError } = await supabase
.from("games")
.insert([
{
creator: "anonymous", creator: "anonymous",
creationdate: new Date().toISOString(), creationdate: new Date().toISOString(),
status: "lobby", status: "lobby",
gamepin: gamePin, gamepin: gamePin,
}); },
])
const { data: gameData, error: gameError } = await toast.promise(insertGamePromise, { .select();
loading: "Creating game...",
success: "Game created!",
error: (err) =>
"Failed to create game: " + (err?.message || "Unknown error") + "\n\nPlease try again.",
});
if (gameError) { if (gameError) {
//wait.v = false; throw gameError;
//return;
} }
const insertQuestionsPromise = supabase.from("questions").insert(questionsData).select("id"); const { data: questionsResult, error: questionsError } = await supabase
.from("questions")
const { data: questionsResult, error: questionsError } = await toast.promise( .insert(questionsData)
insertQuestionsPromise, .select("id");
{
loading: "Inserting questions...",
success: "Questions inserted!",
error: (err) =>
"Failed to insert questions: " +
(err?.message || "Unknown error") +
"\n\nPlease try again.",
},
);
if (questionsError) { if (questionsError) {
wait.v = false; throw questionsError;
return;
} }
const answersData = []; const answersData = [];
@ -72,16 +61,24 @@ export async function createGame() {
}); });
}); });
const insertAnswersPromise = supabase.from("answers").insert(answersData); const { error: answersError } = await supabase
.from("answers")
const { error: answersError } = await toast.promise(insertAnswersPromise, { .insert(answersData)
loading: "Inserting answers...", .select();
success: "Answers inserted!",
error: (err) =>
"Failed to insert answers: " + (err?.message || "Unknown error") + "\n\nPlease try again.",
});
if (answersError) { if (answersError) {
throw answersError;
}
};
try {
await toast.promise(gameCreationPromise(), {
loading: "Creating game...",
success: "Game created successfully!",
error: (err) =>
"Failed to create game: " + (err?.message || "Unknown error") + "\n\nPlease try again.",
});
} catch (error) {
wait.v = false; wait.v = false;
return; return;
} }