|
| 1 | +--- |
| 2 | +import type { __experimental_SubscriptionDetailsButtonProps } from '@clerk/types'; |
| 3 | +
|
| 4 | +import type { HTMLTag, Polymorphic } from 'astro/types'; |
| 5 | +import type { ButtonProps } from '../../types'; |
| 6 | +import { addUnstyledAttributeToFirstTag, logAsPropUsageDeprecation } from './utils'; |
| 7 | +
|
| 8 | +type Props<Tag extends HTMLTag = 'button'> = Polymorphic<ButtonProps<Tag>> & |
| 9 | + Omit<__experimental_SubscriptionDetailsButtonProps, 'onSubscriptionCancel'> & { clickIdentifier?: string }; |
| 10 | +
|
| 11 | +import { generateSafeId } from '@clerk/astro/internal'; |
| 12 | +
|
| 13 | +const safeId = generateSafeId(); |
| 14 | +
|
| 15 | +if ('as' in Astro.props) { |
| 16 | + logAsPropUsageDeprecation(); |
| 17 | +} |
| 18 | +
|
| 19 | +const { as: Tag = 'button', asChild, for: _for, clickIdentifier, subscriptionDetailsProps, ...props } = Astro.props; |
| 20 | +
|
| 21 | +const subscriptionDetailsOptions = { |
| 22 | + for: _for, |
| 23 | + clickIdentifier, |
| 24 | + ...subscriptionDetailsProps, |
| 25 | +}; |
| 26 | +
|
| 27 | +let htmlElement = ''; |
| 28 | +
|
| 29 | +if (asChild) { |
| 30 | + htmlElement = await Astro.slots.render('default'); |
| 31 | + htmlElement = addUnstyledAttributeToFirstTag(htmlElement, safeId); |
| 32 | +} |
| 33 | +--- |
| 34 | + |
| 35 | +{ |
| 36 | + asChild ? ( |
| 37 | + <Fragment set:html={htmlElement} /> |
| 38 | + ) : ( |
| 39 | + <Tag |
| 40 | + {...props} |
| 41 | + data-clerk-unstyled-id={safeId} |
| 42 | + > |
| 43 | + <slot>Subscription details</slot> |
| 44 | + </Tag> |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +<script is:inline define:vars={{ props, subscriptionDetailsOptions, safeId }}> |
| 49 | + const btn = document.querySelector(`[data-clerk-unstyled-id="${safeId}"]`); |
| 50 | + |
| 51 | + btn.addEventListener('click', () => { |
| 52 | + const clerk = window.Clerk; |
| 53 | + |
| 54 | + // Authentication checks |
| 55 | + if (!clerk.user) { |
| 56 | + throw new Error('Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.'); |
| 57 | + } |
| 58 | + |
| 59 | + if (!clerk.organization && subscriptionDetailsOptions.for === 'organization') { |
| 60 | + throw new Error( |
| 61 | + 'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization.', |
| 62 | + ); |
| 63 | + } |
| 64 | + if (subscriptionDetailsOptions.clickIdentifier) { |
| 65 | + const clickEvent = new CustomEvent('clerk:subscription-cancel', { |
| 66 | + detail: subscriptionDetailsOptions.clickIdentifier, |
| 67 | + }); |
| 68 | + subscriptionDetailsOptions.onSubscriptionCancel = () => { |
| 69 | + document.dispatchEvent(clickEvent); |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + return clerk.__internal_openSubscriptionDetails(subscriptionDetailsOptions); |
| 74 | + }); |
| 75 | +</script> |
0 commit comments