feat: create org wizard ✨

This commit is contained in:
Arman committed 2024-05-07 16:10:05 +02:00
1 parent 76b2ba4f93
commit 5894eb8885
6 files changed
+126 -81

No files matched your search

@@ -70,6 +70,7 @@
<FakeModal bind:show title="Add payment method" bind:error onSubmit={handleSubmit}>
<FormList gap={16}>
<slot />
<InputText
id="name"
label="Cardholder name"
@@ -1,15 +1,18 @@
<script lang="ts">
import { Button, InputSelectSearch } from '$lib/elements/forms';
import { Button, InputChoice, InputSelectSearch, InputText } from '$lib/elements/forms';
import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing';
import { sdk } from '$lib/stores/sdk';
import { hasStripePublicKey, isCloud } from '$lib/system';
import { onMount } from 'svelte';
import { Card, CreditCardBrandImage } from '..';
import PaymentModal from './paymentModal.svelte';
import { capitalize } from '$lib/helpers/string';
export let methods: PaymentList;
export let value: string;
export let taxId = '';
let showTaxId = false;
let showPaymentModal = false;
async function cardSaved(event: CustomEvent<PaymentMethodData>) {
@@ -35,21 +38,34 @@
options={filteredMethods.map((method) => {
return {
value: method.$id,
label: `ending in ${method.last4}`,
label: `${capitalize(method.brand)} ending in ${method.last4}`,
data: [method.brand]
};
})}
interactiveOutput
let:option={o}>
<svelte:fragment slot="output" let:option={o}>
<output class="input-text">
<span class="u-flex u-gap-16 u-flex-vertical">
<span class="u-flex u-gap-16">
<span class="u-flex u-cross-center u-gap-8" style="padding-inline:0.25rem">
<span>{o.label}</span>
<CreditCardBrandImage brand={o.data?.toString()} />
</span>
</span>
</span>
</output>
</svelte:fragment>
<span class="u-flex u-gap-16 u-flex-vertical">
<span class="u-flex u-gap-16">
<span class="u-flex u-cross-center u-gap-8" style="padding-inline:0.25rem">
<span> <span class="u-capitalize">{o.data}</span> {o.label}</span>
<span>{o.label}</span>
<CreditCardBrandImage brand={o.data?.toString()} />
</span>
</span>
</span>
<svelte:fragment slot="end">
<svelte:fragment slot="listEnd">
<Button text on:click={() => (showPaymentModal = true)}>
<span class="icon-plus"></span>
<span class="text">Add new payment method</span>
@@ -71,5 +87,18 @@
{/if}
{#if showPaymentModal && isCloud && hasStripePublicKey}
<PaymentModal bind:show={showPaymentModal} on:submit={cardSaved} />
<PaymentModal bind:show={showPaymentModal} on:submit={cardSaved}>
<InputChoice
type="checkbox"
id="taxIdCheck"
label="I'm purchasing as a bussiness"
fullWidth
bind:value={showTaxId}>
{#if showTaxId}
<div class="u-margin-block-start-16">
<InputText id="taxId" label="Tax ID" placeholder="Tax ID" bind:value={taxId} />
</div>
{/if}
</InputChoice>
</PaymentModal>
{/if}
+11 -1
View File
@@ -17,10 +17,19 @@
let classes = '';
export { classes as class };
const { isSubmitting } = setContext<FormContext>('form', {
let form: HTMLFormElement;
export let { isSubmitting } = setContext<FormContext>('form', {
isSubmitting: writable(false)
});
export function checkValidity() {
return form.checkValidity();
}
export function triggerSubmit() {
form.requestSubmit();
}
async function submit(e: SubmitEvent) {
isSubmitting.set(true);
await onSubmit(e);
@@ -29,6 +38,7 @@
</script>
<form
bind:this={form}
class={classes}
class:form={!noStyle}
class:common-section={!noMargin}
@@ -194,11 +194,11 @@
{/each}
</svelte:fragment>
<svelte:fragment slot="other">
{#if $$slots.end}
{#if $$slots.listEnd}
<section class="drop-section">
<ul class="drop-list">
<li class="drop-list-item">
<slot name="end" />
<slot name="listEnd" />
</li>
</ul>
</section>
@@ -33,6 +33,7 @@
import { sdk } from '$lib/stores/sdk';
import { ID } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { writable } from 'svelte/store';
$: anyOrgFree = $organizationList.teams?.find(
(org) => (org as Organization)?.billingPlan === BillingPlan.STARTER
@@ -40,6 +41,10 @@
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/i;
const today = new Date();
const billingPayDate = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);
let formComponent: Form;
let isSubmitting = writable(false);
let methods: PaymentList;
let name: string;
let billingPlan: BillingPlan = BillingPlan.STARTER;
@@ -50,6 +55,7 @@
status: null,
credits: null
};
let taxId: string;
let budgetEnabled = false;
let billingBudget: number;
let showCreditModal = false;
@@ -67,44 +73,55 @@
async function create() {
try {
const org = await sdk.forConsole.billing.createOrganization(
ID.unique(),
name,
billingPlan,
paymentMethodId,
'billingAddressId'
);
let org: Organization;
if (billingPlan === BillingPlan.STARTER) {
org = await sdk.forConsole.billing.createOrganization(
ID.unique(),
name,
BillingPlan.STARTER,
null,
null
);
} else {
org = await sdk.forConsole.billing.createOrganization(
ID.unique(),
name,
billingPlan,
paymentMethodId,
'663a2ebf0ac1f0348a58' //TODO: change hardcoded address when it's not mandatory anymore
);
//Add budget
if (billingBudget) {
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
//Add budget
if (billingBudget) {
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
}
//Add coupon
if (couponData?.code) {
await sdk.forConsole.billing.addCredit(org.$id, couponData.code);
trackEvent(Submit.CreditRedeem);
}
//Add collaborators
if (collaborators?.length) {
collaborators.forEach(async (collaborator) => {
await sdk.forConsole.teams.createMembership(
org.$id,
['owner'],
collaborator,
undefined,
undefined,
`${$page.url.origin}/console/organization-${org.$id}`
);
});
}
// Add tax ID
if (taxId) {
await sdk.forConsole.billing.updateTaxId(org.$id, taxId);
}
}
//Add coupon
if (couponData?.code) {
await sdk.forConsole.billing.addCredit(org.$id, couponData.code);
trackEvent(Submit.CreditRedeem);
}
//Add collaborators
if (collaborators?.length) {
collaborators.forEach(async (collaborator) => {
await sdk.forConsole.teams.createMembership(
org.$id,
['owner'],
collaborator,
undefined,
undefined,
`${$page.url.origin}/console/organization-${org.$id}`
);
});
}
//Add tax ID
// if (taxId) {
// await sdk.forConsole.billing.updateTaxId(org.$id, taxId);
// }
trackEvent(Submit.OrganizationCreate, {
plan: tierToPlan(billingPlan)?.name,
budget_cap_enabled: !!billingBudget,
@@ -150,7 +167,7 @@
<WizardSecondaryContainer>
<WizardSecondaryHeader href={`${base}/console`}>Create organization</WizardSecondaryHeader>
<WizardSecondaryContent>
<Form onSubmit={create}>
<Form bind:this={formComponent} onSubmit={create} bind:isSubmitting>
<FormList>
<InputText
bind:value={name}
@@ -219,7 +236,7 @@
validityRegex={emailRegex}
validityMessage="Invalid email address"
id="members" />
<SelectPaymentMethod bind:methods bind:value={paymentMethodId}
<SelectPaymentMethod bind:methods bind:value={paymentMethodId} bind:taxId
></SelectPaymentMethod>
</FormList>
{#if !couponData?.code}
@@ -253,7 +270,13 @@
<span
class="icon-tag u-color-text-success"
aria-hidden="true" />
{couponData.code.toUpperCase()}
{#if couponData.credits > 100}
{couponData.code.toUpperCase()}
{:else}
<span
use:tooltip={{ content: couponData.code.toUpperCase() }}
>Credits applied</span>
{/if}
</p>
<button
type="button"
@@ -270,11 +293,16 @@
<span class="icon-x" aria-hidden="true" />
</button>
</div>
<p
class="inline-tag"
use:tooltip={{ content: formatCurrency(couponData.credits) }}>
Credits applied
</p>
{#if couponData.credits > 100}
<p
class="inline-tag"
use:tooltip={{ content: formatCurrency(couponData.credits) }}>
Credits applied
</p>
{:else}
<span class="u-color-text-success"
>-{formatCurrency(couponData.credits)}</span>
{/if}
</span>
{/if}
<div class="u-sep-block-start" />
@@ -298,7 +326,7 @@
<InputChoice
type="switchbox"
id="budget"
label="Budget cap"
label="Enable budget cap"
tooltip="If enabled, you will be notified when your spending reaches 75% of the set cap. Update cap alerts in your organization settings."
fullWidth
bind:value={budgetEnabled}>
@@ -321,7 +349,12 @@
<WizardSecondaryFooter>
<Button fullWidthMobile href={`${base}/console`} secondary>Cancel</Button>
<Button fullWidthMobile on:click>Create organization</Button>
<Button
fullWidthMobile
on:click={() => formComponent.triggerSubmit()}
disabled={$isSubmitting}>
Create organization
</Button>
</WizardSecondaryFooter>
</WizardSecondaryContainer>
@@ -1,28 +0,0 @@
import { BillingPlan } from '$lib/constants';
import type { WizardStepsType } from '$lib/layout/wizard.svelte';
import type { Tier } from '$lib/stores/billing';
import { writable } from 'svelte/store';
export const createOrgSteps = writable<WizardStepsType>(new Map());
export const createOrganizationFinalAction = writable<string>('Start trial');
export const createOrganization = writable<{
id?: string;
name: string;
billingPlan: Tier;
paymentMethodId: string;
billingAddressId: string;
collaborators?: string[];
billingBudget?: number;
taxId?: string;
couponCode?: string;
}>({
id: null,
name: null,
billingPlan: BillingPlan.PRO,
paymentMethodId: null,
collaborators: [],
billingAddressId: null,
taxId: null,
couponCode: null
});