Skip to content

Commit

Permalink
fix(ceremony): get user allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Sep 14, 2024
1 parent ed7b95d commit 45ab24d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
13 changes: 11 additions & 2 deletions ceremony/src/lib/components/Join.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
Expand Down
41 changes: 10 additions & 31 deletions ceremony/src/lib/stores/state.svelte.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<string | undefined>(undefined)
loggedIn = $state<boolean>(false)
allowanceState = $state<AllowanceState>(undefined)

pollingState = $state<"stopped" | "polling">("stopped")
state = $state<State>("loading")
clientState = $state<ClientState>("offline")
Expand Down Expand Up @@ -99,6 +97,7 @@ export class ContributorState {
if (userId) {
this.userId = userId
this.loggedIn = true
this.checkAllowanceState(userId)
this.startPolling()
}
onDestroy(() => {
Expand All @@ -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<AllowanceState> {
this.allowanceState = await getAllowanceState(userId);
return this.allowanceState;
}

startPolling() {
if (this.pollingState === "polling") {
console.log("Polling is already running.")
Expand All @@ -126,7 +131,6 @@ export class ContributorState {
}

this.pollingState = "polling"
this.startAllowanceStatePolling()
this.startClientStatePolling()
this.startQueueInfoPolling()
this.startContributionStatePolling()
Expand All @@ -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() {
Expand Down Expand Up @@ -224,18 +207,14 @@ 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)
this.setError(error instanceof Error ? error.message : "Unknown error occurred")
}
}

private updateAllowanceState(state: AllowanceState) {
this.allowanceState = state
}

private updateClientState(state: ClientState) {
this.clientState = state
this.updateState()
Expand Down
18 changes: 14 additions & 4 deletions ceremony/src/lib/supabase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -64,7 +64,7 @@ export const getUserQueueInfo = async () => {
}
}

export const checkContributionState = async (): Promise<ContributionState> => {
export const getContributionState = async (): Promise<ContributionState> => {
const userId = user.session?.user.id
if (!userId) {
throw new Error("User ID is required")
Expand Down Expand Up @@ -100,6 +100,16 @@ export const checkContributionState = async (): Promise<ContributionState> => {
}
}

export const checkAllowanceState = async (): Promise<AllowanceState> => {
return 'invited'
export const getAllowanceState = async (userId: string | undefined): Promise<AllowanceState> => {
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;
}
17 changes: 10 additions & 7 deletions ceremony/src/lib/supabase/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 }
}

4 changes: 3 additions & 1 deletion ceremony/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -13,13 +14,14 @@
})
</script>

<!--Maybe add loading state to handle text jump-->
{#if contributor.loggedIn}
{#if contributor.allowanceState === "invited"}
<Ceremony {contributor}/>
{:else if contributor.allowanceState === "waitingList"}
<H1>Your on the list</H1>
{:else}
<Join/>
<Join {contributor}/>
{/if}
{:else}
<H1>Welcome to union ceremony</H1>
Expand Down

0 comments on commit 45ab24d

Please sign in to comment.