Skip to content

Commit 9f05f73

Browse files
committed
fix: enter on state picker.
1 parent c4d7d64 commit 9f05f73

7 files changed

Lines changed: 45 additions & 32 deletions

File tree

src/lib/components/billing/paymentModal.svelte

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { FakeModal } from '$lib/components';
33
import { InputText, Button } from '$lib/elements/forms';
4-
import { createEventDispatcher, onMount, onDestroy } from 'svelte';
4+
import { onMount, onDestroy } from 'svelte';
55
import {
66
initializeStripe,
77
setPaymentMethod,
@@ -15,16 +15,16 @@
1515
import { Spinner } from '@appwrite.io/pink-svelte';
1616
import type { PaymentMethod } from '@stripe/stripe-js';
1717
import StatePicker from './statePicker.svelte';
18+
import type { PaymentMethodData } from '$lib/sdk/billing';
1819
1920
export let show = false;
21+
export let onCardSubmit: ((card: PaymentMethodData) => void) | null = null;
2022
21-
const dispatch = createEventDispatcher();
22-
23-
let name: string;
24-
let error: string;
2523
let modal: FakeModal;
26-
let showState: boolean = false;
24+
let name: string;
2725
let state: string = '';
26+
let error: string = null;
27+
let showState: boolean = false;
2828
let paymentMethod: PaymentMethod | null = null;
2929
3030
async function handleSubmit() {
@@ -37,7 +37,7 @@
3737
const card = await setPaymentMethod(paymentMethod.id, name, state);
3838
modal.closeModal();
3939
await invalidate(Dependencies.PAYMENT_METHODS);
40-
dispatch('submit', card);
40+
onCardSubmit?.(card);
4141
addNotification({
4242
type: 'success',
4343
message: 'A new payment method has been added to your account'
@@ -55,7 +55,7 @@
5555
}
5656
modal.closeModal();
5757
await invalidate(Dependencies.PAYMENT_METHODS);
58-
dispatch('submit', card);
58+
onCardSubmit?.(card as PaymentMethodData);
5959
addNotification({
6060
type: 'success',
6161
message: 'A new payment method has been added to your account'
@@ -106,7 +106,8 @@
106106
bind:show
107107
title="Add payment method"
108108
bind:error
109-
onSubmit={handleSubmit}>
109+
onSubmit={handleSubmit}
110+
skipEnterOnBackdrop={showState}>
110111
<slot />
111112
{#if showState}
112113
<StatePicker card={paymentMethod} bind:state />

src/lib/components/billing/selectPaymentMethod.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
let showTaxId = false;
1919
let showPaymentModal = false;
2020
21-
async function cardSaved(event: CustomEvent<PaymentMethodData>) {
22-
value = event.detail.$id;
21+
async function cardSaved(card: PaymentMethodData) {
22+
value = card.$id;
2323
2424
if (value) {
2525
methods = {
2626
...methods,
2727
total: methods.total + 1,
28-
paymentMethods: [...methods.paymentMethods, event.detail]
28+
paymentMethods: [...methods.paymentMethods, card]
2929
};
3030
}
3131
@@ -98,7 +98,7 @@
9898
</Layout.Stack>
9999

100100
{#if showPaymentModal && isCloud && hasStripePublicKey}
101-
<PaymentModal bind:show={showPaymentModal} on:submit={cardSaved}>
101+
<PaymentModal bind:show={showPaymentModal} onCardSubmit={cardSaved}>
102102
<svelte:fragment slot="end">
103103
<Selector.Checkbox
104104
id="taxIdCheck"

src/lib/components/fakeModal.svelte

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script lang="ts">
22
import { Alert } from '@appwrite.io/pink-svelte';
3-
import { onMount } from 'svelte';
3+
import { clickOnEnter } from '$lib/helpers/a11y';
44
import Form from '$lib/elements/forms/form.svelte';
55
import { Click, trackEvent } from '$lib/actions/analytics';
6-
import { clickOnEnter } from '$lib/helpers/a11y';
76
7+
export let title = '';
88
export let show = false;
99
export let size: 'small' | 'big' = 'big';
1010
export let icon: string = null;
@@ -15,11 +15,13 @@
1515
export let onSubmit: (e: SubmitEvent) => Promise<void> | void = function () {
1616
return;
1717
};
18-
export let title = '';
1918
20-
let backdrop: HTMLDivElement;
19+
/**
20+
* needed when using `StatePicker`
21+
*/
22+
export let skipEnterOnBackdrop = false;
2123
22-
onMount(async () => {});
24+
let backdrop: HTMLDivElement;
2325
2426
function handleBLur(event: MouseEvent) {
2527
if (event.target === backdrop) {
@@ -60,12 +62,12 @@
6062
<svelte:window on:keydown={handleKeydown} />
6163

6264
{#if show}
63-
<!-- svelte-ignore a11y-no-static-element-interactions -->
65+
<!-- svelte-ignore a11y_no_static_element_interactions -->
6466
<div
67+
bind:this={backdrop}
68+
onclick={handleBLur}
6569
class="payment-modal-backdrop"
66-
on:keyup={clickOnEnter}
67-
on:click={handleBLur}
68-
bind:this={backdrop}>
70+
onkeyup={skipEnterOnBackdrop ? undefined : clickOnEnter}>
6971
<div
7072
class="modal"
7173
class:is-small={size === 'small'}
@@ -99,11 +101,12 @@
99101
style="--button-size:1.5rem;"
100102
aria-label="Close Modal"
101103
title="Close Modal"
102-
on:click={() =>
104+
onclick={() => {
105+
closeModal();
103106
trackEvent(Click.ModalCloseClick, {
104107
from: 'button'
105-
})}
106-
on:click={closeModal}>
108+
});
109+
}}>
107110
<span class="icon-x" aria-hidden="true"></span>
108111
</button>
109112
{/if}

src/lib/stores/stripe.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,20 @@ export async function initializeStripe(node: HTMLElement) {
5858

5959
export async function unmountPaymentElement() {
6060
isStripeInitialized.set(false);
61-
paymentElement?.unmount();
61+
62+
if (paymentElement) {
63+
try {
64+
paymentElement.unmount();
65+
paymentElement.destroy();
66+
} catch (e) {
67+
console.debug('Payment element cleanup:', e.message);
68+
}
69+
}
70+
71+
elements = null;
6272
clientSecret = null;
6373
paymentMethod = null;
64-
elements = null;
74+
paymentElement = null;
6575
}
6676

6777
export async function submitStripeCard(name: string, organizationId?: string) {

src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@
308308
{#if showPayment && isCloud && hasStripePublicKey}
309309
<PaymentModal
310310
bind:show={showPayment}
311-
on:submit={(e) => {
311+
onCardSubmit={(card) => {
312312
if (isSelectedBackup) {
313-
addBackupPaymentMethod(e.detail.$id);
313+
addBackupPaymentMethod(card.$id);
314314
} else {
315-
addPaymentMethod(e.detail.$id);
315+
addPaymentMethod(card.$id);
316316
}
317317
}} />
318318
{/if}

src/routes/(console)/organization-[organization]/billing/replaceCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
export let methods: PaymentList;
2020
2121
let name: string;
22-
let error: string;
22+
let error: string = null;
2323
let selectedPaymentMethodId: string;
2424
let showState: boolean = false;
2525
let state: string = '';

src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@
147147
onSubmit={handleSubmit}
148148
size="big"
149149
title="Retry payment">
150-
<!-- TODO: format currency -->
151150
<p class="text">
152151
Your payment of <span class="inline-tag">{formatCurrency(invoice.grossAmount)}</span> due on {toLocaleDate(
153152
invoice.dueAt

0 commit comments

Comments
 (0)