diff --git a/ceremony/src/lib/components/Join.svelte b/ceremony/src/lib/components/Join.svelte index 6aecf74959..72f675f2e5 100644 --- a/ceremony/src/lib/components/Join.svelte +++ b/ceremony/src/lib/components/Join.svelte @@ -4,16 +4,25 @@ import Button from "$lib/components/Button.svelte" import Text from "$lib/components/typography/Text.svelte" import { callJoinQueue } from "$lib/supabase" import Spinner from "$lib/components/Spinner.svelte"; +import type {ContributorState} from "$lib/stores/state.svelte.ts"; +import {user} from "$lib/stores/user.svelte.ts"; + +type Props = { + contributor: ContributorState +} + +let { contributor }: Props = $props() let code = $state("") let loading = $state(false) async function handleCode() { loading = true - //Maybe check format etc const codeValid = await callJoinQueue(code) if (codeValid) { - console.log("valid") + await contributor.checkAllowanceState(user.session?.user.id) + } else { + console.log('not valid') } } diff --git a/ceremony/src/lib/stores/state.svelte.ts b/ceremony/src/lib/stores/state.svelte.ts index 2b03872146..3b678dfffd 100644 --- a/ceremony/src/lib/stores/state.svelte.ts +++ b/ceremony/src/lib/stores/state.svelte.ts @@ -1,6 +1,6 @@ import {onDestroy} from "svelte" import {checkState} from "$lib/client" -import {checkAllowanceState, checkContributionState, getUserQueueInfo} from "$lib/supabase" +import {getAllowanceState, getUserQueueInfo, getContributionState} from "$lib/supabase" type IntervalID = NodeJS.Timeout | number @@ -63,15 +63,13 @@ type QueueInfoResult = QueueInfoSuccess | QueueInfoError const second = 1000 const CLIENT_POLING_INTERVAL = second +const CONTRIBUTION_POLLING_INTERVAL = second * 10 const QUEUE_POLLING_INTERVAL = second * 10 -const CONTRIBUTION_POLLING_INTERVAL = second * 5 -const ALLOWANCE_POLLING_INTERVAL = second * 5 export class ContributorState { userId = $state(undefined) loggedIn = $state(false) allowanceState = $state(undefined) - pollingState = $state<"stopped" | "polling">("stopped") state = $state("loading") clientState = $state("offline") @@ -99,6 +97,7 @@ export class ContributorState { if (userId) { this.userId = userId this.loggedIn = true + this.checkAllowanceState(userId) this.startPolling() } onDestroy(() => { @@ -110,10 +109,16 @@ export class ContributorState { if (this.userId === undefined && userId) { this.userId = userId this.loggedIn = true + this.checkAllowanceState(userId) this.startPolling() } } + async checkAllowanceState(userId: string | undefined): Promise { + this.allowanceState = await getAllowanceState(userId); + return this.allowanceState; + } + startPolling() { if (this.pollingState === "polling") { console.log("Polling is already running.") @@ -126,7 +131,6 @@ export class ContributorState { } this.pollingState = "polling" - this.startAllowanceStatePolling() this.startClientStatePolling() this.startQueueInfoPolling() this.startContributionStatePolling() @@ -142,27 +146,6 @@ export class ContributorState { this.stopClientStatePolling() this.stopQueueInfoPolling() this.stopContributionStatePolling() - this.stopAllowanceStatePolling() - } - - private startAllowanceStatePolling() { - this.pollAllowanceState() - this.pollIntervals.allowance = setInterval( - () => this.pollAllowanceState(), - ALLOWANCE_POLLING_INTERVAL - ) as IntervalID - } - - private stopAllowanceStatePolling() { - if (this.pollIntervals.allowance) { - clearInterval(this.pollIntervals.allowance) - this.pollIntervals.allowance = null - } - } - - private async pollAllowanceState() { - const state = await checkAllowanceState() - this.updateAllowanceState(state) } private startClientStatePolling() { @@ -224,7 +207,7 @@ export class ContributorState { private async pollContributionState() { try { - const state = await checkContributionState() + const state = await getContributionState() this.updateContributionState(state) } catch (error) { console.log("Error polling contribution state:", error) @@ -232,10 +215,6 @@ export class ContributorState { } } - private updateAllowanceState(state: AllowanceState) { - this.allowanceState = state - } - private updateClientState(state: ClientState) { this.clientState = state this.updateState() diff --git a/ceremony/src/lib/supabase/index.ts b/ceremony/src/lib/supabase/index.ts index 60b2db273d..08901e95a9 100644 --- a/ceremony/src/lib/supabase/index.ts +++ b/ceremony/src/lib/supabase/index.ts @@ -4,7 +4,7 @@ import { getContributor, getQueueCount, getSubmittedContribution, - getUserQueuePosition + getUserQueuePosition, queryAllowance } from "$lib/supabase/queries.ts" import { supabase } from "$lib/supabase/client.ts" import type {AllowanceState, ContributionState} from "$lib/stores/state.svelte.ts" @@ -64,7 +64,7 @@ export const getUserQueueInfo = async () => { } } -export const checkContributionState = async (): Promise => { +export const getContributionState = async (): Promise => { const userId = user.session?.user.id if (!userId) { throw new Error("User ID is required") @@ -100,6 +100,16 @@ export const checkContributionState = async (): Promise => { } } -export const checkAllowanceState = async (): Promise => { - return 'invited' +export const getAllowanceState = async (userId: string | undefined): Promise => { + if (!userId && !user.session?.user.id) { + console.log("User ID is required") + } + + const { data, error } = await queryAllowance(userId); + if (error || !data) return undefined; + + if (data.in_waitlist) return 'waitingList'; + if (data.has_redeemed) return 'invited'; + + return undefined; } diff --git a/ceremony/src/lib/supabase/queries.ts b/ceremony/src/lib/supabase/queries.ts index 5fe0607290..a2a9456a64 100644 --- a/ceremony/src/lib/supabase/queries.ts +++ b/ceremony/src/lib/supabase/queries.ts @@ -46,12 +46,7 @@ export const getQueueCount = async () => { .from("current_queue") .select("*", { count: "exact", head: true }) - if (error) { - console.error("Error getting total count:", error) - return { count: undefined, error } - } - - return { count, error: undefined } + return { count, error } } export const getQueuePayloadId = async (userId: string) => { @@ -60,7 +55,15 @@ export const getQueuePayloadId = async (userId: string) => { .select("payload_id") .eq("id", userId) .single() + return { data, error } +} + +export const queryAllowance = async (userId: string) => { + const { data, error } = await supabase + .from("current_user_state") + .select("in_waitlist, has_redeemed") + .single() - if (error) console.error("Error in getQueuePayloadId:", error) return { data, error } } + diff --git a/ceremony/src/routes/+page.svelte b/ceremony/src/routes/+page.svelte index 5e6d23a4ec..c3c948e0ad 100644 --- a/ceremony/src/routes/+page.svelte +++ b/ceremony/src/routes/+page.svelte @@ -5,6 +5,7 @@ import Ceremony from "$lib/components/Ceremony.svelte" import Join from "$lib/components/Join.svelte" + //This could be set with context API if we expand the app a lot. let contributor: ContributorState = new ContributorState() $effect(() => { @@ -13,13 +14,14 @@ }) + {#if contributor.loggedIn} {#if contributor.allowanceState === "invited"} {:else if contributor.allowanceState === "waitingList"}

Your on the list

{:else} - + {/if} {:else}

Welcome to union ceremony