Skip to content

Commit

Permalink
Merge pull request #280 from hyva-themes/277/payment-rest-api-hook
Browse files Browse the repository at this point in the history
#277 Implement a common hook to perform save payment using REST Api
  • Loading branch information
wigman committed Apr 6, 2022
2 parents 0050804 + 769742a commit 9b62b24
Show file tree
Hide file tree
Showing 47 changed files with 6,630 additions and 6,550 deletions.
12,885 changes: 6,424 additions & 6,461 deletions src/reactapp/package-lock.json

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions src/reactapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
"private": true,
"proxy": "https://demo.hyva.io",
"dependencies": {
"@heroicons/react": "^1.0.5",
"formik": "^2.2.6",
"@heroicons/react": "^1.0.6",
"formik": "^2.2.9",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"npm-run-all": "^4.1.5",
"preact": "^10.6.4",
"prop-types": "^15.7.2",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"preact": "^10.7.0",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-app-polyfill": "^3.0.0",
"react-dom": "^17.0.2",
"react-scripts": "^5.0.0",
"yup": "^0.32.11"
},
Expand Down Expand Up @@ -68,23 +68,23 @@
]
},
"devDependencies": {
"@babel/core": "^7.16.5",
"@babel/eslint-parser": "^7.16.5",
"@babel/preset-env": "^7.16.5",
"@babel/preset-react": "^7.16.5",
"@babel/core": "^7.17.8",
"@babel/eslint-parser": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@tailwindcss/forms": "^0.3.4",
"eslint": "^7.2.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint": "^8.12.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"husky": "^7.0.4",
"lint-staged": "^11.2.6",
"prettier": "^2.5.1",
"react-app-rewired": "^2.1.9",
"lint-staged": "^12.3.7",
"prettier": "^2.6.1",
"react-app-rewired": "^2.2.1",
"react-dev-utils": "^12.0.0",
"tailwindcss": "^2.2.19"
}
Expand Down
1 change: 1 addition & 0 deletions src/reactapp/src/api/sendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const storeCode = env.storeCode || RootElement.getStoreCode();

export default function sendRequest(
dispatch,
// eslint-disable-next-line default-param-last
queryParams = {},
relativeUrl,
responseType = 'json',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import _get from 'lodash.get';
import { useFormikContext } from 'formik';
import { bool, object, node, oneOfType } from 'prop-types';
Expand Down Expand Up @@ -81,7 +81,7 @@ function CheckoutFormWrapper({ initialData, children }) {
shippingAddressValues,
]);

return <>{children}</>;
return children;
}

CheckoutFormWrapper.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ AddressCard.propTypes = {

AddressCard.defaultProps = {
title: '',
billingSameCheckbox: <></>,
billingSameCheckbox: null,
};

export default AddressCard;
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const AddressWrapperMemorized = React.memo(({ children, formikData }) => {
setMostRecentAddressOptions(addressOptions);
}, [stateList]);

// eslint-disable-next-line react/jsx-no-constructed-context-values
const context = {
billingSelected,
setBillingSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function CreateNewAddressLink({ actions, forceHide }) {
const { isLoggedIn } = useAppContext();

if (!isLoggedIn || forceHide) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function SaveInBookCheckbox({ fields, formikData }) {
const saveInAddressBook = !!_get(formikData, 'formSectionValues.saveInBook');

if (!isLoggedIn || !customer?.hasAddress) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const BillingAddressMemorized = React.memo(({ formikData }) => {
<BillingAddressView />
</ToggleBox>
</Card>
) : (
<></>
)}
) : null}
</BillingAddressFormikProvider>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function BillingAddressForm() {
};

if (viewMode) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function BillingAddressFormikProvider({ children, formikData }) {
cartHasBillingAddress,
]);

// eslint-disable-next-line react/jsx-no-constructed-context-values
let context = {
...regionData,
...formikData,
Expand Down Expand Up @@ -168,8 +169,12 @@ function BillingAddressFormikProvider({ children, formikData }) {
}

BillingAddressFormikProvider.propTypes = {
children: node.isRequired,
children: node,
formikData: formikDataShape.isRequired,
};

BillingAddressFormikProvider.defaultProps = {
children: null,
};

export default BillingAddressFormikProvider;
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function BillingAddressOthers({ forceHide }) {
};

if (!isLoggedIn || forceHide) {
return <></>;
return null;
}

const submitButtonLabel = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function BillingAddressSelected() {
};

if (!isCartAddressValid(cartBillingAddress)) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function BillingAddressView() {
};

if (editMode) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function CancelButton() {
};

if (!isCartAddressValid(cartBillingAddress)) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ const CheckoutAgreementsMemorized = React.memo(({ formikData }) => {

return (
<CheckoutAgreementFormikProvider formikData={formikData}>
{_isObjEmpty(checkoutAgreements) ? (
<></>
) : (
{_isObjEmpty(checkoutAgreements) ? null : (
<CheckoutAgreementModalWrapper>
<Card>
<ToggleBox show title={__('Checkout Agreements')}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState, useMemo } from 'react';
import { node } from 'prop-types';

import CheckoutAgreementModal from './CheckoutAgreementModal';
Expand Down Expand Up @@ -38,7 +38,10 @@ function CheckoutAgreementModalWrapper({ children }) {
};
}, [activeModalId]);

const context = { activeModalId, setActiveModalId };
const context = useMemo(
() => ({ activeModalId, setActiveModalId }),
[activeModalId]
);

return (
<CheckoutAgreementsModalContext.Provider value={context}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { Form } from 'formik';
import React, { useEffect, useState, useMemo } from 'react';
import { node } from 'prop-types';
import { Form, FormikContext } from 'formik';

import {
prepareAgreementsFormData,
Expand Down Expand Up @@ -44,7 +44,10 @@ function CheckoutAgreementFormikProvider({ children, formikData }) {
id: CHECKOUT_AGREEMENTS_FORM,
});

const context = { ...formContext, ...formikData, formikData };
const context = useMemo(
() => ({ ...formContext, ...formikData, formikData }),
[formikData, FormikContext]
);

return (
<CheckoutAgreementsFormikContext.Provider value={context}>
Expand All @@ -54,8 +57,12 @@ function CheckoutAgreementFormikProvider({ children, formikData }) {
}

CheckoutAgreementFormikProvider.propTypes = {
children: node.isRequired,
children: node,
formikData: formikDataShape.isRequired,
};

CheckoutAgreementFormikProvider.defaultProps = {
children: null,
};

export default CheckoutAgreementFormikProvider;
2 changes: 1 addition & 1 deletion src/reactapp/src/components/common/Card/Card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Card.propTypes = {
Card.defaultProps = {
bg: '',
classes: '',
children: <></>,
children: null,
};

export default Card;
2 changes: 1 addition & 1 deletion src/reactapp/src/components/common/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Header.propTypes = {

Header.defaultProps = {
small: false,
extra: <></>,
extra: null,
};

export default Header;
2 changes: 1 addition & 1 deletion src/reactapp/src/components/common/Message/Message.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Message() {
}, [message, msgType, setMessage]);

if (!message) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { Form } from 'formik';
import { node } from 'prop-types';
import { string as YupString } from 'yup';
Expand All @@ -19,18 +19,21 @@ const validationSchema = {
function CouponCodeFormikManager({ children, formikData }) {
const formSubmit = () => {};

const context = useFormSection({
const formContext = useFormSection({
formikData,
initialValues,
validationSchema,
id: COUPON_CODE_FORM,
submitHandler: formSubmit,
});

const context = useMemo(
() => ({ ...formContext, ...formikData, formikData }),
[formContext, formikData]
);

return (
<CouponCodeFormikContext.Provider
value={{ ...context, ...formikData, formikData }}
>
<CouponCodeFormikContext.Provider value={context}>
<Form id={COUPON_CODE_FORM}>{children}</Form>
</CouponCodeFormikContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function CartItemsFormManager({ children, formikData }) {
submitHandler: formSubmit,
});

// eslint-disable-next-line react/jsx-no-constructed-context-values
const context = {
...formSectionContext,
...formikData,
Expand Down
2 changes: 1 addition & 1 deletion src/reactapp/src/components/login/components/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function LoginForm() {
});

if (!editMode) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function LoginFormManager({ children, formikData }) {
}
}, [cartEmail, setFormToViewMode]);

// eslint-disable-next-line react/jsx-no-constructed-context-values
const context = {
...formikData,
...formSectionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function LoginInfoBox() {
const customerWantsToSignIn = _get(loginFormValues, 'customerWantsToSignIn');

if (!editMode) {
return <></>;
return null;
}

if (customerWantsToSignIn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function UserInfoBox() {
const customerEmail = _get(customer, 'email', '');

if (editMode) {
return <></>;
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { Form } from 'formik';
import { node } from 'prop-types';
import { string as YupString } from 'yup';
Expand Down Expand Up @@ -49,18 +49,21 @@ function PaymentMethodFormManager({ children, formikData }) {
}
};

const context = useFormSection({
const formContext = useFormSection({
formikData,
initialValues,
validationSchema,
id: PAYMENT_METHOD_FORM,
submitHandler: formSubmit,
});

const context = useMemo(
() => ({ ...formContext, ...formikData, formikData }),
[formContext, formikData]
);

return (
<PaymentMethodFormContext.Provider
value={{ ...context, ...formikData, formikData }}
>
<PaymentMethodFormContext.Provider value={context}>
<Form id={PAYMENT_METHOD_FORM}>{children}</Form>
</PaymentMethodFormContext.Provider>
);
Expand Down
Loading

0 comments on commit 9b62b24

Please sign in to comment.