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