Skip to content

Commit

Permalink
feat(blog): show newsletter form or link below posts (#8920)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA authored Jun 15, 2023
1 parent 83191ea commit 203b298
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 56 deletions.
2 changes: 2 additions & 0 deletions client/src/blog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BlogImage, BlogPostFrontmatter } from "../../../libs/types/blog.js";
import "./index.scss";
import "./post.scss";
import { Button } from "../ui/atoms/button";
import { SignUpSection as NewsletterSignUp } from "../newsletter";

interface BlogIndexData {
posts: BlogPostFrontmatter[];
Expand Down Expand Up @@ -101,6 +102,7 @@ function BlogIndex(props: HydrationData) {
return <PostPreview key={fm.slug} fm={fm} />;
})}
</main>
<NewsletterSignUp />
</>
);
}
5 changes: 5 additions & 0 deletions client/src/blog/post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

.blog-container.post {
max-width: calc(48rem + 4rem);

+ .section-newsletter h2 {
font: var(--type-heading-h3);
margin: 0;
}
}

.blog-container {
Expand Down
28 changes: 17 additions & 11 deletions client/src/blog/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../../../libs/types/blog";
import { useCopyExamplesToClipboard } from "../document/hooks";
import { DEFAULT_LOCALE } from "../../../libs/constants";
import { SignUpSection as NewsletterSignUp } from "../newsletter";

function MaybeLink({ link, children }) {
return link ? (
Expand Down Expand Up @@ -175,17 +176,22 @@ export function BlogPost(props: HydrationData) {
return (
<>
{doc && blogMeta && (
<article
className="blog-container post container main-page-content"
lang={doc?.locale}
>
<BlogImageFigure image={blogMeta?.image} width={800} height={420} />
{blogMeta?.sponsored && <span className="sponsored">Sponsored</span>}
<h1>{doc?.title}</h1>
<AuthorDateReadTime metadata={blogMeta} />
<RenderDocumentBody doc={doc} />
{blogMeta.links && <PreviousNext links={blogMeta.links} />}
</article>
<>
<article
className="blog-container post container main-page-content"
lang={doc?.locale}
>
<BlogImageFigure image={blogMeta?.image} width={800} height={420} />
{blogMeta?.sponsored && (
<span className="sponsored">Sponsored</span>
)}
<h1>{doc?.title}</h1>
<AuthorDateReadTime metadata={blogMeta} />
<RenderDocumentBody doc={doc} />
{blogMeta.links && <PreviousNext links={blogMeta.links} />}
</article>
<NewsletterSignUp />
</>
)}
</>
);
Expand Down
6 changes: 5 additions & 1 deletion client/src/newsletter/index.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.main-newsletter {
.section-newsletter {
margin: 0 auto;
max-width: 35rem;
padding: 2rem;
Expand All @@ -7,4 +7,8 @@
button {
width: 100%;
}

form {
margin-bottom: 2rem;
}
}
106 changes: 63 additions & 43 deletions client/src/newsletter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React, { useState } from "react";
import React, { createElement, useState } from "react";

import { useIsServer } from "../hooks";
import { useIsServer, useLocale } from "../hooks";
import { Button } from "../ui/atoms/button";
import { MainContentContainer } from "../ui/atoms/page-content";
import { useUserData } from "../user-context";

import "./index.scss";

export function Newsletter() {
return (
<MainContentContainer className="main-newsletter">
<MainContentContainer className="section-newsletter">
<SignUpForm />
</MainContentContainer>
);
}

function SignUpForm() {
function SignUpForm({ sendUsersToSettings = false, section = false }) {
const isServer = useIsServer();
const user = useUserData();
const locale = useLocale();
const [pending, setPending] = useState(false);
const [error, setError] = useState(false);
const [submitted, setSubmitted] = useState(false);
Expand Down Expand Up @@ -44,7 +47,7 @@ function SignUpForm() {

return submitted ? (
<>
<h1>Thanks!</h1>
{createElement(section ? "h2" : "h1", null, "Thanks!")}
<p>
If you haven't previously confirmed a subscription to a Mozilla-related
newsletter, you may have to do so. Please check your inbox or your spam
Expand All @@ -53,53 +56,70 @@ function SignUpForm() {
</>
) : (
<>
<h1>Stay Informed with MDN</h1>
{createElement(section ? "h2" : "h1", null, "Stay Informed with MDN")}
<p>
Get the MDN newsletter and never miss an update on the latest web
development trends, tips, and best practices.
</p>
<form className="mdn-form mdn-form-big" onSubmit={submit}>
<div className="mdn-form-item">
<label htmlFor="newsletter_email">Your email address:</label>
{sendUsersToSettings && user?.isAuthenticated ? (
<p>
Sign up via the{" "}
<a href={`/${locale}/plus/settings#newsletter`} rel="_blank">
Settings Page.
</a>
</p>
) : (
<form className="mdn-form mdn-form-big" onSubmit={submit}>
<div className="mdn-form-item">
<label htmlFor="newsletter_email">Your email address:</label>

<input
type="email"
name="email"
required={true}
placeholder="[email protected]"
id="newsletter_email"
value={email}
onChange={(e) => setEmail(e.target.value)}
disabled={pending}
/>
</div>

<div className="mdn-form-item">
<label htmlFor="newsletter_privacy">
<input
type="checkbox"
id="newsletter_privacy"
name="privacy"
type="email"
name="email"
required={true}
placeholder="[email protected]"
id="newsletter_email"
value={email}
onChange={(e) => setEmail(e.target.value)}
disabled={pending}
/>{" "}
I’m okay with Mozilla handling my info as explained in this{" "}
<a
href="https://www.mozilla.org/en-US/privacy/websites/"
target="_blank"
rel="noreferrer"
>
Privacy Notice
</a>
</label>
</div>
/>
</div>

<div className="mdn-form-item">
<label htmlFor="newsletter_privacy">
<input
type="checkbox"
id="newsletter_privacy"
name="privacy"
required={true}
disabled={pending}
/>{" "}
I’m okay with Mozilla handling my info as explained in this{" "}
<a
href="https://www.mozilla.org/en-US/privacy/websites/"
target="_blank"
rel="noreferrer"
>
Privacy Notice
</a>
</label>
</div>

<div className="mdn-form-item">
<Button buttonType="submit" isDisabled={pending || isServer}>
{error ? "Something went wrong, try again" : "Sign Up Now"}
</Button>
</div>
</form>
<div className="mdn-form-item">
<Button buttonType="submit" isDisabled={pending || isServer}>
{error ? "Something went wrong, try again" : "Sign Up Now"}
</Button>
</div>
</form>
)}
</>
);
}

export function SignUpSection() {
return (
<section className="section-newsletter">
<SignUpForm sendUsersToSettings={true} section={true} />
</section>
);
}
2 changes: 1 addition & 1 deletion client/src/settings/newsletter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function Newsletter() {

return (
<section className="field-group">
<h2>Newsletter</h2>
<h2 id="newsletter">Newsletter</h2>
<ul>
<li>
<h3>Receive updates from MDN Plus</h3>
Expand Down

0 comments on commit 203b298

Please sign in to comment.