4436
Sl
sesame-labs-coding-assignment / files
sesame-labs-coding-assignment / ui / src / components / JoinDiscordServer / index.tsx
1.9 KB
1import axios from "axios"; 2import React, { useContext, useState } from "react"; 3import styled from "styled-components"; 4import { LinkButton, StyledButton } from "../../theme/button"; 5import { PageContext } from "../Page"; 6 7export default function JoinDiscordGuild() { 8 const context = useContext(PageContext); 9 if (!context) 10 throw new Error( 11 "JoinDiscordGuild needs to be a child of a PageContext provider" 12 ); 13 14 const [isLoading, setIsLoading] = useState(false); 15 16 const handleVerifyClick = async (e: React.MouseEvent) => { 17 e.preventDefault(); 18 setIsLoading(true); 19 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 } 33 }; 34 35 return isLoading ? ( 36 <p>Loading...</p> 37 ) : ( 38 <div> 39 <ActionsContainer> 40 {context.user?.verified ? ( 41 <p> 42 Verified! your coupon is:{" "} 43 <CouponContainer>{context.user?.coupon}</CouponContainer> 44 </p> 45 ) : ( 46 <> 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> 54 </> 55 )} 56 </ActionsContainer> 57 </div> 58 ); 59} 60 61const ActionsContainer = styled.div` 62 display: grid; 63 grid-template-columns: 1fr 1fr; 64 column-gap: 40px; 65`; 66 67const CouponContainer = styled.pre` 68 border: 2px solid ${(props) => props.theme.colors.highlight}; 69 border-radius: 8px; 70 color: ${(props) => props.theme.colors.background}; 71 background: ${(props) => props.theme.colors.foreground}; 72 font-size: 2em; 73 padding: 1em; 74`;