From 3af85de7474a07e867a777f4727c2a372c6b3e12 Mon Sep 17 00:00:00 2001 From: Ota Prokopec Date: Thu, 6 Apr 2023 15:41:30 +0200 Subject: [PATCH] blazing fast loading (experience) --- src/lib/utils/database/experience.ts | 28 ++++++++----------- .../register/emailVerification.svelte | 2 +- src/routes/game/experience.svelte | 6 ++-- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/lib/utils/database/experience.ts b/src/lib/utils/database/experience.ts index 8028c69..1ae8660 100644 --- a/src/lib/utils/database/experience.ts +++ b/src/lib/utils/database/experience.ts @@ -13,24 +13,19 @@ userStore.subscribe((res) => (user = res)) //Loading of checkpoints and rating is done in the same function to prevent multiple requests to the database export const load = async (pathName: string, previewQuestionsCount?: number, preview?: Function) => { - const checkPoints: Array = [] + let checkPoints: Array = [] // @ts-ignore const experience: Experience = (await databases.listDocuments('63cef30d6da945dd4250', '63cef4bd210fdf2e5888', [Query.equal('ExpURL', pathName)])).documents[0] - const checkPointsIds = experience.ExpCPsID const rating = await getRating(experience.$id) - for (const checkPointId of checkPointsIds) { - try { - // @ts-ignore - checkPoints.push(await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)) - if (checkPointsIds.indexOf(checkPointId) === previewQuestionsCount - 1) { + // @ts-ignore + checkPoints = (await databases.listDocuments('63cef30d6da945dd4250', '63cef84d908acf805758', [Query.equal('$id', experience.ExpCPsID)])).documents + /*if (checkPointsIds.indexOf(checkPointId) === previewQuestionsCount - 1) { experience['rating'] = rating experience['checkPoints'] = checkPoints preview(experience) - } - } catch (error) {} - } + }*/ experience['rating'] = rating experience['checkPoints'] = checkPoints @@ -40,10 +35,10 @@ export const load = async (pathName: string, previewQuestionsCount?: number, pre export const loadAsStore = (pathName: string, previewQuestionsCount?: number, preview?: Function) => { const store = writable(null) - load(pathName, previewQuestionsCount, preview).then(experience => { + load(pathName, previewQuestionsCount, preview).then((experience) => { store.set(experience) }) - return [store] + return [store] } //Fetch of Rating @@ -57,7 +52,6 @@ const getRating = async (expId: string) => { export const answer = async (experienceId: string, checkPointId: string, answer: any) => { try { const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId) - console.log({ checkPoint }) const { CPType, CPAnswerID } = checkPoint const correctAnswer = CPType !== 'INFO' ? (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer : true @@ -154,12 +148,12 @@ export const getUserProgressAsStore = (experienceId: string) => { return [store, loading] as const } -export const getExperienceIdByUrl = async(url: string) => { - return (await databases.listDocuments("63cef30d6da945dd4250", "63cef4bd210fdf2e5888", [Query.equal("ExpURL", url)])).documents[0].$id +export const getExperienceIdByUrl = async (url: string) => { + return (await databases.listDocuments('63cef30d6da945dd4250', '63cef4bd210fdf2e5888', [Query.equal('ExpURL', url)])).documents[0].$id } export const getExperienceIdByUrlAsStore = (url: string): [Writable] => { const store = writable(null) - getExperienceIdByUrl(url).then(exp => store.set(exp)) - return [store] + getExperienceIdByUrl(url).then((exp) => store.set(exp)) + return [store] } // !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {} diff --git a/src/routes/authorization/register/emailVerification.svelte b/src/routes/authorization/register/emailVerification.svelte index 0296de3..31921a5 100644 --- a/src/routes/authorization/register/emailVerification.svelte +++ b/src/routes/authorization/register/emailVerification.svelte @@ -14,7 +14,7 @@ await user.updateVerification(userId, secret) navigate(`/create/account/${params.erantId}`) } catch (err) { - console.log(err) + navigate('/') } })() diff --git a/src/routes/game/experience.svelte b/src/routes/game/experience.svelte index 70fe047..e94e615 100644 --- a/src/routes/game/experience.svelte +++ b/src/routes/game/experience.svelte @@ -31,10 +31,8 @@ onMount(async () => { try { - $gameData = await load(params.gameurl, (await getUserProgress(await getExperienceIdByUrl(params.gameurl))).length || 8, (preview) => { - $gameData = preview - view = 'experience-preview' - }) + $gameData = await load(params.gameurl) + view = 'experience-preview' } catch (error) { navigate('/error') }