Skip to content

Commit

Permalink
fix: separated createStripe from updateStripe form actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Torwent committed Jan 4, 2024
1 parent b009f52 commit 9e3c597
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
28 changes: 21 additions & 7 deletions src/routes/dashboard/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const load = async (event) => {
}

export const actions = {
linkStripe: async ({
createStripe: async ({
request,
locals: { supabaseServer, getSession, getProfile },
url: { origin }
Expand All @@ -62,13 +62,27 @@ export const actions = {

const scripter = await getScripterDashboard(supabaseServer, profile.id)

let link: string | undefined
if (!scripter.stripe) {
const form = await superValidate(promises[2], countryCodeSchema)
if (!form.valid) return setError(form, "", "The country code form is not valid!")
const form = await superValidate(promises[2], countryCodeSchema)
if (!form.valid) return setError(form, "", "The country code form is not valid!")
if (scripter.stripe) return setError(form, "", "Stripe account is already created!")

const link = await createStripeConnectAccount(supabaseServer, origin, scripter, form.data.code)
if (link) throw redirect(303, link)
return
},

updateStripe: async ({ locals: { supabaseServer, getSession, getProfile }, url: { origin } }) => {
const promises = await Promise.all([getSession(), getProfile()])
const profile = promises[1]

if (!promises[0] || !profile) {
return await doLogin(supabaseServer, origin, new URLSearchParams("login&provider=discord"))
}

const scripter = await getScripterDashboard(supabaseServer, profile.id)
if (!scripter.stripe) throw error(403, "You need a linked stripe account to edit it.")

link = await createStripeConnectAccount(supabaseServer, origin, scripter, form.data.code)
} else link = await finishStripeAccountSetup(origin, scripter.stripe)
const link = await finishStripeAccountSetup(origin, scripter.stripe)

if (link) throw redirect(303, link)
return
Expand Down
28 changes: 16 additions & 12 deletions src/routes/dashboard/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
const paymentContainer = document.getElementById("stripePaymentsContainer")
const payoutComponent = stripeConnectInstance.create("payouts")
const payoutContainer = document.getElementById("stripePayoutsContainer")
if (paymentContainer) paymentContainer.appendChild(paymentComponent)
if (payoutContainer) payoutContainer.appendChild(payoutComponent)
}
Expand Down Expand Up @@ -74,10 +74,10 @@
<div class="my-8 grid place-items-center">
<a href="/scripters/{scripter.url}" class="btn variant-filled-secondary">Scripter profile</a>
</div>
{#if !scripter.stripe}
<form method="POST" action="?/createStripe" class="my-32 grid place-items-center" use:enhance>
<h3>Stripe Account</h3>

<form method="POST" class="my-32 grid place-items-center" action="?/linkStripe" use:enhance>
<h3>Stripe Account</h3>
{#if !scripter.stripe}
<div class="my-4">
<label for="code">Choose a country (this cannot be changed later):</label>
<select class="select" name="code" id="code" bind:value={$form.code}>
Expand Down Expand Up @@ -152,10 +152,14 @@
<button disabled={$form.code === ""} class="btn variant-filled-secondary">
Create stripe connected account
</button>
{:else}
</form>
{:else}
<form method="POST" action="?/updateStripe" class="my-32 grid place-items-center">
<h3>Stripe Account</h3>

<button class="btn variant-filled-secondary">Update stripe connected account</button>
{/if}
</form>
</form>
{/if}

<h3 class="justify-center text-center my-12">General stats</h3>

Expand Down Expand Up @@ -239,11 +243,11 @@
/>

<div class="my-16 mx-auto max-w-7xl">
<h3 class="text-center"> Stripe Dashboard </h3>
<h5 class="text-center my-4"> Payments </h5>
<div id="stripePaymentsContainer" class="my-8"/>
<h5 class="text-center my-4"> Payouts </h5>
<h3 class="text-center">Stripe Dashboard</h3>

<h5 class="text-center my-4">Payments</h5>
<div id="stripePaymentsContainer" class="my-8" />
<h5 class="text-center my-4">Payouts</h5>
<div id="stripePayoutsContainer" class="my-8" />
</div>
{/if}
Expand Down

0 comments on commit 9e3c597

Please sign in to comment.