mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
feat: refactor coupons
This commit is contained in:
1 parent
97fe8c56b3
commit
8bd7c2753e
3 files changed
+78
-64
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<script lang="ts">
|
||||
import { Button, FormList, InputText } from '$lib/elements/forms';
|
||||
import type { Coupon } from '$lib/sdk/billing';
|
||||
import { sdk } from '$lib/stores/sdk';
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
export let coupon: string;
|
||||
export let couponData: Partial<Coupon> = {
|
||||
code: null,
|
||||
status: null,
|
||||
credits: null
|
||||
};
|
||||
|
||||
async function addCoupon() {
|
||||
try {
|
||||
const response = await sdk.forConsole.billing.getCoupon(coupon);
|
||||
coupon = null;
|
||||
couponData = response;
|
||||
dispatch('validation', response);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
couponData.code = coupon;
|
||||
couponData.status = 'error';
|
||||
}
|
||||
}
|
||||
|
||||
function removeCoupon() {
|
||||
couponData = {
|
||||
code: null,
|
||||
status: null,
|
||||
credits: null
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
<FormList gap={8}>
|
||||
<InputText
|
||||
placeholder="Coupon code"
|
||||
id="code"
|
||||
label="Add credits"
|
||||
disabled={couponData?.status === 'active'}
|
||||
bind:value={coupon}>
|
||||
<Button
|
||||
secondary
|
||||
disabled={couponData?.status === 'active' || !coupon}
|
||||
on:click={addCoupon}>
|
||||
Apply
|
||||
</Button>
|
||||
</InputText>
|
||||
{#if couponData?.status === 'error'}
|
||||
<div>
|
||||
<span class="icon-exclamation-circle u-color-text-danger" />
|
||||
<span>
|
||||
{couponData.code} is not a valid promo code
|
||||
</span>
|
||||
</div>
|
||||
{:else if couponData?.status === 'active'}
|
||||
<div class="u-flex u-main-space-between u-cross-center">
|
||||
<div>
|
||||
<span class="icon-tag u-color-text-success" />
|
||||
<span>
|
||||
{couponData.code} applied (-${couponData.credits})
|
||||
</span>
|
||||
</div>
|
||||
<Button text on:click={removeCoupon}>Remove</Button>
|
||||
</div>
|
||||
{/if}
|
||||
</FormList>
|
||||
@@ -1 +1,2 @@
|
||||
export { default as PaymentBoxes } from './paymentBoxes.svelte';
|
||||
export { default as CouponInput} from './couponInput.svelte';
|
||||
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import { Box, CreditCardBrandImage } from '$lib/components';
|
||||
import { CouponInput } from '$lib/components/billing';
|
||||
import { Pill } from '$lib/elements';
|
||||
import { Button, FormList, InputText, InputTextarea } from '$lib/elements/forms';
|
||||
import { FormList, InputTextarea } from '$lib/elements/forms';
|
||||
import { toLocaleDate } from '$lib/helpers/date';
|
||||
import { WizardStep } from '$lib/layout';
|
||||
import type { Coupon } from '$lib/sdk/billing';
|
||||
@@ -21,13 +22,13 @@
|
||||
: $organization.billingCurrentInvoiceDate;
|
||||
|
||||
let coupon: string = null;
|
||||
let comment: string = null;
|
||||
let couponData: Partial<Coupon> = {
|
||||
code: null,
|
||||
status: null,
|
||||
credits: null
|
||||
};
|
||||
let showCoupon: boolean = false;
|
||||
let comment: string = null;
|
||||
|
||||
async function fetchCard() {
|
||||
try {
|
||||
const card = await sdk.forConsole.billing.getPaymentMethod(
|
||||
@@ -39,30 +40,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function addCoupon() {
|
||||
try {
|
||||
const response = await sdk.forConsole.billing.getCoupon(coupon);
|
||||
coupon = null;
|
||||
couponData = response;
|
||||
showCoupon = true;
|
||||
$changeOrganizationTier.couponCode = response.$id;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
couponData.code = coupon;
|
||||
couponData.status = 'error';
|
||||
showCoupon = true;
|
||||
}
|
||||
}
|
||||
|
||||
function removeCoupon() {
|
||||
couponData = {
|
||||
code: null,
|
||||
status: null,
|
||||
credits: null
|
||||
};
|
||||
showCoupon = false;
|
||||
}
|
||||
|
||||
$: downgradeToStarter = $changeOrganizationTier.billingPlan === 'tier-0';
|
||||
$: if (!$isUpgrade) {
|
||||
$changeOrganizationFinalAction = 'Confirm plan change';
|
||||
@@ -123,41 +100,7 @@
|
||||
{/if}
|
||||
|
||||
<Box class="u-margin-block-start-32 u-flex u-flex-vertical u-gap-16" radius="small">
|
||||
<FormList gap={8}>
|
||||
<InputText
|
||||
placeholder="Coupon code"
|
||||
id="code"
|
||||
label="Add credits"
|
||||
disabled={showCoupon && couponData?.status !== 'error'}
|
||||
bind:value={coupon}>
|
||||
<Button
|
||||
secondary
|
||||
disabled={(showCoupon && couponData?.status !== 'error') || !coupon}
|
||||
on:click={addCoupon}>
|
||||
Apply
|
||||
</Button>
|
||||
</InputText>
|
||||
{#if showCoupon}
|
||||
{#if couponData?.status === 'error'}
|
||||
<div>
|
||||
<span class="icon-exclamation-circle u-color-text-danger" />
|
||||
<span>
|
||||
{couponData.code} is not a valid promo code
|
||||
</span>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="u-flex u-main-space-between u-cross-center">
|
||||
<div>
|
||||
<span class="icon-tag u-color-text-success" />
|
||||
<span>
|
||||
{couponData.code} applied (-${couponData.credits})
|
||||
</span>
|
||||
</div>
|
||||
<Button text on:click={removeCoupon}>Remove</Button>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</FormList>
|
||||
<CouponInput bind:coupon bind:couponData />
|
||||
{#if $changeOrganizationTier.billingPlan !== 'tier-0'}
|
||||
<span class="u-flex u-main-space-between">
|
||||
<p class="text">{plan.name} plan</p>
|
||||
@@ -167,7 +110,7 @@
|
||||
<p class="text">Additional members ({collaboratorsNumber})</p>
|
||||
<p class="text">${collaboratorPrice * collaboratorsNumber}</p>
|
||||
</span>
|
||||
{#if showCoupon && couponData?.status !== 'error'}
|
||||
{#if couponData?.status === 'active'}
|
||||
<span class="u-flex u-main-space-between">
|
||||
<p class="text">Credits applied ({couponData.credits})</p>
|
||||
<p class="text">-${couponData.credits}</p>
|
||||
@@ -178,7 +121,7 @@
|
||||
<span class="u-flex u-main-space-between">
|
||||
<p class="text">Estimated total (in USD)</p>
|
||||
<p class="text">
|
||||
${showCoupon && couponData?.status !== 'error'
|
||||
${couponData?.status === 'active'
|
||||
? totalExpences - couponData.credits || 0
|
||||
: totalExpences}
|
||||
</p>
|
||||
|
||||
Reference in new issue
Block a user