Skip to content

Commit

Permalink
chore(ceremony): format and save
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Sep 14, 2024
1 parent 51e5802 commit aa677f5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 76 deletions.
9 changes: 2 additions & 7 deletions ceremony/src/lib/components/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<script lang="ts">
import Spinner from "$lib/components/Spinner.svelte";
import Spinner from "$lib/components/Spinner.svelte"
let {
children,
class: className = "",
loading = false,
...props
} = $props()
let { children, class: className = "", loading = false, ...props } = $props()
</script>

<button
Expand Down
57 changes: 28 additions & 29 deletions ceremony/src/lib/components/Join.svelte
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
<script lang="ts">
import H1 from "$lib/components/typography/H1.svelte"
import Button from "$lib/components/Button.svelte"
import Text from "$lib/components/typography/Text.svelte"
import {callJoinQueue} from "$lib/supabase"
import type {ContributorState} from "$lib/stores/state.svelte.ts";
import {user} from "$lib/stores/user.svelte.ts";
import {toast} from "svelte-sonner";
import H1 from "$lib/components/typography/H1.svelte"
import Button from "$lib/components/Button.svelte"
import Text from "$lib/components/typography/Text.svelte"
import { callJoinQueue } from "$lib/supabase"
import type { ContributorState } from "$lib/stores/state.svelte.ts"
import { user } from "$lib/stores/user.svelte.ts"
import { toast } from "svelte-sonner"
type Props = {
contributor: ContributorState
}
type Props = {
contributor: ContributorState
}
let {contributor}: Props = $props()
let { contributor }: Props = $props()
let code = $state("")
let codeLoading = $state(false)
let waitlistLoading = $state(false)
let code = $state("")
let codeLoading = $state(false)
let waitlistLoading = $state(false)
async function handleCode() {
codeLoading = true
const codeValid = await callJoinQueue(code)
if (codeValid) {
await contributor.checkAllowanceState(user.session?.user.id)
codeLoading = false
} else {
toast.error('The code is not valid')
codeLoading = false
code = ''
}
async function handleCode() {
codeLoading = true
const codeValid = await callJoinQueue(code)
if (codeValid) {
await contributor.checkAllowanceState(user.session?.user.id)
codeLoading = false
} else {
toast.error("The code is not valid")
codeLoading = false
code = ""
}
}
async function joinWaitlist() {
waitlistLoading = true
}
function joinWaitlist() {
waitlistLoading = true
}
</script>

<div>
Expand Down
30 changes: 12 additions & 18 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 {getAllowanceState, getUserQueueInfo, getContributionState} from "$lib/supabase"
import { onDestroy } from "svelte"
import { checkState } from "$lib/client"
import { getAllowanceState, getUserQueueInfo, getContributionState } from "$lib/supabase"

type IntervalID = NodeJS.Timeout | number

Expand All @@ -15,16 +15,9 @@ type State =
| "offline"
| "noClient"

export type AllowanceState =
"invited"
| "waitingList"
| undefined
export type AllowanceState = "invited" | "waitingList" | undefined

export type ContributionState =
"contribute"
| "contributed"
| "verifying"
| "notContributed"
export type ContributionState = "contribute" | "contributed" | "verifying" | "notContributed"

export type ClientState =
| "idle"
Expand Down Expand Up @@ -115,8 +108,8 @@ export class ContributorState {
}

async checkAllowanceState(userId: string | undefined): Promise<AllowanceState> {
this.allowanceState = await getAllowanceState(userId);
return this.allowanceState;
this.allowanceState = await getAllowanceState(userId)
return this.allowanceState
}

startPolling() {
Expand Down Expand Up @@ -150,7 +143,10 @@ export class ContributorState {

private startClientStatePolling() {
this.pollClientState()
this.pollIntervals.client = setInterval(() => this.pollClientState(), CLIENT_POLING_INTERVAL) as IntervalID
this.pollIntervals.client = setInterval(
() => this.pollClientState(),
CLIENT_POLING_INTERVAL
) as IntervalID
}

private stopClientStatePolling() {
Expand Down Expand Up @@ -245,7 +241,7 @@ export class ContributorState {
}

private setError(message: string) {
this.queueState = {...this.queueState, error: message}
this.queueState = { ...this.queueState, error: message }
this.state = "error"
}

Expand All @@ -254,7 +250,6 @@ export class ContributorState {
console.log("ContributionState:", this.contributionState)

if (this.contributionState === "contribute") {

switch (this.clientState) {
case "idle":
case "initializing":
Expand All @@ -278,7 +273,6 @@ export class ContributorState {
this.state = "contribute"
break
}

} else if (this.queueState.position !== null) {
this.state = "inQueue"
} else if (this.contributionState === "contributed") {
Expand Down
20 changes: 11 additions & 9 deletions ceremony/src/lib/supabase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
getContributor,
getQueueCount,
getSubmittedContribution,
getUserQueuePosition, queryAllowance
getUserQueuePosition,
queryAllowance
} from "$lib/supabase/queries.ts"
import { supabase } from "$lib/supabase/client.ts"
import type {AllowanceState, ContributionState} from "$lib/stores/state.svelte.ts"
import type { AllowanceState, ContributionState } from "$lib/stores/state.svelte.ts"

export const callJoinQueue = async (codeId: string): Promise<boolean> => {
const userId = user.session?.user.id
Expand Down Expand Up @@ -101,15 +102,16 @@ export const getContributionState = async (): Promise<ContributionState> => {
}

export const getAllowanceState = async (userId: string | undefined): Promise<AllowanceState> => {
if (!userId && !user.session?.user.id) {
console.log("User ID is required")
if (!userId) {
console.log("Need to be logged in to get allowance state")
return
}

const { data, error } = await queryAllowance(userId);
if (error || !data) return undefined;
const { data, error } = await queryAllowance()
if (error || !data) return undefined

if (data.in_waitlist) return 'waitingList';
if (data.has_redeemed) return 'invited';
if (data.in_waitlist) return "waitingList"
if (data.has_redeemed) return "invited"

return undefined;
return undefined
}
3 changes: 1 addition & 2 deletions ceremony/src/lib/supabase/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ export const getQueuePayloadId = async (userId: string) => {
return { data, error }
}

export const queryAllowance = async (userId: string) => {
export const queryAllowance = async () => {
const { data, error } = await supabase
.from("current_user_state")
.select("in_waitlist, has_redeemed")
.single()

return { data, error }
}

26 changes: 15 additions & 11 deletions ceremony/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<script lang="ts">
import {user} from "$lib/stores/user.svelte.ts"
import H1 from "$lib/components/typography/H1.svelte"
import {ContributorState} from "$lib/stores/state.svelte.ts"
import Ceremony from "$lib/components/Ceremony.svelte"
import Join from "$lib/components/Join.svelte"
import { user } from "$lib/stores/user.svelte.ts"
import H1 from "$lib/components/typography/H1.svelte"
import { ContributorState } from "$lib/stores/state.svelte.ts"
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()
//Improvements
//Only start polling depending on allowance, no need to poll if not allowed.
//
$effect(() => {
const userId = user.session?.user.id
if (userId) contributor.setUserId(userId)
})
//This could be set with context API if we expand the app a lot.
let contributor: ContributorState = new ContributorState()
$effect(() => {
const userId = user.session?.user.id
if (userId) contributor.setUserId(userId)
})
</script>

<!--Maybe add loading state to handle text jump-->
Expand Down

0 comments on commit aa677f5

Please sign in to comment.