@@ -1,40 +1,35 @@
11 import axios from "axios";
22 import React, { useContext, useState } from "react";
33 import styled from "styled-components";
4-import { StyledButton } from "../../theme/button";
4+import { LinkButton, StyledButton } from "../../theme/button";
55 import { PageContext } from "../Page";
66
77 export default function JoinDiscordGuild() {
88 const context = useContext(PageContext);
9- const [isLoading, setIsLoading] = useState(false);
10-
11- const handleJoinLink = async (e: React.MouseEvent) => {
12- e.preventDefault();
13-
14- window.open(
15- `${process.env.REACT_APP_API_URL}/discord/join`,
16- "_blank",
17- "noopener,noreferrer"
9+ if (!context)
10+ throw new Error(
11+ "JoinDiscordGuild needs to be a child of a PageContext provider"
1812 );
19- };
13+
14+ const [isLoading, setIsLoading] = useState(false);
2015
2116 const handleVerifyClick = async (e: React.MouseEvent) => {
2217 e.preventDefault();
2318 setIsLoading(true);
2419
25- await axios
26- .get("/discord/verify")
27- .then((response) => {
28- setIsLoading(false);
29- context?.setUser(response.data);
30- })
31- .catch((e) => {
32- console.log(e);
33- setIsLoading(false);
34- context?.setError(
35- "We are having trouble reaching discord, please try again later."
36- );
37- });
20+ try {
21+ const user = await axios
22+ .get("/discord/verify")
23+ .then((response) => response.data);
24+ context.setUser(user);
25+ setIsLoading(false);
26+ } catch (e) {
27+ console.error(e);
28+ context.setError(
29+ "We are having trouble reaching discord, please try again later."
30+ );
31+ setIsLoading(false);
32+ }
3833 };
3934
4035 return isLoading ? (
@@ -42,15 +37,20 @@ export default function JoinDiscordGuild() {
4237 ) : (
4338 <div>
4439 <ActionsContainer>
45- {context?.user?.verified ? (
40+ {context.user?.verified ? (
4641 <p>
4742 Verified! your coupon is:{" "}
48- <CouponContainer>{context?.user?.coupon}</CouponContainer>
43+ <CouponContainer>{context.user?.coupon}</CouponContainer>
4944 </p>
5045 ) : (
5146 <>
52- <StyledButton onClick={handleJoinLink}>Join</StyledButton>
53- <StyledButton onClick={handleVerifyClick}>verify</StyledButton>
47+ <LinkButton
48+ href={`${process.env.REACT_APP_API_URL}/discord/join`}
49+ target={"_blank"}
50+ >
51+ Join
52+ </LinkButton>
53+ <StyledButton onClick={handleVerifyClick}>Verify</StyledButton>
5454 </>
5555 )}
5656 </ActionsContainer>