@@ -24,9 +24,10 @@ export default function AppSumoForm () {
24
24
const [ password , setPassword ] = useState ( "" )
25
25
const [ code , setCode ] = useState ( "" )
26
26
const [ open , setOpen ] = useState ( false )
27
- const [ status , setStatus ] = useState ( "" )
27
+ const [ message , setMessage ] = useState ( "" )
28
28
const [ verified , setVerified ] = useState ( false )
29
29
const [ loading , setLoading ] = useState ( false )
30
+ const [ color , setColor ] = useState ( 'primary' )
30
31
const classes = useStyles ( )
31
32
32
33
const isLoggedIn = identity && identity . isLoggedIn
@@ -45,39 +46,63 @@ export default function AppSumoForm () {
45
46
e . preventDefault ( )
46
47
setLoading ( true )
47
48
if ( verified ) {
48
- let response = await fetch ( "/.netlify/functions/appSumo" , {
49
- method : "POST" ,
50
- headers : {
51
- "Content-Type" : "application/json"
52
- } ,
53
- body : JSON . stringify ( {
54
- action : 'getByCodeAndStatus' ,
55
- code,
56
- status : 'free'
57
- } )
58
- } )
59
- let coupon = await response . json ( )
60
-
61
- if ( Object . keys ( coupon ) . length ) {
62
- await identity . signupUser ( email , password , {
63
- full_name : name ,
64
- appsumo_code : code
65
- } )
66
-
67
- const user = await identity . loginUser ( email , password , true )
68
-
69
- await fetch ( "/.netlify/functions/appSumo" , {
49
+ try {
50
+ setMessage ( 'Verifying AppSumo Code...' )
51
+ setOpen ( true )
52
+ let response = await fetch ( "/.netlify/functions/appSumo" , {
70
53
method : "POST" ,
71
54
headers : {
72
- "Content-Type" : "application/json" ,
73
- "Authorization" : `Bearer ${ user . token . access_token } `
55
+ "Content-Type" : "application/json"
74
56
} ,
75
57
body : JSON . stringify ( {
76
- action : 'activate'
58
+ action : 'getByCodeAndStatus' ,
59
+ code,
60
+ status : 'free'
77
61
} )
78
62
} )
63
+ let coupon = await response . json ( )
64
+
65
+ if ( Object . keys ( coupon ) . length ) {
66
+ setMessage ( 'Registering user...' )
67
+ setOpen ( true )
68
+ await identity . signupUser ( email , password , {
69
+ full_name : name ,
70
+ appsumo_code : code
71
+ } )
79
72
80
- navigate ( '/account' )
73
+ setMessage ( 'Logging in...' )
74
+ setOpen ( true )
75
+ const user = await identity . loginUser ( email , password , true )
76
+
77
+ setMessage ( 'Activating AppSumo Code...' )
78
+ setOpen ( true )
79
+ await fetch ( "/.netlify/functions/appSumo" , {
80
+ method : "POST" ,
81
+ headers : {
82
+ "Content-Type" : "application/json" ,
83
+ "Authorization" : `Bearer ${ user . token . access_token } `
84
+ } ,
85
+ body : JSON . stringify ( {
86
+ action : 'activate'
87
+ } )
88
+ } )
89
+
90
+ setMessage ( 'Redirecting to account page...' )
91
+ setOpen ( true )
92
+ await navigate ( '/account' )
93
+ } else {
94
+ setMessage ( `AppSumo Code '${ code } ' was not found in the system.` )
95
+ setOpen ( true )
96
+ setColor ( 'secondary' )
97
+ }
98
+ }
99
+ catch ( e ) {
100
+ setMessage ( e . message )
101
+ setOpen ( true )
102
+ setColor ( 'secondary' )
103
+ }
104
+ finally {
105
+ setLoading ( false )
81
106
}
82
107
83
108
// fetch("/", {
@@ -100,7 +125,7 @@ export default function AppSumoForm () {
100
125
// })
101
126
}
102
127
else {
103
- setStatus ( "Captcha not verified. Please select again." )
128
+ setMessage ( "Captcha not verified. Please select again." )
104
129
setOpen ( true )
105
130
setLoading ( false )
106
131
}
@@ -213,9 +238,10 @@ export default function AppSumoForm () {
213
238
< Snackbar
214
239
open = { open }
215
240
onClose = { ( ) => setOpen ( false ) }
216
- message = { status }
241
+ message = { message }
217
242
TransitionComponent = { Fade }
218
243
autoHideDuration = { 3000 }
244
+ color = { color }
219
245
/>
220
246
</ GridItem >
221
247
</ GridContainer >
0 commit comments