-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentsForm.jsx
99 lines (88 loc) · 3.3 KB
/
CommentsForm.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { useState, useEffect, useRef } from 'react';
import { submitComment } from '../services';
const CommentsForm = ({ slug }) => {
const [error, setError] = useState(false);
const [localStorage, setLocalStorage] = useState(null);
const [showSuccessMessage, setShowSuccessMessage] = useState(false);
const commentEl = useRef();
const nameEl = useRef();
const emailEl = useRef();
const storeDataEl = useRef();
useEffect(() => {
nameEl.current.value = window.localStorage.getItem('name');
emailEl.current.value = window.localStorage.getItem('email');
}, [])
const handleCommentSubmission = () => {
setError(false);
const { value: comment } = commentEl.current;
const { value: name } = nameEl.current;
const { value: email } = emailEl.current;
const { checked: storeData } = storeDataEl.current;
if(!comment || !name || !email) {
setError(true);
return;
}
const commentObj = {name, email, comment, slug}
if(storeData) {
window.localStorage.setItem('name', name);
window.localStorage.setItem('email', email);
} else {
window.localStorage.removeItem('name');
window.localStorage.removeItem('email');
}
submitComment(commentObj)
.then((res) => {
setShowSuccessMessage(true);
setTimeout(() => {
setShowSuccessMessage(false);
}, 3000)
})
}
return (
<div className="bg-white shadow-lg rounded-lg p-8 pb-12 mb-8">
<h3 className='text-xl mb-8 font-semibold border-b pb-4'>Leave a reply</h3>
<div className="grid grid-cols-1 gap-4 mb-4">
<textarea
ref={commentEl}
className='p-4 outline-none w-full rounded-lg focus:ring-2 focus:ring-gray-200 bg-gray-100 text-gray-700'
placeholder='Comment'
name='comment'
/>
</div>
<div className="lg:grid-cols-2 grid grid-cols-1 gap-4 mb-4">
<input
type="text"
ref={nameEl}
className='py-2 px-4 outline-none w-full rounded-lg focus:ring-2 focus:ring-gray-200 bg-gray-100 text-gray-700'
placeholder='Name'
name='name'
/>
<input
type="text"
ref={emailEl}
className='py-2 px-4 outline-none w-full rounded-lg focus:ring-2 focus:ring-gray-200 bg-gray-100 text-gray-700'
placeholder='Email'
name='email'
/>
</div>
<div className="grid grid-cols-1 gap-4 mb-4">
<div className="container">
<input id='storeData' name='storeData' value={true} type="checkbox" ref={storeDataEl} />
<label htmlFor="storeData" className='text-gray-500 cursor-pointer ml-2'>Save my info for next time</label>
</div>
</div>
{error && <p className='text-xs text-red-500'>All fields are required</p>}
<div className="mt-8 ">
<button
type='button'
onClick={handleCommentSubmission}
className='transition duration-500 ease hover:bg-indigo-900 inline-block bg-pink-600 text-lg rounded-full text-white px-8 py-3 cursor-pointer'
>
Post Comment
</button>
{showSuccessMessage && <span className='text-xl float-right font-semibold mt-3 text-green-500'>Comment submitted for review</span>}
</div>
</div>
)
}
export default CommentsForm