From 469e8ef00c0310cf9be64dd18714946d93f0a74b Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Tue, 7 Jul 2020 02:49:14 +0200 Subject: [PATCH 1/2] Finish implementing React Hook Form --- components/Checkout/Billing.component.jsx | 299 +++++++++++------- .../Checkout/CheckoutForm.component.jsx | 78 ++--- components/Checkout/Error.component.jsx | 10 - package-lock.json | 2 +- utils/validator/checkoutValidator.js | 5 + 5 files changed, 214 insertions(+), 180 deletions(-) delete mode 100644 components/Checkout/Error.component.jsx diff --git a/components/Checkout/Billing.component.jsx b/components/Checkout/Billing.component.jsx index 56e126bbb..db88c5f1a 100644 --- a/components/Checkout/Billing.component.jsx +++ b/components/Checkout/Billing.component.jsx @@ -1,124 +1,197 @@ -import Error from './Error.component'; +import { useForm } from 'react-hook-form'; + import CheckoutTitle from 'components/Header/CheckoutTitle.component'; -const Billing = ({ input, handleOnChange }) => { - // https://react-hook-form.com/get-started#Quickstart +const Input = ({ + name, + label, + register, + placeholder, + value, + parameters, + type = 'text', + readOnly = false, +}) => ( + <> + + + +); + +const Billing = ({ onSubmit }) => { + const { register, handleSubmit, errors } = useForm(); + return ( <>
-
- -
-
-
- - - -
-
- - - -
-
- - - -
-
- - - -
-
- - - -
-
- - - -
-
- - - -
-
- -
-
- +
+
+ +
+
+
+ + {errors.firstName && ( + + FEIL: {errors.firstName.message} + + )} +
+
+ + {errors.lastName && ( + + FEIL: {errors.lastName.message} + + )} +
+
+ + {errors.address1 && ( + + FEIL: {errors.address1.message} + + )} +
+
+ + {errors.postcode && ( + + FEIL: {errors.postcode.message} + + )} +
+
+ + {errors.city && ( + + FEIL: {errors.city.message} + + )} +
+
+ + {errors.email && ( + + FEIL: {errors.email.message} + + )} +
+
+ + {errors.phone && ( + + FEIL: {errors.phone.message} + + )} +
+
+ +
+
+ +
-
+
); diff --git a/components/Checkout/CheckoutForm.component.jsx b/components/Checkout/CheckoutForm.component.jsx index a1653652a..14c0bfa8c 100644 --- a/components/Checkout/CheckoutForm.component.jsx +++ b/components/Checkout/CheckoutForm.component.jsx @@ -17,8 +17,6 @@ import { createCheckoutData, } from 'utils/functions/functions'; -import validateAndSanitizeCheckoutForm from 'utils/validator/checkoutValidator'; - const CheckoutForm = () => { const [cart, setCart] = useContext(AppContext); const [input, setInput] = useState(INITIAL_STATE); @@ -57,42 +55,6 @@ const CheckoutForm = () => { }, }); - /* - * Handle form submit. - * - * @param {Object} event Event Object. - * - * @return {void} - */ - const handleFormSubmit = (event) => { - event.preventDefault(); - const result = validateAndSanitizeCheckoutForm(input); - if (!result.isValid) { - setInput({ ...input, errors: result.errors }); - return; - } - const checkOutData = createCheckoutData(input); - setOrderData(checkOutData); - setRequestError(null); - }; - - /* - * Handle onChange input. - * - * @param {Object} event Event Object. - * - * @return {void} - */ - const handleOnChange = (event) => { - if ('createAccount' === event.target.name) { - const newState = { ...input, [event.target.name]: !input.createAccount }; - setInput(newState); - } else { - const newState = { ...input, [event.target.name]: event.target.value }; - setInput(newState); - } - }; - useEffect(() => { if (null !== orderData) { // Perform checkout mutation when the value for orderData changes. @@ -100,28 +62,32 @@ const CheckoutForm = () => { } }, [orderData]); + const onSubmit = (data) => { + const checkOutData = createCheckoutData(data); + setOrderData(checkOutData); + setRequestError(null); + }; + return ( <> {cart ? ( -
-
- {/* Order*/} - - {/*Payment Details*/} -
- -
- {/* Checkout Loading*/} - {checkoutLoading && ( -
- Behandler ordre, vennligst vent ... -
- -
- )} - {requestError} +
+ {/* Order*/} + + {/*Payment Details*/} +
+
- + {/* Checkout Loading*/} + {checkoutLoading && ( +
+ Behandler ordre, vennligst vent ... +
+ +
+ )} + {requestError} +
) : ( <> {orderCompleted && ( diff --git a/components/Checkout/Error.component.jsx b/components/Checkout/Error.component.jsx deleted file mode 100644 index f5a61ef40..000000000 --- a/components/Checkout/Error.component.jsx +++ /dev/null @@ -1,10 +0,0 @@ -const Error = ( { errors, fieldName } ) => { - - return( - errors && ( errors.hasOwnProperty( fieldName ) ) ? ( -
{ errors[fieldName] }
- ) : '' - ) -}; - -export default Error; diff --git a/package-lock.json b/package-lock.json index b15e7e3ac..46ce807af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nextjs-woocommerce", - "version": "1.0.0", + "version": "0.9.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/utils/validator/checkoutValidator.js b/utils/validator/checkoutValidator.js index f2c63b5ed..a33a22435 100644 --- a/utils/validator/checkoutValidator.js +++ b/utils/validator/checkoutValidator.js @@ -1,4 +1,9 @@ const validateAndSanitizeCheckoutForm = (data) => { + + console.log(data) + + + return { isValid: true, }; From 3e573084a394de4b3c87b1010cc4b841721daa17 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Tue, 7 Jul 2020 02:50:04 +0200 Subject: [PATCH 2/2] Add React Hook Form to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b0b0ac45f..db5eda9a0 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ - Cart handling and checkout with WooCommerce (Cash On Delivery only for now) - Algolia search - Apollo Client with GraphQL +- React Hook Form with form validation - Animations with React-Spring and Animate.css - Loading spinner created with Styled Components - Shows page load progress with Nprogress during navigation @@ -42,7 +43,6 @@ ## TODO - Display product variation name in cart / checkout -- Implement React-hook-form for validation - Hide products not in stock - Add better SEO - Add a better README.md