|
| 1 | +import { useState } from 'react'; |
| 2 | +import Error from './error'; |
| 3 | +import validateAndSanitizeCommentsForm from '../../validator/comments'; |
| 4 | + |
| 5 | +const CommentForm = () => { |
| 6 | + |
| 7 | + const [ input, setInput ] = useState( {} ); |
| 8 | + |
| 9 | + const handleFormSubmit = ( event ) => { |
| 10 | + event.preventDefault(); |
| 11 | + |
| 12 | + const commentFormValidationResult = validateAndSanitizeCommentsForm( input ); |
| 13 | + console.log( 'commentFormValidationResult', commentFormValidationResult, input ); |
| 14 | + |
| 15 | + setInput( { |
| 16 | + ...input, |
| 17 | + errors: commentFormValidationResult.errors, |
| 18 | + } ); |
| 19 | + |
| 20 | + // If there are any errors, return. |
| 21 | + if ( ! commentFormValidationResult.isValid ) { |
| 22 | + return null; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /* |
| 27 | + * Handle onchange input. |
| 28 | + * |
| 29 | + * @param {Object} event Event Object. |
| 30 | + * @param {bool} isShipping If this is false it means it is billing. |
| 31 | + * @param {bool} isBillingOrShipping If this is false means its standard input and not billing or shipping. |
| 32 | + * |
| 33 | + * @return {void} |
| 34 | + */ |
| 35 | + const handleOnChange = ( event ) => { |
| 36 | + const { target } = event || {}; |
| 37 | + |
| 38 | + const newState = { ...input, [ target.name ]: target.value }; |
| 39 | + setInput( newState ); |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <form action="/" noValidate onSubmit={ handleFormSubmit }> |
| 44 | + <h2>Leave a comment</h2> |
| 45 | + <p className="comment-notes"> |
| 46 | + <span id="email-notes">Your email address will not be published.</span> |
| 47 | + <span className="required-field-message">Required fields are marked <span className="required">*</span></span> |
| 48 | + </p> |
| 49 | + <div className="comment-form-comment mb-2"> |
| 50 | + <label htmlFor="comment" className="block mb-2 font-medium text-gray-900 dark:text-white"> |
| 51 | + Comment |
| 52 | + <span className="required">*</span> |
| 53 | + </label> |
| 54 | + <textarea |
| 55 | + id="comment" |
| 56 | + className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" |
| 57 | + name="comment" |
| 58 | + cols="45" |
| 59 | + rows="5" |
| 60 | + maxLength="65525" |
| 61 | + required |
| 62 | + value={ input?.comment ?? '' } |
| 63 | + onChange={ ( event ) => handleOnChange( event ) } |
| 64 | + /> |
| 65 | + <Error errors={ input?.errors ?? {} } fieldName="comment"/> |
| 66 | + </div> |
| 67 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-2"> |
| 68 | + <div className="comment-form-author"> |
| 69 | + <label htmlFor="author" className="block mb-2 font-medium text-gray-900 dark:text-white"> |
| 70 | + Name |
| 71 | + <span className="required">*</span> |
| 72 | + </label> |
| 73 | + <input |
| 74 | + id="author" |
| 75 | + className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 w-full block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" |
| 76 | + name="author" |
| 77 | + type="text" |
| 78 | + size="30" |
| 79 | + maxLength="245" |
| 80 | + autoComplete="name" |
| 81 | + required="" |
| 82 | + value={ input?.author ?? '' } |
| 83 | + onChange={ ( event ) => handleOnChange( event ) } |
| 84 | + /> |
| 85 | + <Error errors={ input?.errors ?? {} } fieldName="author"/> |
| 86 | + </div> |
| 87 | + <div className="comment-form-email border-0 mb-2"> |
| 88 | + <label htmlFor="email" className="block mb-2 font-medium text-gray-900 dark:text-white"> |
| 89 | + Email |
| 90 | + <span className="required">*</span> |
| 91 | + </label> |
| 92 | + <input |
| 93 | + id="email" |
| 94 | + className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 w-full block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" |
| 95 | + name="email" |
| 96 | + type="email" |
| 97 | + size="30" |
| 98 | + maxLength="100" |
| 99 | + aria-describedby="email-notes" |
| 100 | + autoComplete="email" |
| 101 | + required |
| 102 | + value={ input?.email ?? '' } |
| 103 | + onChange={ ( event ) => handleOnChange( event ) } |
| 104 | + /> |
| 105 | + <Error errors={ input?.errors ?? {} } fieldName="email"/> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + <div className="comment-form-url mb-2"> |
| 109 | + <label htmlFor="url" className="block mb-2 font-medium text-gray-900 dark:text-white"> |
| 110 | + Website |
| 111 | + </label> |
| 112 | + <input |
| 113 | + id="url" |
| 114 | + className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" |
| 115 | + name="url" |
| 116 | + type="url" |
| 117 | + size="30" |
| 118 | + maxLength="200" |
| 119 | + autoComplete="url" |
| 120 | + value={ input?.url ?? '' } |
| 121 | + onChange={ ( event ) => handleOnChange( event ) } |
| 122 | + /> |
| 123 | + <Error errors={ input?.errors ?? {} } fieldName="url"/> |
| 124 | + </div> |
| 125 | + <div className="comment-form-cookies-consent mb-4"> |
| 126 | + <input |
| 127 | + id="wp-comment-cookies-consent" |
| 128 | + name="wp_comment_cookies_consent" |
| 129 | + type="checkbox" |
| 130 | + value="yes" |
| 131 | + onChange={ ( event ) => handleOnChange( event ) } |
| 132 | + /> |
| 133 | + <label htmlFor="wp-comment-cookies-consent" className="mb-2 ml-2 font-medium text-gray-900 dark:text-white"> |
| 134 | + Save my name, email, and website in this browser for the next time I comment. |
| 135 | + </label> |
| 136 | + <Error errors={ input?.errors ?? {} } fieldName="wp_comment_cookies_consent"/> |
| 137 | + </div> |
| 138 | + <div className="form-submit"> |
| 139 | + <input name="submit" type="submit" id="submit" className=" cursor-pointer text-white bg-blue-700 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" value="Post Comment"/> |
| 140 | + <input type="hidden" name="comment_post_ID" value="377" id="comment_post_ID"/> |
| 141 | + <input type="hidden" name="comment_parent" id="comment_parent" value="0"/> |
| 142 | + </div> |
| 143 | + </form> |
| 144 | + ) |
| 145 | +} |
| 146 | + |
| 147 | +export default CommentForm; |
0 commit comments