Skip to content

Commit d689d27

Browse files
committed
React 18 useTransition and Create Contact Form
1 parent 15f78aa commit d689d27

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

app/connect/email-list/form.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use client';
2+
3+
import { useState, useTransition } from 'react';
4+
import postContact from './post-contact';
5+
import Spinner from './spinner';
6+
7+
export default function Form() {
8+
const [message, setMessage] = useState('');
9+
const [displayMessage, setDisplayMessage] = useState(false);
10+
const [isPending, startTransition] = useTransition();
11+
12+
const handleSubmit = (data) => {
13+
const name = data.get('name');
14+
const email = data.get('email');
15+
16+
startTransition(() => {
17+
let message;
18+
19+
postContact({ name, email })
20+
.then(json => {
21+
console.log(`json`, json);
22+
23+
const errorMessage = json.ErrorMessage;
24+
25+
if (json.Count > 0) {
26+
message = 'Success! Keep an eye out on your inbox updates';
27+
} else if (errorMessage && errorMessage.includes('already exists')) {
28+
message = 'Email is already subscribed';
29+
} else if (errorMessage && errorMessage.includes('properties invalid')) {
30+
message = 'Email is not formatted correctly';
31+
} else {
32+
message = 'Error. Try again or email [email protected] to be added manually.'
33+
}
34+
35+
setMessage(message);
36+
setDisplayMessage(true);
37+
});
38+
});
39+
}
40+
41+
return (
42+
<form action={handleSubmit} className="indent grid gap-[2px] w-[270px]">
43+
<label htmlFor="name">Name</label>
44+
<input type="text" id="name" name="name" required />
45+
<label htmlFor="email">Email</label>
46+
<input type="email" id="email" name="email" required />
47+
<button
48+
type="submit"
49+
disabled={isPending}
50+
className={
51+
`bg-blue-700 text-center text-white w-[130px] h-[36px] mt-[4px]
52+
disabled:bg-slate-50 disabled:text-slate-500`
53+
}
54+
>
55+
{
56+
isPending
57+
? (
58+
<span className="flex justify-center items-center h-[36px]">
59+
<Spinner /> Sending
60+
</span>
61+
)
62+
: 'Submit'
63+
}
64+
</button>
65+
{displayMessage ? message : <></> }
66+
</form>
67+
)
68+
}

app/connect/email-list/spinner.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function Spinner() {
2+
return (
3+
<svg className="animate-spin -ml-1 mr-3 h-5 w-5 text-slate-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
4+
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
5+
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
6+
</svg>
7+
)
8+
}

app/connect/followers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function Followers() {
1616
}, []);
1717

1818
return (
19-
<div className="ml-[15px] lg:ml-[20px] mt-[5px]">
19+
<div className="indent">
2020
Follower progress: {goalString}
2121
</div>
2222
)

app/connect/page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import Form from './email-list/form';
1+
import Form from './email-list/form';
22
import Followers from './followers';
33

44
export default function Page() {
@@ -30,7 +30,7 @@ export default function Page() {
3030
<div>
3131
3. Join my email list.
3232
</div>
33-
{/* <Form /> */}
33+
<Form />
3434
<br />
3535
<div>
3636
Reach out for inquiries at <span className="underline">[email protected]</span>

app/globals.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
.link {
99
@apply underline text-blue-800
1010
}
11+
.indent {
12+
@apply ml-[15px] lg:ml-[20px] mt-[5px]
13+
}

0 commit comments

Comments
 (0)