Skip to content

Commit

Permalink
Merge pull request #48 from pluralsh/klink/newsletter-signup
Browse files Browse the repository at this point in the history
feat: Use Hubspot API for Newsletter signups
  • Loading branch information
dogmar committed Aug 4, 2023
2 parents d173b31 + 082dc93 commit e87a560
Show file tree
Hide file tree
Showing 46 changed files with 423 additions and 151 deletions.
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
Documentation site for [Plural](https://www.plural.sh/), the open-source, unified, application deployment platform that makes it easy to run open-source software on Kubernetes. Our marketplace has dozens of top tier applications ready to deploy.
[![Netlify Status](https://api.netlify.com/api/v1/badges/58af7361-4aec-4e82-a963-7b192690851a/deploy-status)](https://app.netlify.com/sites/pluraldocs/deploys)

Marketing site for [Plural](https://www.plural.sh/), the open-source, unified, application deployment platform that makes it easy to run open-source software on Kubernetes. Our marketplace has dozens of top tier applications ready to deploy.

Built with [Next.js](https://nextjs.org/) and [Markdoc](https://markdoc.dev/).

## Contributing

### Running the docs locally
### Running the site locally

To run the docs locally, you'll need to have yarn and node as prerequisites. Then run the following commands:
To run the site locally, you'll need to have yarn and node as prerequisites. Then run the following commands:

```shell
yarn # build the environment
yarn dev # run docs locally
yarn dev # run site locally
```

### Adding a new document

All content can be located under the [pages](/pages) directory. The directory structure directly informs the website URL structure, so consider that when placing your new document.

All of our documents are in standard Markdown (.md) file format. If you are including an image, add it to [public/assets] using the same directory structure as pages.

Finally, make sure to add your new document to the NavData.tsx file located [here](/src/NavData.tsx).

### Updating structure

If you are making any changes to the documentation structure or organization, you'll likely need to set up page redirects. These can be added in [next.config.js](next.config.js). Make sure to
look for internal usages throughout all the documents on the site to make sure that there are no broken links.
128 changes: 128 additions & 0 deletions app/api/newsletter/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { type NextRequest, NextResponse } from 'next/server'

import { Client } from '@hubspot/api-client'
import queryString from 'query-string'

import { isValidEmail } from '@src/utils/text'

const hubspotClient = new Client({
accessToken: process.env.HUBSPOT_API_SECRET,
})

async function isCaptchaValid(captchaVal) {
const postBody = queryString.stringify({
secret: process.env.SITE_RECAPTCHA_SECRET,
response: captchaVal,
})
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
})

try {
const res = await fetch('https://www.google.com/recaptcha/api/siteverify', {
method: 'POST',
headers,
body: postBody,
})
const response = await res.json()

return response.success
} catch (e) {
return false
}
}

async function setEmail(email) {
const response = await hubspotClient.apiRequest({
method: 'POST',
path: '/crm/v3/objects/contacts',
body: { properties: { email } },
})

if (!response.ok) {
try {
const body = (await response.json()) as {
category?: string
message?: string
}

if (body?.category === 'CONFLICT') {
const contactId = body.message?.match(/Existing ID:\s*(?<id>\d+)/)
?.groups?.id

if (!contactId) {
return false
}
const patchRes = await hubspotClient.apiRequest({
method: 'PATCH',
path: `/crm/v3/objects/contacts/${contactId}`,
body: { properties: { email } },
})

if (patchRes.ok) {
return true
}

return false
}
} catch (e) {
return false
}
}

return true
}

const ErrorResponse = (error: {
type: 'form' | 'captcha' | 'hubspot'
message: string
}) =>
NextResponse.json(
{
error,
},
{ status: 500 }
)

export async function POST(req: NextRequest) {
let formData: FormData

try {
formData = await req.formData()
} catch (e) {
return ErrorResponse({ type: 'form', message: `${e}` })
}
const email = formData.get('email')

if (!email || typeof email !== 'string') {
return ErrorResponse({
type: 'form',
message: 'Missing email address',
})
}
if (!isValidEmail(email)) {
return ErrorResponse({
type: 'form',
message: 'Invalid email address',
})
}

const validCaptcha = await isCaptchaValid(
formData.get('g-recaptcha-response')
)

if (!validCaptcha) {
return ErrorResponse({ type: 'captcha', message: 'Invalid captcha' })
}

const success = setEmail(email)

if (!success) {
return ErrorResponse({
type: 'hubspot',
message: 'Could not create contact',
})
}

return NextResponse.json({ success })
}
1 change: 1 addition & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@graphql-codegen/typescript": "4.0.1",
"@graphql-codegen/typescript-operations": "4.0.1",
"@graphql-codegen/typescript-react-apollo": "3.3.7",
"@hubspot/api-client": "9.1.0",
"@loomhq/loom-embed": "1.5.0",
"@markdoc/markdoc": "0.3.0",
"@markdoc/next.js": "0.2.2",
Expand All @@ -56,15 +57,16 @@
"js-yaml": "4.1.0",
"lodash-es": "4.17.21",
"memoize-one": "6.0.0",
"moment-timezone": "^0.5.43",
"moment-timezone": "0.5.43",
"next": "13.4.12",
"next-compose-plugins": "2.2.1",
"next-transpile-modules": "10.0.0",
"octokit": "^3.1.0",
"octokit": "3.1.0",
"posthog-js": "1.75.2",
"query-string": "8.1.0",
"raw-loader": "4.0.2",
"react": "18.2.0",
"react-add-to-calendar": "^0.1.5",
"react-add-to-calendar": "0.1.5",
"react-aria": "3.25.0",
"react-dom": "18.2.0",
"react-embed": "3.7.0",
Expand Down
30 changes: 0 additions & 30 deletions public/forms/newsletter.html

This file was deleted.

Binary file modified public/images/careers/photo-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/careers/photo-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/careers/photo-group-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/careers/photo-group-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/cont-deploy/create-cluster-m.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/cont-deploy/deploy-service.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/cont-deploy/deployments-clusters-update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/cont-deploy/pipelines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/cont-deploy/whitepaper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/images/gradients/gradient-blue-1.png
Binary file not shown.
Binary file modified public/images/homepage/features/easy-setup-2_poster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/homepage/features/production-2.mp4
Binary file not shown.
Binary file removed public/images/homepage/features/production-2.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/homepage/features/security-2_poster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/homepage/features/upgrades-1.mp4
Binary file not shown.
Binary file removed public/images/homepage/features/upgrades-1.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/homepage/hero-apps.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/homepage/hero-configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/how-plural-works/how-plural-works-screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 1 addition & 11 deletions public/images/icons/search-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions public/images/icons/star.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/marketplace/png/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/marketplace/png/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/marketplace/png/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/marketplace/png/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/images/marketplace/png/[email protected]
Binary file modified public/images/marketplace/png/[email protected]
28 changes: 1 addition & 27 deletions public/images/plural-logo.svg
Binary file modified public/images/product/dotted-line-vertical.png
Binary file modified public/images/product/gdpr-cert.png
100755 → 100644
4 changes: 2 additions & 2 deletions src/components/ExternalScripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function HubSpot() {
return (
<Script
type="text/plain" // text/plain prevents loading until Cookiebot determines prefs
data-cookieconsent="statistics"
data-cookieconsent="marketing"
strategy="afterInteractive"
id="hs-script-loader"
async
Expand All @@ -64,7 +64,7 @@ function Clearbit() {
return (
<Script
type="text/plain" // text/plain prevents loading until Cookiebot determines prefs
data-cookieconsent="statistics"
data-cookieconsent="marketing"
async
src="https://tag.clearbitscripts.com/v1/pk_f7a55ef5149a4b9142a485385b8b0c96/tags.js"
referrerPolicy="strict-origin-when-cross-origin"
Expand Down
4 changes: 3 additions & 1 deletion src/components/GithubStars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { GitHubLogoIcon } from '@pluralsh/design-system'
import styled from 'styled-components'
import useSWR from 'swr'

import { abbrNum } from '@src/utils/abbrNum'

import { ButtonFillTwo } from './PageHeaderButtons'

type GithubRepoData = {
Expand Down Expand Up @@ -124,7 +126,7 @@ export default function GithubStars({ account = 'pluralsh', repo = 'plural' }) {
rel="noreferrer noopener"
href={`https://github.com/${account}/${repo}/stargazers`}
>
{data?.stargazers_count}
{abbrNum(data?.stargazers_count, 1)}
</a>
)}
</GithubLink>
Expand Down
32 changes: 17 additions & 15 deletions src/components/NewsletterSignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export function NewsletterSignupForm() {
message: `There was a problem subscribing to the newsletter: ${error}`,
})
}

function resetRecaptcha() {
recaptchaRef?.current?.reset()
setRecaptchaVal('')
}

const submitEmail = useCallback<FormEventHandler<HTMLFormElement>>(
(event) => {
event.preventDefault()
Expand Down Expand Up @@ -79,35 +85,31 @@ export function NewsletterSignupForm() {
}
const body = new URLSearchParams(formData as any).toString()

recaptchaRef?.current?.reset()
setRecaptchaVal('')

if (process.env.NODE_ENV === 'development') {
setResponse({
type: 'error',
message: 'Must be server-deployed to submit form.',
})

return
}
fetch('/forms/newsletter.html', {
fetch('/api/newsletter', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body,
})
.then((e) => {
if (e.ok) {
.then((res) => {
if (res.ok) {
setResponse({
type: 'success',
message: 'Thank you for subscribing to the newsletter',
})
setEmail('')
} else {
setError(e.statusText)
res.json().then((resObj) => {
if (resObj?.error?.type === 'captcha') {
setRecaptchaVal('')
}
setError(resObj?.error?.message || 'Unknown issue')
})
}
resetRecaptcha()
})
.catch((error) => {
setError(error)
resetRecaptcha()
})
},
[recaptchaVal]
Expand Down
Loading

0 comments on commit e87a560

Please sign in to comment.