Skip to content

Commit 14d25a8

Browse files
committed
adding Alert to support success, error, info, warning, and message
1 parent 1eb2867 commit 14d25a8

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

Diff for: UI/Alert.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Based on https://github.com/leerob/leerob.io/blob/main/components/SuccessMessage.tsx
2+
import React, {useEffect, useState} from 'react';
3+
4+
export default function Alert({ type="message", children }) {
5+
// An success message is green with a checkmark
6+
// An error message is red with an x
7+
// An info message is blue with a question mark
8+
// An warning message is yellow with a warning sign
9+
// An message is black with a question mark
10+
const [color, setColor] = useState();
11+
const [icon, setIcon] = useState();
12+
13+
useEffect(() => {
14+
if (type === 'success') {
15+
setColor("text-green-700 dark:text-green-400")
16+
setIcon("M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z")
17+
} else if (type === "error") {
18+
setColor("text-red-700 dark:text-red-400")
19+
setIcon("M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z")
20+
} else if (type === "info") {
21+
setColor("text-blue-700 dark:text-blue-400")
22+
setIcon("M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z")
23+
} else if (type === "warning") {
24+
setColor("text-yellow-700 dark:text-yellow-400")
25+
setIcon("M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z")
26+
} else {
27+
setColor("text-black")
28+
setIcon("M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z")
29+
}
30+
}, [type])
31+
32+
33+
return (
34+
<>
35+
{color && icon && (
36+
<p className={"flex items-center text-sm font-bold " + color}>
37+
<svg
38+
xmlns="http://www.w3.org/2000/svg"
39+
viewBox="0 0 20 20"
40+
fill="currentColor"
41+
className="mr-2 h-4 w-4"
42+
>
43+
<path
44+
fillRule="evenodd"
45+
d={icon}
46+
clipRule="evenodd"
47+
/>
48+
</svg>
49+
{children}
50+
</p>
51+
)}
52+
</>
53+
);
54+
}

Diff for: UI/SuccessMessage.js

-21
This file was deleted.

Diff for: components/ContactForm.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import SuccessMessage from '../UI/SuccessMessage';
2+
import Alert from '../UI/Alert';
33

44
export default function ContactForm() {
55
const nameRef = React.createRef();
@@ -32,6 +32,7 @@ export default function ContactForm() {
3232
}
3333

3434
// A contact form with a name, email, and message using tailwind css.
35+
// TODO: Add a better validation both UI, logic, and backend.
3536
return (
3637
<div id="contact" className="min-h-screen grid place-items-center content-center p-4">
3738
<div className="max-w-8xl w-full flex flex-wrap justify-center">
@@ -53,7 +54,7 @@ export default function ContactForm() {
5354
Message
5455
</label>
5556
<textarea required className="appearance-none block w-full bg-gray-200 h-40 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-message" type="text" placeholder="Lets get coffee." ref={messageRef} />
56-
{success && <SuccessMessage>Message sent!</SuccessMessage>}
57+
{success && <Alert type="success">Message sent!</Alert>}
5758
</div>
5859
</div>
5960
<div className="flex items-center justify-center">

Diff for: pages/api/contact.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import sendgrid from '@sendgrid/mail';
33
sendgrid.setApiKey(process.env.SENDGRID_API_KEY);
44

55
export default async function handler(req, res) {
6+
return res.status(200).json({
7+
statusCode: 200,
8+
body: 'Hello, world!',
9+
});
610
if (req.method === 'POST') {
711
const { name, email, message } = req.body;
812

0 commit comments

Comments
 (0)