1
- import React , { useState } from "react" ;
1
+ import React , { useEffect , useState } from "react" ;
2
2
import * as AppGeneral from "../socialcalc/index.js" ;
3
3
import { File , Local } from "../Storage/LocalStorage" ;
4
4
import { isPlatform , IonToast } from "@ionic/react" ;
@@ -8,6 +8,13 @@ import { IonActionSheet, IonAlert } from "@ionic/react";
8
8
import { saveOutline , save , mail , print } from "ionicons/icons" ;
9
9
import { APP_NAME } from "../../app-data.js" ;
10
10
11
+ import {
12
+ canUserPerformAction ,
13
+ updateUserQuota ,
14
+ updateUserSubscription ,
15
+ } from "../../firebase/firestore.js" ;
16
+ import useUser from "../../hooks/useUser.js" ;
17
+
11
18
const Menu : React . FC < {
12
19
showM : boolean ;
13
20
setM : Function ;
@@ -22,6 +29,28 @@ const Menu: React.FC<{
22
29
const [ showAlert4 , setShowAlert4 ] = useState ( false ) ;
23
30
const [ showToast1 , setShowToast1 ] = useState ( false ) ;
24
31
const [ toastMessage , setToastMessage ] = useState ( "" ) ;
32
+ const [ canPrint , setCanPrint ] = useState ( false ) ;
33
+ const [ canEmail , setCanEmail ] = useState ( false ) ;
34
+ const { user, isLoading } = useUser ( ) ;
35
+ const [ showUpgradeAlert , setShowUpgradeAlert ] = useState ( false ) ;
36
+
37
+ const handleUpgradeSubscription = async ( tier : string ) => {
38
+ if ( user ) {
39
+ try {
40
+ await updateUserSubscription ( user . uid , tier ) ;
41
+ setToastMessage ( `Successfully upgraded to ${ tier } tier!` ) ;
42
+ setShowToast1 ( true ) ;
43
+ checkUserPermissions ( ) ;
44
+ } catch ( error ) {
45
+ console . error ( "Error upgrading subscription:" , error ) ;
46
+ setToastMessage ( "Failed to upgrade subscription. Please try again." ) ;
47
+ setShowToast1 ( true ) ;
48
+ }
49
+ } else {
50
+ setToastMessage ( "Please log in to upgrade your subscription." ) ;
51
+ setShowToast1 ( true ) ;
52
+ }
53
+ } ;
25
54
/* Utility functions */
26
55
const _validateName = async ( filename ) => {
27
56
filename = filename . trim ( ) ;
@@ -55,19 +84,52 @@ const Menu: React.FC<{
55
84
}
56
85
return filename ;
57
86
} ;
87
+ useEffect ( ( ) => {
88
+ checkUserPermissions ( ) ;
89
+ } , [ isLoading ] ) ;
90
+
91
+ const checkUserPermissions = async ( ) => {
92
+ if ( user ) {
93
+ const printPermission = await canUserPerformAction ( user . uid , "print" ) ;
94
+ const emailPermission = await canUserPerformAction ( user . uid , "email" ) ;
95
+ setCanPrint ( printPermission ) ;
96
+ setCanEmail ( emailPermission ) ;
97
+ }
98
+ } ;
99
+
100
+ const doPrint = async ( ) => {
101
+ if ( ! canPrint ) {
102
+ setToastMessage ( "You've reached your print quota limit." ) ;
103
+ setShowToast1 ( true ) ;
104
+ return ;
105
+ }
106
+
107
+ if ( user ) {
108
+ const updated = await updateUserQuota ( user . uid , "print" ) ;
109
+ if ( ! updated ) {
110
+ setToastMessage (
111
+ "Failed to update quota. You may have reached your limit."
112
+ ) ;
113
+ setShowToast1 ( true ) ;
114
+ return ;
115
+ }
116
+ }
58
117
59
- const doPrint = ( ) => {
60
118
if ( isPlatform ( "hybrid" ) ) {
61
119
const printer = Printer ;
62
120
printer . print ( AppGeneral . getCurrentHTMLContent ( ) ) ;
63
121
} else {
64
122
const content = AppGeneral . getCurrentHTMLContent ( ) ;
65
- // useReactToPrint({ content: () => content });
66
123
const printWindow = window . open ( "/printwindow" , "Print Invoice" ) ;
67
124
printWindow . document . write ( content ) ;
68
125
printWindow . print ( ) ;
69
126
}
127
+
128
+ setToastMessage ( "Print job sent successfully." ) ;
129
+ setShowToast1 ( true ) ;
130
+ checkUserPermissions ( ) ; // Update permissions after printing
70
131
} ;
132
+
71
133
const doSave = ( ) => {
72
134
if ( props . file === "default" ) {
73
135
setShowAlert1 ( true ) ;
@@ -113,7 +175,24 @@ const Menu: React.FC<{
113
175
}
114
176
} ;
115
177
116
- const sendEmail = ( ) => {
178
+ const sendEmail = async ( ) => {
179
+ if ( ! canEmail ) {
180
+ setToastMessage ( "You've reached your email quota limit." ) ;
181
+ setShowToast1 ( true ) ;
182
+ return ;
183
+ }
184
+
185
+ if ( user ) {
186
+ const updated = await updateUserQuota ( user . uid , "email" ) ;
187
+ if ( ! updated ) {
188
+ setToastMessage (
189
+ "Failed to update quota. You may have reached your limit."
190
+ ) ;
191
+ setShowToast1 ( true ) ;
192
+ return ;
193
+ }
194
+ }
195
+
117
196
if ( isPlatform ( "hybrid" ) ) {
118
197
const content = AppGeneral . getCurrentHTMLContent ( ) ;
119
198
const base64 = btoa ( content ) ;
@@ -127,9 +206,15 @@ const Menu: React.FC<{
127
206
subject : `${ APP_NAME } attached` ,
128
207
isHtml : true ,
129
208
} ) ;
209
+
210
+ setToastMessage ( "Email sent successfully." ) ;
211
+ setShowToast1 ( true ) ;
130
212
} else {
131
- alert ( "This Functionality works on Anroid/IOS devices" ) ;
213
+ setToastMessage ( "This functionality works on Android/iOS devices only." ) ;
214
+ setShowToast1 ( true ) ;
132
215
}
216
+
217
+ checkUserPermissions ( ) ;
133
218
} ;
134
219
135
220
return (
@@ -172,6 +257,14 @@ const Menu: React.FC<{
172
257
console . log ( "Email clicked" ) ;
173
258
} ,
174
259
} ,
260
+ {
261
+ text : "Upgrade Subscription" ,
262
+ icon : "arrow-up-circle-outline" ,
263
+ handler : ( ) => {
264
+ setShowUpgradeAlert ( true ) ;
265
+ console . log ( "Upgrade Subscription clicked" ) ;
266
+ } ,
267
+ } ,
175
268
] }
176
269
/>
177
270
< IonAlert
@@ -225,6 +318,31 @@ const Menu: React.FC<{
225
318
}
226
319
buttons = { [ "Ok" ] }
227
320
/>
321
+ < IonAlert
322
+ animated
323
+ isOpen = { showUpgradeAlert }
324
+ onDidDismiss = { ( ) => setShowUpgradeAlert ( false ) }
325
+ header = "Upgrade Subscription"
326
+ message = "Choose a subscription tier to upgrade:"
327
+ buttons = { [
328
+ {
329
+ text : "Bronze ($150/month)" ,
330
+ handler : ( ) => handleUpgradeSubscription ( "bronze" ) ,
331
+ } ,
332
+ {
333
+ text : "Silver ($200/month)" ,
334
+ handler : ( ) => handleUpgradeSubscription ( "silver" ) ,
335
+ } ,
336
+ {
337
+ text : "Gold ($250/month)" ,
338
+ handler : ( ) => handleUpgradeSubscription ( "gold" ) ,
339
+ } ,
340
+ {
341
+ text : "Cancel" ,
342
+ role : "cancel" ,
343
+ } ,
344
+ ] }
345
+ />
228
346
< IonToast
229
347
animated
230
348
isOpen = { showToast1 }
0 commit comments