|
1 | 1 | <script lang="ts"> |
2 | 2 | import { FakeModal } from '$lib/components'; |
3 | 3 | import { InputText, Button } from '$lib/elements/forms'; |
4 | | - import { createEventDispatcher, onMount } from 'svelte'; |
5 | | - import { initializeStripe, setPaymentMethod, submitStripeCard } from '$lib/stores/stripe'; |
| 4 | + import { onMount, onDestroy } from 'svelte'; |
| 5 | + import { |
| 6 | + initializeStripe, |
| 7 | + setPaymentMethod, |
| 8 | + submitStripeCard, |
| 9 | + unmountPaymentElement |
| 10 | + } from '$lib/stores/stripe'; |
6 | 11 | import { invalidate } from '$app/navigation'; |
7 | 12 | import { Dependencies } from '$lib/constants'; |
8 | 13 | import { addNotification } from '$lib/stores/notifications'; |
9 | 14 | import { page } from '$app/state'; |
10 | 15 | import { Spinner } from '@appwrite.io/pink-svelte'; |
11 | 16 | import type { PaymentMethod } from '@stripe/stripe-js'; |
12 | 17 | import StatePicker from './statePicker.svelte'; |
| 18 | + import type { PaymentMethodData } from '$lib/sdk/billing'; |
13 | 19 |
|
14 | 20 | export let show = false; |
| 21 | + export let onCardSubmit: ((card: PaymentMethodData) => void) | null = null; |
15 | 22 |
|
16 | | - const dispatch = createEventDispatcher(); |
17 | | -
|
18 | | - let name: string; |
19 | | - let error: string; |
20 | 23 | let modal: FakeModal; |
21 | | - let showState: boolean = false; |
| 24 | + let name: string; |
22 | 25 | let state: string = ''; |
| 26 | + let error: string = null; |
| 27 | + let showState: boolean = false; |
23 | 28 | let paymentMethod: PaymentMethod | null = null; |
24 | 29 |
|
25 | 30 | async function handleSubmit() { |
|
32 | 37 | const card = await setPaymentMethod(paymentMethod.id, name, state); |
33 | 38 | modal.closeModal(); |
34 | 39 | await invalidate(Dependencies.PAYMENT_METHODS); |
35 | | - dispatch('submit', card); |
| 40 | + onCardSubmit?.(card); |
36 | 41 | addNotification({ |
37 | 42 | type: 'success', |
38 | 43 | message: 'A new payment method has been added to your account' |
|
50 | 55 | } |
51 | 56 | modal.closeModal(); |
52 | 57 | await invalidate(Dependencies.PAYMENT_METHODS); |
53 | | - dispatch('submit', card); |
| 58 | + onCardSubmit?.(card as PaymentMethodData); |
54 | 59 | addNotification({ |
55 | 60 | type: 'success', |
56 | 61 | message: 'A new payment method has been added to your account' |
|
89 | 94 | }; |
90 | 95 | }); |
91 | 96 |
|
| 97 | + onDestroy(unmountPaymentElement); |
| 98 | +
|
92 | 99 | $: if (element) { |
93 | 100 | observer.observe(element, { childList: true }); |
94 | 101 | } |
|
99 | 106 | bind:show |
100 | 107 | title="Add payment method" |
101 | 108 | bind:error |
102 | | - onSubmit={handleSubmit}> |
| 109 | + onSubmit={handleSubmit} |
| 110 | + skipEnterOnBackdrop={showState}> |
103 | 111 | <slot /> |
104 | 112 | {#if showState} |
105 | 113 | <StatePicker card={paymentMethod} bind:state /> |
|
0 commit comments