mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
refactor: credit page
This commit is contained in:
1 parent
e4d25b3495
commit
90cca36567
6 files changed
+216
-8
No files matched your search
@@ -6,14 +6,14 @@
|
||||
export let label: string | undefined = undefined;
|
||||
export let optionalText: string | undefined = undefined;
|
||||
export let showLabel = true;
|
||||
export let value: string | number | boolean;
|
||||
export let value: string | number | boolean | null;
|
||||
export let placeholder = '';
|
||||
export let required = false;
|
||||
export let hideRequired = false;
|
||||
export let disabled = false;
|
||||
export let wrapperTag: FormItemTag = 'li';
|
||||
export let options: {
|
||||
value: string | boolean | number;
|
||||
value: string | boolean | number | null;
|
||||
label: string;
|
||||
}[];
|
||||
export let isMultiple = false;
|
||||
|
||||
@@ -64,6 +64,7 @@ export type Coupon = {
|
||||
expiration: string;
|
||||
status: string; // 'active' | 'disabled' | 'expired'
|
||||
validity: number;
|
||||
campaign?: string;
|
||||
};
|
||||
|
||||
export type Credit = {
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
<script lang="ts">
|
||||
import { afterNavigate, goto, invalidate } from '$app/navigation';
|
||||
import { base } from '$app/paths';
|
||||
import { page } from '$app/stores';
|
||||
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
|
||||
import { EstimatedTotalBox, SelectPaymentMethod } from '$lib/components/billing';
|
||||
import { BillingPlan, Dependencies } from '$lib/constants';
|
||||
import { Button, Form, FormList, InputSelect, InputTags } from '$lib/elements/forms';
|
||||
import {
|
||||
WizardSecondaryContainer,
|
||||
WizardSecondaryContent,
|
||||
WizardSecondaryFooter,
|
||||
WizardSecondaryHeader
|
||||
} from '$lib/layout';
|
||||
import { type PaymentList } from '$lib/sdk/billing';
|
||||
import { addNotification } from '$lib/stores/notifications';
|
||||
import { organizationList, type Organization } from '$lib/stores/organization';
|
||||
import { sdk } from '$lib/stores/sdk';
|
||||
import { onMount } from 'svelte';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export let data;
|
||||
|
||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/i;
|
||||
let previousPage: string = `${base}/console`;
|
||||
|
||||
afterNavigate(({ from }) => {
|
||||
previousPage = from?.url?.pathname || previousPage;
|
||||
});
|
||||
|
||||
let selectedOrgId: string = null;
|
||||
let formComponent: Form;
|
||||
let isSubmitting = writable(false);
|
||||
let methods: PaymentList;
|
||||
let paymentMethodId: string;
|
||||
let collaborators: string[];
|
||||
let taxId: string;
|
||||
let billingBudget: number;
|
||||
let options = [
|
||||
...$organizationList?.teams?.map((team) => ({
|
||||
value: team.$id,
|
||||
label: team.name
|
||||
})),
|
||||
{
|
||||
value: null,
|
||||
label: 'Create new organization'
|
||||
}
|
||||
];
|
||||
|
||||
onMount(async () => {
|
||||
await loadPaymentMethods();
|
||||
if ($organizationList?.total === 1) {
|
||||
selectedOrgId = $organizationList?.teams[0]?.$id;
|
||||
} else if (!$organizationList?.total) {
|
||||
selectedOrgId = null;
|
||||
}
|
||||
});
|
||||
|
||||
async function loadPaymentMethods() {
|
||||
methods = await sdk.forConsole.billing.listPaymentMethods();
|
||||
|
||||
paymentMethodId =
|
||||
selectedOrg?.paymentMethodId ??
|
||||
methods.paymentMethods.find((method) => !!method?.last4)?.$id ??
|
||||
null;
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
const org = await sdk.forConsole.billing.updatePlan(
|
||||
selectedOrg.$id,
|
||||
BillingPlan.PRO,
|
||||
paymentMethodId,
|
||||
null
|
||||
);
|
||||
|
||||
//Add coupon
|
||||
if (data.couponData?.code) {
|
||||
await sdk.forConsole.billing.addCredit(org.$id, data.couponData.code);
|
||||
}
|
||||
|
||||
//Add budget
|
||||
if (billingBudget) {
|
||||
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
await invalidate(Dependencies.ACCOUNT);
|
||||
await invalidate(Dependencies.ORGANIZATION);
|
||||
|
||||
await goto(`/console/organization-${org.$id}`);
|
||||
|
||||
addNotification({
|
||||
type: 'success',
|
||||
message: 'Credits applied successfully'
|
||||
});
|
||||
trackEvent(Submit.CreditRedeem);
|
||||
} catch (e) {
|
||||
addNotification({
|
||||
type: 'error',
|
||||
message: e.message
|
||||
});
|
||||
trackError(e, Submit.CreditRedeem);
|
||||
}
|
||||
}
|
||||
|
||||
$: selectedOrg = $organizationList?.teams?.find(
|
||||
(team) => team.$id === selectedOrgId
|
||||
) as Organization;
|
||||
|
||||
$: isButtonDisabled =
|
||||
selectedOrg?.paymentMethodId && selectedOrg?.billingPlan === BillingPlan.PRO;
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Apply credits - Appwrite</title>
|
||||
</svelte:head>
|
||||
|
||||
<WizardSecondaryContainer>
|
||||
<WizardSecondaryHeader href={previousPage}>Apply credits</WizardSecondaryHeader>
|
||||
<WizardSecondaryContent>
|
||||
{#if $organizationList?.total}
|
||||
<FormList>
|
||||
<InputSelect
|
||||
bind:value={selectedOrgId}
|
||||
label="Select organization"
|
||||
{options}
|
||||
required
|
||||
placeholder="Select organization"
|
||||
id="organization" />
|
||||
</FormList>
|
||||
{/if}
|
||||
{#if selectedOrg?.billingPlan !== BillingPlan.PRO || !selectedOrg?.paymentMethodId}
|
||||
<Form bind:this={formComponent} onSubmit={handleSubmit} bind:isSubmitting>
|
||||
<FormList>
|
||||
<InputTags
|
||||
bind:tags={collaborators}
|
||||
label="Invite members by email"
|
||||
tooltip="Invited members will have access to all services and payment data within your organization"
|
||||
placeholder="Enter email address(es)"
|
||||
validityRegex={emailRegex}
|
||||
validityMessage="Invalid email address"
|
||||
id="members" />
|
||||
<SelectPaymentMethod bind:methods bind:value={paymentMethodId} bind:taxId />
|
||||
</FormList>
|
||||
</Form>
|
||||
{/if}
|
||||
<svelte:fragment slot="aside">
|
||||
<div class="card">
|
||||
<img src="/images/3D-card-illust-light.png" alt="" />
|
||||
</div>
|
||||
<EstimatedTotalBox
|
||||
bind:billingPlan={selectedOrg.billingPlan}
|
||||
{collaborators}
|
||||
bind:couponData={data.couponData}
|
||||
bind:billingBudget />
|
||||
</svelte:fragment>
|
||||
</WizardSecondaryContent>
|
||||
|
||||
<WizardSecondaryFooter>
|
||||
<Button fullWidthMobile href={`${base}/console`} secondary>Cancel</Button>
|
||||
<Button
|
||||
fullWidthMobile
|
||||
on:click={() => formComponent.triggerSubmit()}
|
||||
disabled={$isSubmitting || isButtonDisabled}>
|
||||
Apply credits
|
||||
</Button>
|
||||
</WizardSecondaryFooter>
|
||||
</WizardSecondaryContainer>
|
||||
@@ -0,0 +1,23 @@
|
||||
import { base } from '$app/paths';
|
||||
import type { Coupon } from '$lib/sdk/billing.js';
|
||||
import { sdk } from '$lib/stores/sdk.js';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load = async ({ url }) => {
|
||||
let couponData: Coupon;
|
||||
if (url.searchParams.has('code')) {
|
||||
const code = url.searchParams.get('code');
|
||||
try {
|
||||
couponData = await sdk.forConsole.billing.getCoupon(code);
|
||||
} catch (e) {
|
||||
redirect(303, `${base}/console`);
|
||||
}
|
||||
}
|
||||
if (!couponData?.campaign) {
|
||||
redirect(303, `${base}/console`);
|
||||
}
|
||||
|
||||
return {
|
||||
couponData
|
||||
};
|
||||
};
|
||||
@@ -73,8 +73,8 @@
|
||||
let feedbackMessage: string;
|
||||
|
||||
onMount(async () => {
|
||||
if ($page.url.searchParams.has('coupon')) {
|
||||
const coupon = $page.url.searchParams.get('coupon');
|
||||
if ($page.url.searchParams.has('code')) {
|
||||
const coupon = $page.url.searchParams.get('code');
|
||||
try {
|
||||
const response = await sdk.forConsole.billing.getCoupon(coupon);
|
||||
couponData = response;
|
||||
@@ -360,9 +360,6 @@
|
||||
{/if}
|
||||
</Form>
|
||||
<svelte:fragment slot="aside">
|
||||
<div>
|
||||
<img src="/images/3D-card-illust-light.png" alt="" />
|
||||
</div>
|
||||
{#if billingPlan !== BillingPlan.STARTER && $organization.billingPlan !== BillingPlan.PRO}
|
||||
<EstimatedTotalBox
|
||||
{billingPlan}
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
}
|
||||
|
||||
await invalidate(Dependencies.ACCOUNT);
|
||||
trackEvent(Submit.AccountCreate);
|
||||
trackEvent(Submit.AccountCreate, { campaign_name: data?.couponData?.code });
|
||||
|
||||
if ($page.url.searchParams) {
|
||||
const redirect = $page.url.searchParams.get('redirect');
|
||||
|
||||
Reference in new issue
Block a user