Skip to content

Commit 977677f

Browse files
committed
Added snackbar-based progress info.
1 parent dbb9109 commit 977677f

File tree

2 files changed

+103
-55
lines changed

2 files changed

+103
-55
lines changed

src/components/AppSumoForm.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ export default function AppSumoForm () {
2424
const [password, setPassword] = useState("")
2525
const [code, setCode] = useState("")
2626
const [open, setOpen] = useState(false)
27-
const [status, setStatus] = useState("")
27+
const [message, setMessage] = useState("")
2828
const [verified, setVerified] = useState(false)
2929
const [loading, setLoading] = useState(false)
30+
const [color, setColor] = useState('primary')
3031
const classes = useStyles()
3132

3233
const isLoggedIn = identity && identity.isLoggedIn
@@ -45,39 +46,63 @@ export default function AppSumoForm () {
4546
e.preventDefault()
4647
setLoading(true)
4748
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", {
7053
method: "POST",
7154
headers: {
72-
"Content-Type": "application/json",
73-
"Authorization": `Bearer ${user.token.access_token}`
55+
"Content-Type": "application/json"
7456
},
7557
body: JSON.stringify({
76-
action: 'activate'
58+
action: 'getByCodeAndStatus',
59+
code,
60+
status: 'free'
7761
})
7862
})
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+
})
7972

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)
81106
}
82107

83108
// fetch("/", {
@@ -100,7 +125,7 @@ export default function AppSumoForm () {
100125
// })
101126
}
102127
else {
103-
setStatus("Captcha not verified. Please select again.")
128+
setMessage("Captcha not verified. Please select again.")
104129
setOpen(true)
105130
setLoading(false)
106131
}
@@ -213,9 +238,10 @@ export default function AppSumoForm () {
213238
<Snackbar
214239
open={open}
215240
onClose={() => setOpen(false)}
216-
message={status}
241+
message={message}
217242
TransitionComponent={Fade}
218243
autoHideDuration={3000}
244+
color={color}
219245
/>
220246
</GridItem>
221247
</GridContainer>

src/pages/account.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CircularProgress } from "@material-ui/core"
1+
import { CircularProgress, Fade, Snackbar } from "@material-ui/core"
22
import { makeStyles } from "@material-ui/core/styles"
33
import classNames from "classnames"
44
import { navigate } from "gatsby"
@@ -15,35 +15,46 @@ const useStyles = makeStyles(accountPageStyle)
1515

1616
function AccountPage () {
1717
const classes = useStyles();
18-
const identity = useIdentityContext()
19-
const isLoggedIn = identity && identity.isLoggedIn
20-
const [coupon, setCoupon] = useState(false)
21-
const [loading, setLoading] = useState(true)
18+
const identity = useIdentityContext();
19+
const isLoggedIn = identity && identity.isLoggedIn;
20+
const [coupon, setCoupon] = useState(false);
21+
const [loading, setLoading] = useState(true);
22+
const [open, setOpen] = useState(false);
23+
const [message, setMessage] = useState("");
2224

2325
useEffect(() => {
2426
const getCode = async () => {
25-
const response = await fetch("/.netlify/functions/appSumo", {
26-
method: "POST",
27-
headers: {
28-
"Authorization": `Bearer ${identity.user.token.access_token}`,
29-
"Content-Type": "application/json"
30-
},
31-
body: JSON.stringify({
32-
action: 'getByCode'
33-
})
34-
})
35-
const data = await response.json()
27+
try {
28+
const response = await fetch("/.netlify/functions/appSumo", {
29+
method: "POST",
30+
headers: {
31+
"Authorization": `Bearer ${identity.user.token.access_token}`,
32+
"Content-Type": "application/json"
33+
},
34+
body: JSON.stringify({
35+
action: "getByCode"
36+
})
37+
});
3638

37-
setLoading(false)
38-
if (Object.keys(data).length) {
39-
setCoupon(data)
39+
const data = await response.json();
40+
if (Object.keys(data).length) {
41+
setCoupon(data);
42+
}
43+
}
44+
catch (e) {
45+
setMessage(e.message);
46+
setOpen(true);
47+
}
48+
finally {
49+
setLoading(false);
4050
}
41-
}
51+
52+
};
4253

4354
if (isLoggedIn) {
44-
getCode()
55+
getCode();
4556
}
46-
}, [isLoggedIn, identity])
57+
}, [isLoggedIn, identity]);
4758

4859
if (isLoggedIn) {
4960
const imageClasses = classNames(
@@ -75,21 +86,32 @@ function AccountPage () {
7586
<AppSumoCode coupon={coupon} user={identity.user} classes={classes} />
7687
</GridItem>
7788
: null}
89+
{open ?
90+
<GridItem xs={12} md={6} style={{ marginTop: "30px", textAlign: "center" }}>
91+
<Snackbar
92+
open={open}
93+
onClose={() => setOpen(false)}
94+
message={message}
95+
TransitionComponent={Fade}
96+
autoHideDuration={3000}
97+
/>
98+
</GridItem>
99+
: null}
78100
</GridContainer>
79101
</div>
80102
</div>
81103
);
82104
}
83105

84-
if (typeof window !== 'undefined') {
85-
navigate("/", { replace: true })
106+
if (typeof window !== "undefined") {
107+
navigate("/", { replace: true });
86108
}
87109

88-
return 'Redirecting...'
110+
return "Redirecting...";
89111
}
90112

91113
export default function() {
92114
return <RenderOnMount>
93115
<AccountPage />
94-
</RenderOnMount>
116+
</RenderOnMount>;
95117
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy