-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add email subscription component (#474)
* Add email subscription form component * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Suhaha <[email protected]> * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Suhaha <[email protected]> * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Yuiham <[email protected]> * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Yuiham <[email protected]> * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Yuiham <[email protected]> * Update src/components/MDXComponents/EmailSubscriptionForm.tsx Co-authored-by: Yuiham <[email protected]> * Use CSS-in-JS * Adjust imports * Address comments * fix(components/email-subscription): correct styles * fix(components/email-subscription): fix API request --------- Co-authored-by: Liya Du <[email protected]> Co-authored-by: Suhaha <[email protected]> Co-authored-by: Yuiham <[email protected]>
- Loading branch information
1 parent
ea7d0a3
commit 62390f0
Showing
6 changed files
with
185 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { Trans, useI18next } from "gatsby-plugin-react-i18next" | ||
import { useState } from "react" | ||
import { z } from "zod" | ||
|
||
import axios from "axios" | ||
|
||
import { LoadingButton } from "@mui/lab" | ||
import Box from "@mui/material/Box" | ||
import TextField from "@mui/material/TextField" | ||
|
||
|
||
type FormType = { | ||
email: string; | ||
loading: boolean; | ||
error: null | "invalidEmail" | "networkError"; | ||
}; | ||
|
||
|
||
// Collect email address that subscribe to TiDB release and send it to the SendGrid API | ||
function EmailSubscriptionForm() { | ||
const { t, navigate } = useI18next(); | ||
|
||
const API_URL = | ||
"https://03tryjsrrb.execute-api.us-west-2.amazonaws.com/Prod/subscribe/"; | ||
|
||
const [formData, setFormData] = useState<FormType>({ | ||
email: "", | ||
loading: false, | ||
error: null, | ||
}); | ||
const [success, setSuccess] = useState(false); | ||
|
||
const validateEmail = (): boolean => { | ||
// Set up a schema for validating the email address | ||
const schema = z.string().email(); | ||
|
||
try { | ||
schema.parse(formData.email); | ||
return true; | ||
} catch (e) { | ||
console.error(e); | ||
setFormData({ ...formData, error: "invalidEmail" }); | ||
return false; | ||
} | ||
}; | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { value } = event.target; | ||
setFormData((prevState) => ({ ...prevState, email: value })); | ||
}; | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (validateEmail()) { | ||
// Submitting... | ||
setFormData({ ...formData, loading: true, error: null }); | ||
|
||
try { | ||
|
||
const payload = new URLSearchParams({ | ||
email: formData.email | ||
}) | ||
await axios.post(API_URL, payload); | ||
|
||
setFormData({ ...formData, loading: false, error: null }); | ||
setSuccess(true); | ||
} catch (e) { | ||
setFormData({ ...formData, loading: false, error: "networkError" }); | ||
console.error(e); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<p> | ||
<strong> | ||
<Trans i18nKey="releaseSubscription.title" />: | ||
</strong> | ||
</p> | ||
|
||
{!success ? ( | ||
<div> | ||
<form onSubmit={handleSubmit}> | ||
<Box display="flex" alignItems="baseline"> | ||
<TextField | ||
size="small" | ||
variant="outlined" | ||
type="text" | ||
name="email" | ||
placeholder={t("releaseSubscription.placeholder")} | ||
value={formData.email} | ||
onChange={handleChange} | ||
error={!!formData.error} | ||
helperText={!!formData.error ? (formData.error === "invalidEmail" ? t('releaseSubscription.error.invalidEmail') : t('releaseSubscription.error.networkError')) : ' '} | ||
/> | ||
|
||
<LoadingButton | ||
type="submit" | ||
variant="contained" | ||
color="primary" | ||
loading={formData.loading} | ||
loadingIndicator={t('releaseSubscription.button.subscribing')} | ||
sx={{ ml: '1rem' }} | ||
> | ||
<Trans i18nKey="releaseSubscription.button.subscribe" /> | ||
</LoadingButton> | ||
</Box> | ||
</form> | ||
</div> | ||
) : ( | ||
<Box> | ||
<p> | ||
<span | ||
role="img" | ||
aria-label="love-letter" | ||
style={{ marginRight: "0.2rem" }} | ||
> | ||
💌 | ||
</span> | ||
<Trans i18nKey="releaseSubscription.success" /> | ||
</p> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export function EmailSubscriptionWrapper() { | ||
const { language } = useI18next(); | ||
const disabled = language === Locale.ja; | ||
|
||
if (disabled) { | ||
return null; | ||
} | ||
|
||
return <EmailSubscriptionForm /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
export { | ||
Tip, | ||
Note, | ||
CustomAlert, | ||
Important, | ||
Note, | ||
Tip, | ||
Warning, | ||
CustomAlert, | ||
} from "components/MDXComponents/Alert"; | ||
export { | ||
ColumnTitle, | ||
NavColumn, | ||
NavColumns, | ||
} from "components/MDXComponents/ColumnDeprecated"; | ||
export { SimpleTab } from "components/MDXComponents/SimpleTab"; | ||
export { SyntaxDiagram } from "components/MDXComponents/SyntaxDiagram"; | ||
export { TabsPanel } from "components/MDXComponents/TabsPanel"; | ||
export { CustomContent } from "components/MDXComponents/CustomContent"; | ||
export { | ||
LearningPathContainer, | ||
LearningPath, | ||
} from "components/MDXComponents/LearningPath"; | ||
export { | ||
DocHomeCard, | ||
DocHomeCardContainer, | ||
DocHomeContainer, | ||
DocHomeSection, | ||
DocHomeCardContainer, | ||
DocHomeCard, | ||
} from "components/MDXComponents/DocHome"; | ||
export { EmailSubscriptionWrapper } from "components/MDXComponents/EmailSubscriptionForm"; | ||
export { | ||
LearningPath, | ||
LearningPathContainer, | ||
} from "components/MDXComponents/LearningPath"; | ||
export { MDSvgIcon } from "components/MDXComponents/MDSvgIcon"; | ||
export { SimpleTab } from "components/MDXComponents/SimpleTab"; | ||
export { SyntaxDiagram } from "components/MDXComponents/SyntaxDiagram"; | ||
export { TabsPanel } from "components/MDXComponents/TabsPanel"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters