|
| 1 | +/** |
| 2 | + * External Dependencies. |
| 3 | + */ |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | +import cx from 'classnames'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Internal Dependencies. |
| 9 | + */ |
| 10 | +import validateAndSanitizeCommentsForm from '../../validator/comments'; |
| 11 | +import TextArea from '../form-elements/text-area'; |
| 12 | +import Input from '../form-elements/input'; |
| 13 | +import { postComment } from '../../utils/blog'; |
| 14 | +import { sanitize } from '../../utils/miscellaneous'; |
| 15 | +import Loading from '../loading'; |
| 16 | + |
| 17 | +const CommentForm = ( { postId, replyCommentID } ) => { |
| 18 | + |
| 19 | + /** |
| 20 | + * Initialize Input State. |
| 21 | + * |
| 22 | + * @type {{date: Date, postId: number, wp_comment_cookies_consent: boolean}} |
| 23 | + */ |
| 24 | + const initialInputState = { |
| 25 | + postId: postId || 0, |
| 26 | + date: new Date(), |
| 27 | + parent: replyCommentID || 0, |
| 28 | + } |
| 29 | + |
| 30 | + const [ input, setInput ] = useState( initialInputState ); |
| 31 | + const [ commentPostSuccess, setCommentPostSuccess ] = useState( false ); |
| 32 | + const [ commentPostError, setCommentPostError ] = useState( '' ); |
| 33 | + const [ clearFormValues, setClearFormValues ] = useState( false ); |
| 34 | + const [ loading, setLoading ] = useState( false ); |
| 35 | + const submitBtnClasses = cx( |
| 36 | + 'text-white hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-16px uppercase w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800', |
| 37 | + { |
| 38 | + 'cursor-pointer bg-blue-700': ! loading, |
| 39 | + 'bg-blue-400 dark:bg-blue-500 cursor-not-allowed': loading, |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + /** |
| 44 | + * When the reply Comment id gets updated |
| 45 | + * then update the input. |
| 46 | + */ |
| 47 | + useEffect( () => { |
| 48 | + setInput( { |
| 49 | + ...input, |
| 50 | + parent: replyCommentID || 0, |
| 51 | + } ); |
| 52 | + }, [ replyCommentID ] ); |
| 53 | + |
| 54 | + /** |
| 55 | + * If 'clearFormValues' becomes true, |
| 56 | + * reset the input value to initialInputState |
| 57 | + */ |
| 58 | + useEffect( () => { |
| 59 | + if ( clearFormValues ) { |
| 60 | + setInput( initialInputState ); |
| 61 | + } |
| 62 | + }, [ clearFormValues ] ); |
| 63 | + |
| 64 | + /** |
| 65 | + * If 'commentPostSuccess' is set to true, set to false after |
| 66 | + * few seconds so the message disappears. |
| 67 | + */ |
| 68 | + useEffect( () => { |
| 69 | + if ( commentPostSuccess ) { |
| 70 | + const intervalId = setTimeout( () => { |
| 71 | + setCommentPostSuccess( false ) |
| 72 | + }, 10000 ) |
| 73 | + |
| 74 | + // Unsubscribe from the interval. |
| 75 | + return () => { |
| 76 | + clearInterval( intervalId ); |
| 77 | + }; |
| 78 | + } |
| 79 | + }, [ commentPostSuccess ] ) |
| 80 | + |
| 81 | + /** |
| 82 | + * Handle form submit. |
| 83 | + * |
| 84 | + * @param {Event} event Event. |
| 85 | + * |
| 86 | + * @return {null} |
| 87 | + */ |
| 88 | + const handleFormSubmit = ( event ) => { |
| 89 | + event.preventDefault(); |
| 90 | + |
| 91 | + const commentFormValidationResult = validateAndSanitizeCommentsForm( input ); |
| 92 | + |
| 93 | + setInput( { |
| 94 | + ...input, |
| 95 | + errors: commentFormValidationResult.errors, |
| 96 | + } ); |
| 97 | + |
| 98 | + // If there are any errors, return. |
| 99 | + if ( ! commentFormValidationResult.isValid ) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + // Set loading to true. |
| 104 | + setLoading( true ); |
| 105 | + |
| 106 | + // Make a POST request to post comment. |
| 107 | + const response = postComment( 377, input ); |
| 108 | + |
| 109 | + /** |
| 110 | + * The postComment() returns a promise, |
| 111 | + * When the promise gets resolved, i.e. request is complete, |
| 112 | + * then handle the success or error messages. |
| 113 | + */ |
| 114 | + response.then(( res ) => { |
| 115 | + setLoading( false ); |
| 116 | + if ( res.success ) { |
| 117 | + setCommentPostSuccess( true ); |
| 118 | + setClearFormValues( true ); |
| 119 | + } else { |
| 120 | + setCommentPostError( res.error ?? 'Something went wrong. Please try again' ); |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + /* |
| 126 | + * Handle onchange input. |
| 127 | + * |
| 128 | + * @param {Object} event Event Object. |
| 129 | + * |
| 130 | + * @return {void} |
| 131 | + */ |
| 132 | + const handleOnChange = ( event ) => { |
| 133 | + |
| 134 | + // Reset the comment post success and error messages, first. |
| 135 | + if ( commentPostSuccess ) { |
| 136 | + setCommentPostSuccess( false ); |
| 137 | + } |
| 138 | + if ( commentPostError ) { |
| 139 | + setCommentPostError( '' ); |
| 140 | + } |
| 141 | + |
| 142 | + const { target } = event || {}; |
| 143 | + const newState = { ...input, [ target.name ]: target.value }; |
| 144 | + setInput( newState ); |
| 145 | + }; |
| 146 | + |
| 147 | + return ( |
| 148 | + <form action="/" noValidate onSubmit={ handleFormSubmit } id="comment-form"> |
| 149 | + <h2>Leave a comment</h2> |
| 150 | + <p className="comment-notes"> |
| 151 | + <span id="email-notes">Your email address will not be published.</span> |
| 152 | + <span className="required-field-message">Required fields are marked <span className="required">*</span></span> |
| 153 | + </p> |
| 154 | + <TextArea |
| 155 | + id="comment" |
| 156 | + containerClassNames="comment-form-comment mb-2" |
| 157 | + name="comment" |
| 158 | + label="Comment" |
| 159 | + cols="45" |
| 160 | + rows="5" |
| 161 | + required |
| 162 | + textAreaValue={ input?.comment ?? '' } |
| 163 | + handleOnChange={ handleOnChange } |
| 164 | + errors={ input?.errors ?? {} } |
| 165 | + /> |
| 166 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2"> |
| 167 | + <Input |
| 168 | + name="author" |
| 169 | + inputValue={ input?.author } |
| 170 | + required |
| 171 | + handleOnChange={ handleOnChange } |
| 172 | + label="Name" |
| 173 | + errors={ input?.errors ?? {} } |
| 174 | + containerClassNames="comment-form-author" |
| 175 | + /> |
| 176 | + <Input |
| 177 | + name="email" |
| 178 | + inputValue={ input?.email } |
| 179 | + required |
| 180 | + handleOnChange={ handleOnChange } |
| 181 | + label="Email" |
| 182 | + errors={ input?.errors ?? {} } |
| 183 | + containerClassNames="comment-form-email mb-2" |
| 184 | + /> |
| 185 | + </div> |
| 186 | + <Input |
| 187 | + name="url" |
| 188 | + inputValue={ input?.url ?? '' } |
| 189 | + handleOnChange={ handleOnChange } |
| 190 | + label="Website" |
| 191 | + errors={ input?.errors ?? {} } |
| 192 | + containerClassNames="comment-form-url mb-2" |
| 193 | + /> |
| 194 | + <div className="form-submit py-4"> |
| 195 | + <input |
| 196 | + name="submit" |
| 197 | + type="submit" |
| 198 | + id="submit" |
| 199 | + className={ submitBtnClasses } |
| 200 | + value="Post Comment" |
| 201 | + disabled={ loading } |
| 202 | + /> |
| 203 | + </div> |
| 204 | + { |
| 205 | + commentPostSuccess && ! loading ? |
| 206 | + ( |
| 207 | + <div |
| 208 | + className="p-4 mb-4 text-sm text-green-800 rounded-lg bg-green-50 dark:bg-gray-800 dark:text-green-400" |
| 209 | + role="alert"> |
| 210 | + <span className="font-medium">Success!</span> Your comment has been submitted for approval. |
| 211 | + It will be posted after admin's approval. |
| 212 | + </div> |
| 213 | + ) : null |
| 214 | + } |
| 215 | + { |
| 216 | + commentPostError && ! loading ? |
| 217 | + ( |
| 218 | + <div |
| 219 | + className="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400" |
| 220 | + role="alert"> |
| 221 | + <span className="font-medium">Error! </span> |
| 222 | + <div className="inline-block" dangerouslySetInnerHTML={ { __html: sanitize( commentPostError ) } } /> |
| 223 | + </div> |
| 224 | + ) : null |
| 225 | + } |
| 226 | + { |
| 227 | + loading ? <Loading text="Processing..."/>: null |
| 228 | + } |
| 229 | + </form> |
| 230 | + ) |
| 231 | +} |
| 232 | + |
| 233 | +export default CommentForm; |
0 commit comments