|
| 1 | +import type { __experimental_CommercePlanResource } from '@clerk/types'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { Alert, Box, Button, Col, Flex, Heading, Text } from '../../customizables'; |
| 5 | +import { Drawer } from '../../elements'; |
| 6 | +import type { PlanPeriod } from './PlanCard'; |
| 7 | +import { PlanCardFeaturesList, PlanCardHeader } from './PlanCard'; |
| 8 | + |
| 9 | +type DrawerRootProps = React.ComponentProps<typeof Drawer.Root>; |
| 10 | + |
| 11 | +type PlanDetailDrawerProps = { |
| 12 | + isOpen: DrawerRootProps['open']; |
| 13 | + setIsOpen: DrawerRootProps['onOpenChange']; |
| 14 | + portalProps?: DrawerRootProps['portalProps']; |
| 15 | + strategy: DrawerRootProps['strategy']; |
| 16 | + plan?: __experimental_CommercePlanResource; |
| 17 | + planPeriod: PlanPeriod; |
| 18 | + setPlanPeriod: (p: PlanPeriod) => void; |
| 19 | +}; |
| 20 | + |
| 21 | +export function PlanDetailDrawer({ |
| 22 | + isOpen, |
| 23 | + setIsOpen, |
| 24 | + portalProps, |
| 25 | + strategy, |
| 26 | + plan, |
| 27 | + planPeriod, |
| 28 | + setPlanPeriod, |
| 29 | +}: PlanDetailDrawerProps) { |
| 30 | + if (!plan) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + const hasFeatures = plan.features.length > 0; |
| 34 | + return ( |
| 35 | + <Drawer.Root |
| 36 | + open={isOpen} |
| 37 | + onOpenChange={setIsOpen} |
| 38 | + strategy={strategy} |
| 39 | + portalProps={portalProps} |
| 40 | + > |
| 41 | + <Drawer.Overlay /> |
| 42 | + <Drawer.Content> |
| 43 | + <Drawer.Header |
| 44 | + sx={t => |
| 45 | + !hasFeatures |
| 46 | + ? { |
| 47 | + flex: 1, |
| 48 | + borderBottomWidth: 0, |
| 49 | + background: t.colors.$colorBackground, |
| 50 | + } |
| 51 | + : null |
| 52 | + } |
| 53 | + > |
| 54 | + <PlanCardHeader |
| 55 | + plan={plan} |
| 56 | + planPeriod={planPeriod} |
| 57 | + setPlanPeriod={setPlanPeriod} |
| 58 | + closeSlot={<Drawer.Close />} |
| 59 | + /> |
| 60 | + </Drawer.Header> |
| 61 | + {hasFeatures ? ( |
| 62 | + <Drawer.Body> |
| 63 | + <Box |
| 64 | + sx={t => ({ |
| 65 | + padding: t.space.$4, |
| 66 | + })} |
| 67 | + > |
| 68 | + <PlanCardFeaturesList plan={plan} /> |
| 69 | + </Box> |
| 70 | + </Drawer.Body> |
| 71 | + ) : null} |
| 72 | + <CancelFooter |
| 73 | + plan={plan} |
| 74 | + handleClose={() => setIsOpen(false)} |
| 75 | + /> |
| 76 | + </Drawer.Content> |
| 77 | + </Drawer.Root> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +const CancelFooter = ({ plan }: { plan: __experimental_CommercePlanResource; handleClose: () => void }) => { |
| 82 | + // const { __experimental_commerce } = useClerk(); |
| 83 | + const [showConfirmation, setShowConfirmation] = React.useState(false); |
| 84 | + const [isSubmitting, setIsSubmitting] = React.useState(false); |
| 85 | + const [hasError, setHasError] = React.useState(false); |
| 86 | + |
| 87 | + const cancelSubscription = async () => { |
| 88 | + setHasError(false); |
| 89 | + setIsSubmitting(true); |
| 90 | + |
| 91 | + // TODO: we need to get a handle on the subscription object in order to cancel it, |
| 92 | + // but this method doesn't exist yet. |
| 93 | + // |
| 94 | + // await subscription.cancel().then(() => { |
| 95 | + // setIsSubmitting(false); |
| 96 | + // handleClose(); |
| 97 | + // }).catch(() => { setHasError(true); setIsSubmitting(false); }); |
| 98 | + }; |
| 99 | + |
| 100 | + // TODO: remove when we can hook up cancel button |
| 101 | + // return null; |
| 102 | + |
| 103 | + return ( |
| 104 | + <Drawer.Footer> |
| 105 | + {showConfirmation ? ( |
| 106 | + <Col gap={8}> |
| 107 | + <Heading textVariant='h3'>Cancel {plan.name} Subscription?</Heading> |
| 108 | + <Text colorScheme='secondary'> |
| 109 | + You can keep using “{plan.name}” features until [DATE], after which you will no longer have |
| 110 | + access. |
| 111 | + </Text> |
| 112 | + {hasError && ( |
| 113 | + <Alert colorScheme='danger'>There was a problem canceling your subscription, please try again.</Alert> |
| 114 | + )} |
| 115 | + <Flex |
| 116 | + gap={3} |
| 117 | + justify='end' |
| 118 | + > |
| 119 | + {!isSubmitting && ( |
| 120 | + <Button |
| 121 | + variant='ghost' |
| 122 | + size='sm' |
| 123 | + textVariant='buttonLarge' |
| 124 | + onClick={() => { |
| 125 | + setHasError(false); |
| 126 | + setShowConfirmation(false); |
| 127 | + }} |
| 128 | + > |
| 129 | + Keep Subscription |
| 130 | + </Button> |
| 131 | + )} |
| 132 | + <Button |
| 133 | + variant='solid' |
| 134 | + colorScheme='danger' |
| 135 | + size='sm' |
| 136 | + textVariant='buttonLarge' |
| 137 | + isLoading={isSubmitting} |
| 138 | + onClick={cancelSubscription} |
| 139 | + > |
| 140 | + Cancel Subscription |
| 141 | + </Button> |
| 142 | + </Flex> |
| 143 | + </Col> |
| 144 | + ) : ( |
| 145 | + <Button |
| 146 | + variant='bordered' |
| 147 | + colorScheme='secondary' |
| 148 | + size='sm' |
| 149 | + textVariant='buttonLarge' |
| 150 | + sx={{ |
| 151 | + width: '100%', |
| 152 | + }} |
| 153 | + onClick={() => setShowConfirmation(true)} |
| 154 | + > |
| 155 | + Cancel Subscription |
| 156 | + </Button> |
| 157 | + )} |
| 158 | + </Drawer.Footer> |
| 159 | + ); |
| 160 | +}; |
0 commit comments