Skip to content

Commit 382f3f2

Browse files
committed
Add Google Form Link partial
1 parent 65a0d23 commit 382f3f2

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/partials/GoogleFormLink.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
)

src/partials/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import AbStudy from "./AbStudy"
4040
import CrowdSorcerer from "./CrowdSorcerer"
4141
import MoodleExercise from "./MoodleExercise"
4242
import SqlTrainerExercise from "./SqlTrainerExercise"
43+
import GoogleFormLink from "./GoogleFormLink"
4344

4445
import {
4546
Table,
@@ -87,6 +88,7 @@ const mapping = {
8788
"registration-link-ohja": RegistrationLink2,
8889
"moodle-exercise": MoodleExercise,
8990
"sqltrainer-exercise": SqlTrainerExercise,
91+
"google-form-link": GoogleFormLink,
9092
notice: Notice,
9193
crowdsorcerer: CrowdSorcerer,
9294
youtube: Youtube,

0 commit comments

Comments
 (0)