|
| 1 | +import React, { useContext } from "react" |
| 2 | +import LoginStateContext from "../contexes/LoginStateContext" |
| 3 | +import styled from "styled-components" |
| 4 | +import { Card } from "@material-ui/core" |
| 5 | +import { OutboundLink } from "gatsby-plugin-google-analytics" |
| 6 | +import { withTranslation } from "react-i18next" |
| 7 | +import withSimpleErrorBoundary from "../util/withSimpleErrorBoundary" |
| 8 | +import { useAsync } from "react-use" |
| 9 | +import { getCachedUserDetails } from "../services/moocfi" |
| 10 | + |
| 11 | +const Wrapper = styled(Card)` |
| 12 | + margin-bottom: 2rem; |
| 13 | + padding: 1rem; |
| 14 | +` |
| 15 | + |
| 16 | +const MessageWrapper = styled.div` |
| 17 | + display: flex; |
| 18 | + align-items: center; |
| 19 | +` |
| 20 | + |
| 21 | +const P = styled.p` |
| 22 | + margin-bottom: 1rem !important; |
| 23 | +` |
| 24 | + |
| 25 | +const GoogleFormLink = ({ children, href, t, emailfieldname }) => { |
| 26 | + const { loggedIn } = useContext(LoginStateContext) |
| 27 | + const userDetails = useAsync(getCachedUserDetails) |
| 28 | + |
| 29 | + if (!loggedIn) { |
| 30 | + return ( |
| 31 | + <Wrapper> |
| 32 | + <MessageWrapper> |
| 33 | + <div> |
| 34 | + <P>{t("loginToSee")}</P> |
| 35 | + </div> |
| 36 | + </MessageWrapper> |
| 37 | + </Wrapper> |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + if (userDetails.loading) { |
| 42 | + return <div>Loading...</div> |
| 43 | + } |
| 44 | + |
| 45 | + if (userDetails.error) { |
| 46 | + return <div>Error while loading user information.</div> |
| 47 | + } |
| 48 | + |
| 49 | + const email = userDetails.value.email |
| 50 | + |
| 51 | + let link = href |
| 52 | + if (emailfieldname) { |
| 53 | + link = `${link}&${encodeURIComponent(emailfieldname)}=${encodeURIComponent( |
| 54 | + email, |
| 55 | + )}` |
| 56 | + } |
| 57 | + return ( |
| 58 | + <OutboundLink href={link} target="_blank" rel="noopener noreferrer"> |
| 59 | + {children} |
| 60 | + </OutboundLink> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +export default withTranslation("common")( |
| 65 | + withSimpleErrorBoundary(GoogleFormLink), |
| 66 | +) |
0 commit comments