-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMembershipPurchaseButton.tsx
More file actions
63 lines (59 loc) · 2 KB
/
MembershipPurchaseButton.tsx
File metadata and controls
63 lines (59 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ResponseError } from '@acm-uiuc/core-client';
import { ShoppingCart } from 'lucide-react';
import { useState } from 'react';
import { membershipApiClient } from '../api/index.js';
import AuthActionButton from './AuthActionButton.tsx';
import MembershipStatusPopup, {
type MembershipStatus,
} from './MembershipStatusPopup.tsx';
interface Props {
class?: string;
}
export default function MembershipPurchaseButton({ class: className }: Props) {
const [status, setStatus] = useState<MembershipStatus | null>(null);
return (
<>
<MembershipStatusPopup status={status} onClose={() => setStatus(null)} />
<AuthActionButton
class={className}
icon={ShoppingCart}
defaultText="Purchase Membership"
workingText="Loading..."
returnPath="/membership?authButtonClick"
onAction={async (accessToken, showError) => {
try {
const checkoutUrl =
await membershipApiClient.apiV2MembershipCheckoutGet({
xUiucToken: accessToken,
});
window.location.href = checkoutUrl;
} catch (e) {
if (e instanceof ResponseError && e.response) {
const body = (await e.response.json()) as {
message?: string;
id?: number;
};
if (body.message?.includes('is already a paid member')) {
const memberInfo = await membershipApiClient.apiV1MembershipGet(
{ xUiucToken: accessToken }
);
setStatus({ ...memberInfo, accessToken });
} else {
showError(
body.id || 500,
body.message ||
'An error occurred creating your checkout session.'
);
}
} else {
showError(
500,
'An error occurred creating your checkout session.'
);
}
}
}}
/>
</>
);
}