@@ -6,6 +6,7 @@ import useDocumentTitle from "../hooks/useDocumentTitle.ts";
6
6
import { Alert , Button , CircularProgress , Link , Typography } from "@mui/joy" ;
7
7
import { onAuthStateChanged , sendEmailVerification } from "firebase/auth" ;
8
8
import { auth } from "../resources/Firebase.js" ;
9
+ import Paths from '../resources/Paths.ts' ;
9
10
10
11
const str2xml = ( str : string ) => {
11
12
if ( str . charCodeAt ( 0 ) === 65279 ) {
@@ -78,11 +79,7 @@ const getParagraphs = async (file: File) => {
78
79
return paragraphs ;
79
80
} ;
80
81
81
- interface QuestionBankProps {
82
-
83
- }
84
-
85
- const QuestionBank : React . FC < QuestionBankProps > = ( { } ) => {
82
+ const QuestionBank : React . FC = ( { } ) => {
86
83
/* hooks */
87
84
useDocumentTitle ( 'My Answers' ) ;
88
85
@@ -157,6 +154,31 @@ const QuestionBank: React.FC<QuestionBankProps> = ({ }) => {
157
154
</ >
158
155
) ;
159
156
157
+ /* subscription */
158
+ const [ subscriptionChecked , setSubscriptionChecked ] = useState < boolean > ( false ) ;
159
+ const [ subscriptionExpiryDate , setSubscriptionExpiryDate ] = useState < Date > ( ) ;
160
+
161
+ const checkSubscription = useCallback ( async ( ) => {
162
+ try {
163
+ const response = await fetch ( Paths . Serverless + '?user-uid=' + auth . currentUser ?. uid ) ;
164
+ if ( response . status === 200 ) {
165
+ const timestamp = await response . text ( ) ;
166
+ const timestampInt = parseInt ( timestamp , 10 ) ;
167
+ setSubscriptionChecked ( true ) ;
168
+ setSubscriptionExpiryDate ( new Date ( timestampInt * 1000 ) ) ;
169
+ }
170
+ } catch ( error ) {
171
+ setSubscriptionChecked ( true ) ;
172
+ console . error ( error ) ;
173
+ }
174
+ } , [ setSubscriptionChecked , setSubscriptionExpiryDate ] ) ;
175
+
176
+ useEffect ( ( ) => {
177
+ checkSubscription ( ) ;
178
+ } , [ checkSubscription ] ) ;
179
+
180
+ const hasSubscriptionExpired = subscriptionExpiryDate && ( subscriptionExpiryDate < new Date ( Date . now ( ) ) ) ;
181
+
160
182
/* parse data */
161
183
const [ paragraphs , setParagraphs ] = useState < string [ ] > ( [ ] ) ;
162
184
@@ -178,8 +200,8 @@ const QuestionBank: React.FC<QuestionBankProps> = ({ }) => {
178
200
179
201
return (
180
202
< div >
181
- { auth . currentUser && ! emailVerified && (
182
- < Alert color = "warning" >
203
+ { ! emailVerified && (
204
+ < Alert color = 'success' >
183
205
< Typography
184
206
level = "body-sm"
185
207
sx = { { color : "inherit" , display : "flex" , gap : ".25em" , alignItems : "center" } }
@@ -189,10 +211,44 @@ const QuestionBank: React.FC<QuestionBankProps> = ({ }) => {
189
211
</ Typography >
190
212
</ Alert >
191
213
) }
192
- < Button > Purchase</ Button >
193
- { paragraphs . map ( ( paragraph , index ) => (
194
- < div key = { index } > { paragraph } </ div >
195
- ) ) }
214
+ { subscriptionExpiryDate && (
215
+ < >
216
+ < Alert color = { hasSubscriptionExpired ? 'danger' : 'success' } >
217
+ < Typography
218
+ level = "body-sm"
219
+ sx = { { color : "inherit" , display : "flex" , gap : ".25em" , alignItems : "center" } }
220
+ >
221
+ Your subscription { hasSubscriptionExpired ? 'expired' : 'will renew' } on {
222
+ subscriptionExpiryDate && (
223
+ subscriptionExpiryDate . toLocaleString ( 'en-GB' , {
224
+ hour : '2-digit' ,
225
+ minute : '2-digit' ,
226
+ hour12 : true
227
+ } ) +
228
+ ' on ' +
229
+ subscriptionExpiryDate . toLocaleString ( 'en-GB' , {
230
+ weekday : 'long' ,
231
+ day : 'numeric' ,
232
+ month : 'long' ,
233
+ year : 'numeric'
234
+ } ) )
235
+ } .
236
+ </ Typography >
237
+ </ Alert >
238
+ { paragraphs . map ( ( paragraph , index ) => (
239
+ < div key = { index } > { paragraph } </ div >
240
+ ) ) }
241
+ </ >
242
+ ) }
243
+ { subscriptionChecked
244
+ ? ( ! subscriptionExpiryDate || hasSubscriptionExpired ) &&
245
+ < Button onClick = { ( ) => {
246
+ auth . currentUser && ( window . location . href = Paths . Subscribe + auth . currentUser . uid )
247
+ } } >
248
+ Purchase
249
+ </ Button >
250
+ : < CircularProgress />
251
+ }
196
252
</ div >
197
253
) ;
198
254
} ;
0 commit comments