forked from open-sauced/app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
NewsletterForm
component to design system (open-sauced#1311)
- Loading branch information
Showing
5 changed files
with
166 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import Image from "next/image"; | ||
import { AiFillCloseCircle } from "react-icons/ai"; | ||
import Button from "components/atoms/Button/button"; | ||
import TextInput from "components/atoms/TextInput/text-input"; | ||
import SaucedLogo from "img/fallbackImageColor.svg"; | ||
import { validateEmail } from "lib/utils/validate-email"; | ||
|
||
const NewsletterForm = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [email, setEmail] = useState(""); | ||
const [isValidEmail, setIsValidEmail] = useState(false); | ||
const [isSubscribed, setIsSubscribed] = useState(false); | ||
const [errorMsg, setErrorMsg] = useState(""); | ||
|
||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!isValidEmail) { | ||
setErrorMsg("Please enter a valid email"); | ||
return; | ||
} | ||
|
||
setLoading(true); | ||
|
||
// TODO: Add API call to subscribe user | ||
setTimeout(() => { | ||
setLoading(false); | ||
setIsSubscribed(true); | ||
}, 3000); | ||
}; | ||
|
||
const handleChange = (value: string) => { | ||
setEmail(value); | ||
const isValidEmail = validateEmail(email); | ||
setIsValidEmail(!!isValidEmail); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isValidEmail) { | ||
setErrorMsg(""); | ||
} | ||
}, [isValidEmail]); | ||
return ( | ||
<> | ||
{isSubscribed ? ( | ||
<div className="flex items-center justify-center gap-3 px-4 py-6 border rounded-lg h-44 bg-light-slate-1 w-80"> | ||
<div className="text-2xl text-center "> | ||
<Image className="mx-auto" src={SaucedLogo} alt="Sauced Logo" /> | ||
<p>You’re Subscribed!</p> | ||
</div> | ||
</div> | ||
) : ( | ||
<div className="flex flex-col gap-3 px-4 py-6 border rounded-lg w-max"> | ||
<div className="w-64 space-y-1"> | ||
<h2 className="text-lg">Subscribe to our newsletter</h2> | ||
<p className="text-sm text-light-slate-11">Stay up to date with the latest OpenSauced news and trends!</p> | ||
</div> | ||
<form className="w-full" onSubmit={handleSubmit}> | ||
<div className="flex items-center justify-between gap-1 "> | ||
<TextInput | ||
handleChange={(value) => handleChange(value)} | ||
state={isValidEmail ? "valid" : "invalid"} | ||
value={email} | ||
className="w-32 text-sm focus:outline-none" | ||
type="text" | ||
placeholder="Email" | ||
/> | ||
<Button | ||
loading={loading} | ||
className="inline-block py-1 border-light-orange-7 text-light-orange-10" | ||
type="submit" | ||
variant="text" | ||
> | ||
Subscribe | ||
</Button> | ||
</div> | ||
{errorMsg && ( | ||
<p className="flex items-center gap-1 mt-2 text-xs font-light text-red-500"> | ||
<AiFillCloseCircle onClick={() => setErrorMsg("")} className="text-sm cursor-pointer" /> {errorMsg} | ||
</p> | ||
)} | ||
</form> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default NewsletterForm; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
import { ComponentMeta, ComponentStory } from "@storybook/react"; | ||
import NewsletterForm from "components/molecules/NewsletterForm/newsletter-form"; | ||
|
||
const storyConfig = { | ||
title: "Design System/Molecules/NewsletterForm", | ||
} as ComponentMeta<typeof NewsletterForm>; | ||
|
||
export default storyConfig; | ||
|
||
const NewsletterFormTemplate: ComponentStory<typeof NewsletterForm> = (args) => <NewsletterForm />; | ||
|
||
export const Default = NewsletterFormTemplate.bind({}); |