loading experience as store => better

This commit is contained in:
Ota Prokopec 2023-03-30 22:33:56 +02:00
parent 9c6f720684
commit b2721a9c37
2 changed files with 30 additions and 33 deletions

View File

@ -2,7 +2,7 @@ import { databases, user as userStore } from '$lib/appwrite'
import { Account, Models, Permission, Query, Role } from 'appwrite' import { Account, Models, Permission, Query, Role } from 'appwrite'
import database from 'svelte-appwrite-client/src/lib/database' import database from 'svelte-appwrite-client/src/lib/database'
import { getLocationDataFromLatAndLong } from '../locations' import { getLocationDataFromLatAndLong } from '../locations'
import { writable } from 'svelte/store' import { Writable, writable } from 'svelte/store'
import { CheckPoint, Experience } from '$lib/TStypes/experiences' import { CheckPoint, Experience } from '$lib/TStypes/experiences'
import collections from '$lib/collections' import collections from '$lib/collections'
@ -154,12 +154,12 @@ export const getUserProgressAsStore = (experienceId: string) => {
return [store, loading] as const return [store, loading] as const
} }
export const getExperienceByURL = async(url: string) => { export const getExperienceIdByUrl = async(url: string) => {
return (await databases.listDocuments("63cef30d6da945dd4250", "63cef4bd210fdf2e58880", [Query.equal("ExpURL", url)])).documents[0] return (await databases.listDocuments("63cef30d6da945dd4250", "63cef4bd210fdf2e5888", [Query.equal("ExpURL", url)])).documents[0].$id
} }
export const getExperienceByURLAsStore = (url: string) => { export const getExperienceIdByUrlAsStore = (url: string): [Writable<string>] => {
const store = writable<Models.Document>(null) const store = writable<string>(null)
getExperienceByURL(url).then(exp => store.set(exp)) getExperienceIdByUrl(url).then(exp => store.set(exp))
return [store] as const return [store]
} }
// !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {} // !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {}

View File

@ -12,7 +12,7 @@
import Renderer from './Forms/Renderer.svelte' import Renderer from './Forms/Renderer.svelte'
import Marker from '../../lib/components/Map/Marker.svelte' import Marker from '../../lib/components/Map/Marker.svelte'
import { onMount } from 'svelte' import { onMount } from 'svelte'
import { getExperienceByURLAsStore, getUserProgress, getUserProgressAsStore, load, loadAsStore } from '$lib/utils/database/experience' import { getExperienceIdByUrl, getExperienceIdByUrlAsStore, getUserProgress, getUserProgressAsStore, load, loadAsStore } from '$lib/utils/database/experience'
import { getLocationDataFromLatAndLong } from '$lib/utils/locations' import { getLocationDataFromLatAndLong } from '$lib/utils/locations'
import { navigate } from '$lib/router' import { navigate } from '$lib/router'
import { Experience } from '$lib/TStypes/experiences' import { Experience } from '$lib/TStypes/experiences'
@ -21,29 +21,26 @@
export let params: { gameurl: string } export let params: { gameurl: string }
let gameData = writable<Experience>(null) let gameData = writable<Experience>(null)
let [exp] = getExperienceByURLAsStore(params.gameurl) let [id] = getExperienceIdByUrlAsStore(params.gameurl)
$: [userProgress] = gameData ? getUserProgressAsStore($exp?.$id) : [] $: [userProgress] = $gameData ? getUserProgressAsStore($id) : []
/*
$: [gameData] = loadAsStore(params.gameurl, $userProgress?.length + 4 || 8, (preview) => {
$gameData = loadAsStore(params.gameurl, $userProgress.length+4 || 8, (preview) => {
$gameData = preview $gameData = preview
view = 'experience-preview' view = 'experience-preview'
}) })*/
/*onMount(async () => { onMount(async () => {
try { try {
console.log($userProgress?.length || 8) $gameData = await load(params.gameurl, (await getUserProgress(await getExperienceIdByUrl(params.gameurl))).length || 8, (preview) => {
$gameData = preview
gameData = await load(params.gameurl, (await getUserProgress(gameData?.$id)) || 8, (preview) => {
gameData = preview
view = 'experience-preview' view = 'experience-preview'
}) })
} catch (error) { } catch (error) {
navigate('/error') navigate('/error')
} }
})*/ })
/*$: assets = gameData?.questions /*$: assets = $gameData?.questions
?.filter((q) => q.thumbnail !== null) ?.filter((q) => q.thumbnail !== null)
?.slice(0, 8) ?.slice(0, 8)
?.map((q) => q.thumbnail)*/ ?.map((q) => q.thumbnail)*/
@ -52,7 +49,7 @@
//is user already in game //is user already in game
//const userInGame = JSON.parse(localStorage.getItem(location.pathname)) //const userInGame = JSON.parse(localStorage.getItem(location.pathname))
//$: if (gameData && userInGame) view = 'game-play' //$: if ($gameData && userInGame) view = 'game-play'
</script> </script>
{#if view === 'experience-loading'} {#if view === 'experience-loading'}
@ -61,27 +58,27 @@
<Loading /> <Loading />
</h1> </h1>
{:else if view === 'experience-preview'} {:else if view === 'experience-preview'}
<Overlay shareData={{ url: window.location.href }} img={gameData.ExpImage}> <Overlay shareData={{ url: window.location.href }} img={$gameData.ExpImage}>
<div> <div>
<span class="text-[36px]">{gameData.ExpName}</span> <span class="text-[36px]">{$gameData.ExpName}</span>
<div class="bubbles"> <div class="bubbles">
<Bubble background="blue"> <Bubble background="blue">
<span slot="icon"><IconStar /></span> <span slot="icon"><IconStar /></span>
<span> {gameData.rating} </span> <span> {$gameData.rating} </span>
</Bubble> </Bubble>
<Bubble background="white"> <Bubble background="white">
<span slot="icon"><IconPoint /></span> <span slot="icon"><IconPoint /></span>
{#await getLocationDataFromLatAndLong(gameData.ExpLocation[0], gameData.ExpLocation[1]) then { city }} {#await getLocationDataFromLatAndLong($gameData.ExpLocation[0], $gameData.ExpLocation[1]) then { city }}
<span>{city}</span> <span>{city}</span>
{/await} {/await}
</Bubble> </Bubble>
</div> </div>
</div> </div>
{#if gameData.ExpIntroduction} {#if $gameData.ExpIntroduction}
<Section title="Popis"> <Section title="Popis">
<span> <span>
{@html gameData.ExpIntroduction} {@html $gameData.ExpIntroduction}
</span> </span>
</Section> </Section>
{/if} {/if}
@ -94,25 +91,25 @@
</div> </div>
</Section> </Section>
<Section title="Progress"> <Section title="Progress">
<Progressbar max={gameData.ExpCPsID.length} progress={$userProgress.length} showWrittenProgress /> <Progressbar max={$gameData.ExpCPsID.length} progress={$userProgress.length} showWrittenProgress />
</Section> </Section>
<div class="w-full relative"> <div class="w-full relative">
<div class="px-4 m-auto" style="max-width: var(--max-viewport-width);"> <div class="px-4 m-auto" style="max-width: var(--max-viewport-width);">
<Map radius class="w-full h-44" center={{ lng: gameData.ExpLocation[1], lat: gameData.ExpLocation[0] }}> <Map radius class="w-full h-44" center={{ lng: $gameData.ExpLocation[1], lat: $gameData.ExpLocation[0] }}>
{#each gameData.checkPoints as { CPLocation: [lat, lng] }} {#each $gameData.checkPoints as { CPLocation: [lat, lng] }}
<Marker {lat} {lng} /> <Marker {lat} {lng} />
{/each} {/each}
</Map> </Map>
</div> </div>
</div> </div>
{#if gameData.checkPoints.length} {#if $gameData.checkPoints.length}
<Button on:click={() => (view = 'experience-play')} primary>{$userProgress ? 'Pokračovat' : 'Hrát'}</Button> <Button on:click={() => (view = 'experience-play')} primary>{$userProgress ? 'Pokračovat' : 'Hrát'}</Button>
{/if} {/if}
</Overlay> </Overlay>
{:else if view === 'experience-play'} {:else if view === 'experience-play'}
<Renderer {gameData} /> <Renderer gameData={$gameData} />
{/if} {/if}
<style> <style>