11import { loadScript } from '@paypal/paypal-js'
22import { prod } from '../Config'
3- import { changeSubTab , Tabs } from '../Tabs'
43import { Alert , Notification } from '../UpdateHTML'
54import { memoize } from '../Utility'
65import { products , subscriptionProducts } from './CartTab'
@@ -10,28 +9,17 @@ import { updatePseudoCoins } from './UpgradesSubtab'
109const tab = document . querySelector < HTMLElement > ( '#pseudoCoins > #cartContainer' ) !
1110const form = tab . querySelector ( 'div.cartList' ) !
1211
13- const checkout = form . querySelector ( 'button#checkout' )
14- const closeCart = form . querySelector ( 'button#closeCart' )
12+ const checkout = form . querySelector < HTMLElement > ( 'button#checkout' )
1513const radioTOSAgree = form . querySelector < HTMLInputElement > ( 'section > input[type="radio"]' ) !
1614const totalCost = form . querySelector ( 'p#totalCost' )
1715const itemList = form . querySelector ( '#itemList' ) !
1816
19- let tosAgreed = false
2017const formatter = Intl . NumberFormat ( 'en-US' , {
2118 style : 'currency' ,
2219 currency : 'USD'
2320} )
2421
2522export const initializeCheckoutTab = memoize ( ( ) => {
26- closeCart ?. addEventListener ( 'click' , ( ) => {
27- changeSubTab ( Tabs . Purchase , { page : 0 } )
28- } )
29-
30- radioTOSAgree . addEventListener ( 'click' , ( ) => {
31- tosAgreed = ! tosAgreed
32- radioTOSAgree . checked = tosAgreed
33- } )
34-
3523 itemList . insertAdjacentHTML (
3624 'afterend' ,
3725 products . map ( ( product ) => ( `
@@ -46,8 +34,8 @@ export const initializeCheckoutTab = memoize(() => {
4634 ` ) ) . join ( '' )
4735 )
4836
49- checkout ?. addEventListener ( 'click' , ( e ) => {
50- if ( ! tosAgreed ) {
37+ function submitCheckout ( e : MouseEvent ) {
38+ if ( ! radioTOSAgree . checked ) {
5139 e . preventDefault ( )
5240 Notification ( 'You must accept the terms of service first!' )
5341 return
@@ -59,13 +47,22 @@ export const initializeCheckoutTab = memoize(() => {
5947 fd . set ( product . id , `${ product . quantity } ` )
6048 }
6149
62- fd . set ( 'tosAgree' , tosAgreed ? 'on' : 'off' )
50+ fd . set ( 'tosAgree' , radioTOSAgree . checked ? 'on' : 'off' )
51+
52+ checkout ?. setAttribute ( 'disabled' , '' )
6353
64- checkout . setAttribute ( 'disabled' , '' )
54+ let url : string
6555
66- const url = ! prod
67- ? 'https://synergism.cc/stripe/test/create-checkout-session'
68- : 'https://synergism.cc/stripe/create-checkout-session'
56+ const clickedId = ( e . target as HTMLElement ) . id
57+
58+ if ( clickedId === 'checkout' ) {
59+ url = ! prod
60+ ? 'https://synergism.cc/stripe/test/create-checkout-session'
61+ : 'https://synergism.cc/stripe/create-checkout-session'
62+ } else {
63+ Notification ( `You clicked on ${ clickedId } .` )
64+ return
65+ }
6966
7067 fetch ( url , {
7168 method : 'POST' ,
@@ -79,9 +76,11 @@ export const initializeCheckoutTab = memoize(() => {
7976 }
8077 } )
8178 . finally ( ( ) => {
82- checkout . removeAttribute ( 'disabled' )
79+ checkout ? .removeAttribute ( 'disabled' )
8380 } )
84- } )
81+ }
82+
83+ checkout ?. addEventListener ( 'click' , submitCheckout )
8584
8685 initializePayPal ( '#checkout-paypal' )
8786} )
@@ -188,7 +187,7 @@ async function initializePayPal (selector: string | HTMLElement) {
188187 fd . set ( product . id , `${ product . quantity } ` )
189188 }
190189
191- fd . set ( 'tosAgree' , tosAgreed ? 'on' : 'off' )
190+ fd . set ( 'tosAgree' , radioTOSAgree . checked ? 'on' : 'off' )
192191 const url = 'https://synergism.cc/paypal/orders/create'
193192
194193 const response = await fetch ( url , {
0 commit comments