xonecas.io
4436
Sl

sesame-labs-coding-assignment / commit de7e904

use try/catch when using the async/await patterm

Sean2023-01-17 de7e9048068106fb6358845d45fcdf5f04b68224 parent 4026d96

5 files changed

ui/src/components/AuthenticationForm/index.tsx+27−22
@@ -7,6 +7,11 @@ export function AuthenticationForm() {
77 const [isLoading, setIsLoading] = useState(false);
88 const context = useContext(PageContext);
99
10+ if (!context)
11+ throw new Error(
12+ "AuthenticationForm needs to be a child of a PageContext provider"
13+ );
14+
1015 const handleAuthenticateClick = async (e: React.MouseEvent) => {
1116 e.preventDefault();
1217 setIsLoading(true);
@@ -20,17 +25,19 @@ export function AuthenticationForm() {
2025 method: "eth_requestAccounts",
2126 });
2227
23- await axios
24- .post("/authenticate", { walletAddress: accounts[0] })
25- .then((response) => {
26- context?.setUser(response.data);
27- })
28- .catch((error) => {
29- console.log(error);
30- context?.setError("We we're unable to receive your account details.");
31- });
28+ try {
29+ const user = await axios
30+ .post("/authenticate", {
31+ walletAddress: accounts[0],
32+ })
33+ .then((response) => response.data);
34+ context.setUser(user);
35+ } catch (e) {
36+ console.error(e);
37+ context.setError("We we're unable to receive your account details.");
38+ }
3239 } else {
33- context?.setError("Please install the metamask browser extension.");
40+ context.setError("Please install the metamask browser extension.");
3441 }
3542 setIsLoading(false);
3643 };
@@ -39,17 +46,15 @@ export function AuthenticationForm() {
3946 e.preventDefault();
4047 setIsLoading(true);
4148
42- await axios
43- .post("/deauthenticate")
44- .then(() => {
45- context?.setUser(null);
46- })
47- .catch((error) => {
48- console.log(error);
49- context?.setError(
50- "We are not able to reach the api server, please try again later."
51- );
52- });
49+ try {
50+ await axios.post("/deauthenticate");
51+ context.setUser(null);
52+ } catch (e) {
53+ console.error(e);
54+ context.setError(
55+ "We are not able to reach the api server, please try again later."
56+ );
57+ }
5358
5459 setIsLoading(false);
5560 };
@@ -58,7 +63,7 @@ export function AuthenticationForm() {
5863 <div>
5964 {isLoading ? (
6065 <div>Loading...</div>
61- ) : context?.user ? (
66+ ) : context.user ? (
6267 <StyledButton onClick={handleDeauthenticateClick}>
6368 Sign out
6469 </StyledButton>
ui/src/components/Header/index.tsx+3−1
@@ -6,10 +6,12 @@ import { PageContext } from "../Page";
66
77 export default function Header() {
88 const context = useContext(PageContext);
9+ const error = context?.error;
10+
911 return (
1012 <StyledHeader>
1113 <h1>Sesame Labs Coding Assignment</h1>
12- <ErrorContainer>{context?.error}</ErrorContainer>
14+ <ErrorContainer>{error || ""}</ErrorContainer>
1315 <div>
1416 <AuthenticationForm />
1517 </div>
ui/src/components/JoinDiscordServer/index.tsx+28−28
@@ -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>
ui/src/theme/button.tsx+18−0
@@ -13,3 +13,21 @@ export const StyledButton = styled.button`
1313 cursor: pointer;
1414 }
1515 `;
16+
17+export const LinkButton = styled.a`
18+ &,
19+ &:visited {
20+ min-width: 100px;
21+ padding: 0.5em 1em;
22+ background: ${(props) => props.theme.colors.background};
23+ color: ${(props) => props.theme.colors.foreground};
24+ border: 2px solid ${(props) => props.theme.colors.highlight};
25+ border-radius: 4px;
26+ }
27+
28+ &:hover,
29+ &:active {
30+ border-color: ${(props) => props.theme.colors.altHighlight};
31+ cursor: pointer;
32+ }
33+`;
ui/src/types.ts+6−6
@@ -1,12 +1,12 @@
1-export interface UserType {
1+export type UserType = {
22 id?: number;
33 walletAddress: string;
44 discordTokenId?: number;
55 verified: boolean;
66 coupon?: string;
7-}
7+};
88
9-export interface PageContextType {
9+export type PageContextType = {
1010 user: UserType | null;
1111 setUser: Function;
1212 quest: {
@@ -17,13 +17,13 @@ export interface PageContextType {
1717 } | null;
1818 error?: string;
1919 setError: Function;
20-}
20+};
2121
22-export interface ThemeType {
22+export type ThemeType = {
2323 colors: {
2424 background: string;
2525 foreground: string;
2626 highlight: string;
2727 altHighlight: string;
2828 };
29-}
29+};