@@ -4,6 +4,8 @@ import './style.scss';
44import {
55 CreateSubscriptionV2Request ,
66 CreateSubscriptionV2Response ,
7+ CancelSubscriptionV2Request ,
8+ CancelSubscriptionV2Response ,
79 useTonConnectUI ,
810 useTonWallet
911} from '@tonconnect/ui-react' ;
@@ -36,6 +38,9 @@ export function SubscriptionForm() {
3638 const [ subscriptionRes , setSubscriptionRes ] = useState < CreateSubscriptionV2Response | null > (
3739 null
3840 ) ;
41+ const [ subscriptionError , setSubscriptionError ] = useState < string | null > ( null ) ;
42+ const [ cancelRes , setCancelRes ] = useState < CancelSubscriptionV2Response | null > ( null ) ;
43+ const [ cancelError , setCancelError ] = useState < string | null > ( null ) ;
3944
4045 const wallet = useTonWallet ( ) ;
4146 const [ tonConnectUi ] = useTonConnectUI ( ) ;
@@ -48,10 +53,45 @@ export function SubscriptionForm() {
4853 // setSubscription(template);
4954 // };
5055
51- const onSend = ( ) =>
56+ const onSend = ( ) => {
57+ setSubscriptionError ( null ) ;
5258 tonConnectUi
5359 . createSubscription ( subscription , { version : 'v2' } )
54- . then ( res => setSubscriptionRes ( res ) ) ;
60+ . then ( res => {
61+ setSubscriptionRes ( res ) ;
62+ setSubscriptionError ( null ) ;
63+ } )
64+ . catch ( err => {
65+ setSubscriptionError ( err instanceof Error ? err . message : String ( err ) ) ;
66+ setSubscriptionRes ( null ) ;
67+ } ) ;
68+ } ;
69+
70+ const onCancel = ( ) => {
71+ if ( ! subscriptionRes ?. boc ) {
72+ console . error ( 'No subscription response boc available' ) ;
73+ return ;
74+ }
75+
76+ const cancelRequest : CancelSubscriptionV2Request = {
77+ validUntil : Math . floor ( Date . now ( ) / 1000 ) + 600 , // 10 minutes from now
78+ extensionAddress : subscriptionRes . boc ,
79+ network : subscription . network ,
80+ from : subscription . from
81+ } ;
82+
83+ setCancelError ( null ) ;
84+ tonConnectUi
85+ . cancelSubscription ( cancelRequest , { version : 'v2' } )
86+ . then ( res => {
87+ setCancelRes ( res ) ;
88+ setCancelError ( null ) ;
89+ } )
90+ . catch ( err => {
91+ setCancelError ( err instanceof Error ? err . message : String ( err ) ) ;
92+ setCancelRes ( null ) ;
93+ } ) ;
94+ } ;
5595
5696 return (
5797 < div className = "create-subscription-form" >
@@ -77,16 +117,42 @@ export function SubscriptionForm() {
77117 onAdd = { onChange }
78118 onDelete = { onChange }
79119 />
120+ { subscriptionError && (
121+ < >
122+ < h4 style = { { color : 'red' } } > Create subscription error</ h4 >
123+ < div style = { { color : 'red' , padding : '10px' , border : '1px solid red' } } >
124+ { subscriptionError }
125+ </ div >
126+ </ >
127+ ) }
80128 { subscriptionRes && (
81129 < >
82- < h4 > Create subscription response</ h4 >
130+ < h4 style = { { color : 'green' } } > Create subscription response</ h4 >
83131 < ReactJson name = { false } src = { subscriptionRes } theme = "ocean" />
84132 </ >
85133 ) }
134+ { cancelError && (
135+ < >
136+ < h4 style = { { color : 'red' } } > Cancel subscription error</ h4 >
137+ < div style = { { color : 'red' , padding : '10px' , border : '1px solid red' } } >
138+ { cancelError }
139+ </ div >
140+ </ >
141+ ) }
142+ { cancelRes && (
143+ < >
144+ < h4 style = { { color : 'green' } } > Cancel subscription response</ h4 >
145+ < ReactJson name = { false } src = { cancelRes } theme = "ocean" />
146+ </ >
147+ ) }
86148 { wallet && (
87149 < div className = "buttons-container" >
88150 < button onClick = { onSend } > Create subscription</ button >
89- { subscriptionRes && < button > Cancel subscription</ button > }
151+ { subscriptionRes && (
152+ < button onClick = { onCancel } disabled = { ! subscriptionRes ?. boc } >
153+ Cancel subscription
154+ </ button >
155+ ) }
90156 </ div >
91157 ) }
92158 </ div >
0 commit comments