@commercelayer/react-components 4.12.0-beta.1 → 4.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/gift_cards/GiftCardOrCouponForm.js +1 -1
- package/lib/cjs/components/orders/PlaceOrderButton.d.ts +4 -0
- package/lib/cjs/components/orders/PlaceOrderButton.js +1 -1
- package/lib/esm/components/gift_cards/GiftCardOrCouponForm.js +1 -1
- package/lib/esm/components/orders/PlaceOrderButton.d.ts +4 -0
- package/lib/esm/components/orders/PlaceOrderButton.js +1 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponForm=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),rapid_form_1=require("rapid-form"),react_1=require("react"),CouponAndGiftCardFormContext_1=tslib_1.__importDefault(require("../../context/CouponAndGiftCardFormContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext"));function GiftCardOrCouponForm(props){const{children,codeType
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GiftCardOrCouponForm=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),rapid_form_1=require("rapid-form"),react_1=require("react"),CouponAndGiftCardFormContext_1=tslib_1.__importDefault(require("../../context/CouponAndGiftCardFormContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext"));function GiftCardOrCouponForm(props){const{children,codeType,autoComplete="on",onSubmit}=props,p=tslib_1.__rest(props,["children","codeType","autoComplete","onSubmit"]),{validation,values,reset}=(0,rapid_form_1.useRapidForm)(),{setGiftCardOrCouponCode,order,errors,setOrderErrors}=(0,react_1.useContext)(OrderContext_1.default),ref=(0,react_1.useRef)(null),[type,setType]=(0,react_1.useState)(codeType);(0,react_1.useEffect)(()=>{var _a,_b,_c,_d;if(type!=null&&((_a=values[type])===null||_a===void 0?void 0:_a.value)===""&&errors!=null&&errors.length>0){const err=errors.filter(e=>e.field===type);setOrderErrors(err),onSubmit&&onSubmit({value:(_b=values[type])===null||_b===void 0?void 0:_b.value,success:!1})}type!=null&&((_c=values[type])===null||_c===void 0?void 0:_c.value)===""&&(setOrderErrors([]),onSubmit&&onSubmit({value:(_d=values[type])===null||_d===void 0?void 0:_d.value,success:!1}))},[values]),(0,react_1.useEffect)(()=>{codeType==null?(order?.gift_card_code&&!order?.coupon_code&&setType("coupon_code"),!order?.gift_card_code&&order?.coupon_code&&setType("gift_card_code"),!order?.gift_card_code&&!order?.coupon_code&&setType("gift_card_or_coupon_code")):setType(codeType)},[order]);const handleSubmit=e=>tslib_1.__awaiter(this,void 0,void 0,function*(){var _a;e.preventDefault();const code=type!=null&&values[type]!=null?values[type].value:void 0;if(code!=null&&setGiftCardOrCouponCode!=null&&type!=null){const{success,order:order2}=yield setGiftCardOrCouponCode({code,codeType:type}),value=(_a=values[type])===null||_a===void 0?void 0:_a.value;onSubmit&&onSubmit({success,value,order:order2}),success&&reset(e)}});return codeType!=null&&order?.[codeType]!=null&&order?.[codeType]!==""||order?.gift_card_code&&order?.coupon_code||order==null?null:(0,jsx_runtime_1.jsx)(CouponAndGiftCardFormContext_1.default.Provider,{value:{validation,codeType:type},children:(0,jsx_runtime_1.jsx)("form",Object.assign({ref,autoComplete,onSubmit:e=>{handleSubmit(e)}},p,{children}))})}exports.GiftCardOrCouponForm=GiftCardOrCouponForm,exports.default=GiftCardOrCouponForm;
|
|
@@ -14,6 +14,10 @@ interface Props extends Omit<JSX.IntrinsicElements['button'], 'children' | 'onCl
|
|
|
14
14
|
* The label of the button
|
|
15
15
|
*/
|
|
16
16
|
label?: string | ReactNode;
|
|
17
|
+
/**
|
|
18
|
+
* The label of the button when it's loading
|
|
19
|
+
*/
|
|
20
|
+
loadingLabel?: string | ReactNode;
|
|
17
21
|
/**
|
|
18
22
|
* If false, the button doesn't place the order automatically. Default: true
|
|
19
23
|
*/
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PlaceOrderButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),PlaceOrderContext_1=tslib_1.__importDefault(require("../../context/PlaceOrderContext")),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction")),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),getCardDetails_1=tslib_1.__importDefault(require("../../utils/getCardDetails"));function PlaceOrderButton(props){var _a,_b,_c,_d;const ref=(0,react_1.useRef)(null),{children,label="Place order",autoPlaceOrder=!0,disabled,onClick}=props,p=tslib_1.__rest(props,["children","label","autoPlaceOrder","disabled","onClick"]),{isPermitted,setPlaceOrder,options,paymentType,setButtonRef}=(0,react_1.useContext)(PlaceOrderContext_1.default),[notPermitted,setNotPermitted]=(0,react_1.useState)(!0),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled),{currentPaymentMethodRef,loading,currentPaymentMethodType,paymentSource,setPaymentSource,setPaymentMethodErrors,currentCustomerPaymentSourceId}=(0,react_1.useContext)(PaymentMethodContext_1.default),{order}=(0,react_1.useContext)(OrderContext_1.default),isFree=order?.total_amount_with_taxes_cents===0;(0,react_1.useEffect)(()=>{var _a2;if(loading)setNotPermitted(loading);else if(paymentType===currentPaymentMethodType&&paymentType){const card=(0,getCardDetails_1.default)({customerPayment:{payment_source:paymentSource},paymentType});currentCustomerPaymentSourceId!=null&&paymentSource?.id===currentCustomerPaymentSourceId&&card.brand===""&&(card.brand="credit-card"),(isFree&&isPermitted||!((_a2=currentPaymentMethodRef?.current)===null||_a2===void 0)&&_a2.onsubmit||card.brand)&&isPermitted&&setNotPermitted(!1)}else setNotPermitted(!(isFree&&isPermitted));return()=>{setNotPermitted(!0)}},[isPermitted,paymentType,(_a=currentPaymentMethodRef?.current)===null||_a===void 0?void 0:_a.onsubmit,loading,currentPaymentMethodType,order,paymentSource]),(0,react_1.useEffect)(()=>{paymentType==="paypal_payments"&&options?.paypalPayerId&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.paypalPayerId,paymentType]),(0,react_1.useEffect)(()=>{var _a2,_b2;paymentType==="stripe_payments"&&["succeeded","pending"].includes((_b2=(_a2=options?.stripe)===null||_a2===void 0?void 0:_a2.redirectStatus)!==null&&_b2!==void 0?_b2:"")&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[(_b=options?.stripe)===null||_b===void 0?void 0:_b.redirectStatus,paymentType]),(0,react_1.useEffect)(()=>{var _a2,_b2,_c2,_d2,_e,_f,_g;if(order?.status!=null&&["draft","pending"].includes(order?.status))if(paymentType==="adyen_payments"&&(!((_a2=options?.adyen)===null||_a2===void 0)&&_a2.redirectResult)&&paymentSource!=null){const attributes={payment_request_details:{details:{redirectResult:(_b2=options?.adyen)===null||_b2===void 0?void 0:_b2.redirectResult}},_details:1};setPaymentSource({paymentSourceId:paymentSource?.id,paymentResource:"adyen_payments",attributes}).then(res=>{var _a3,_b3,_c3;const resultCode=(_a3=res?.payment_response)===null||_a3===void 0?void 0:_a3.resultCode,errorCode=(_b3=res?.payment_response)===null||_b3===void 0?void 0:_b3.errorCode,message=(_c3=res?.payment_response)===null||_c3===void 0?void 0:_c3.message;["Authorised","Pending","Received"].includes(resultCode)&&autoPlaceOrder?handleClick():errorCode!=null&&setPaymentMethodErrors([{code:"PAYMENT_INTENT_AUTHENTICATION_FAILURE",resource:"payment_methods",field:currentPaymentMethodType,message}])})}else(paymentType==="adyen_payments"&&(!((_c2=options?.adyen)===null||_c2===void 0)&&_c2.MD)&&(!((_d2=options?.adyen)===null||_d2===void 0)&&_d2.PaRes)&&autoPlaceOrder||paymentType==="adyen_payments"&&((_f=(_e=order?.payment_source)===null||_e===void 0?void 0:_e.payment_response)===null||_f===void 0?void 0:_f.resultCode)==="Authorised"&&((_g=ref?.current)===null||_g===void 0?void 0:_g.disabled)===!1&¤tCustomerPaymentSourceId==null&&autoPlaceOrder)&&handleClick()},[options?.adyen,paymentType,(_d=(_c=order?.payment_source)===null||_c===void 0?void 0:_c.payment_response)===null||_d===void 0?void 0:_d.resultCode]),(0,react_1.useEffect)(()=>{var _a2;paymentType==="checkout_com_payments"&&(!((_a2=options?.checkoutCom)===null||_a2===void 0)&&_a2.session_id)&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.checkoutCom,paymentType]),(0,react_1.useEffect)(()=>{ref?.current!=null&&setButtonRef!=null&&setButtonRef(ref)},[ref]);const handleClick=e=>tslib_1.__awaiter(this,void 0,void 0,function*(){var _e,_f,_g,_h,_j;e?.preventDefault(),e?.stopPropagation();let isValid=!0;setForceDisable(!0);const checkPaymentSource=yield setPaymentSource({paymentResource:paymentType,paymentSourceId:paymentSource?.id}),card=paymentType&&(0,getCardDetails_1.default)({paymentType,customerPayment:{payment_source:checkPaymentSource}});!((_e=currentPaymentMethodRef?.current)===null||_e===void 0)&&_e.onsubmit&&[!options?.paypalPayerId,!(!((_f=options?.adyen)===null||_f===void 0)&&_f.MD),!(!((_g=options?.checkoutCom)===null||_g===void 0)&&_g.session_id)].every(Boolean)?(isValid=yield(_h=currentPaymentMethodRef.current)===null||_h===void 0?void 0:_h.onsubmit({paymentSource:checkPaymentSource,setPlaceOrder,onclickCallback:onClick}),!isValid&&((_j=checkPaymentSource.payment_response)===null||_j===void 0?void 0:_j.resultCode)==="Authorised"&&(isValid=!0)):card?.brand&&(isValid=!0);const placed=isValid&&setPlaceOrder&&(checkPaymentSource||isFree)&&(yield setPlaceOrder({paymentSource:checkPaymentSource,currentCustomerPaymentSourceId}));setForceDisable(!1),onClick&&placed&&onClick(placed)}),disabledButton=disabled!==void 0?disabled:notPermitted,parentProps=Object.assign(Object.assign({},p),{label,disabled:disabledButton,handleClick,ref});return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("button",Object.assign({ref,type:"button",disabled:disabledButton||forceDisable,onClick:e=>{handleClick(e)}},p,{children:
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PlaceOrderButton=void 0;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),PlaceOrderContext_1=tslib_1.__importDefault(require("../../context/PlaceOrderContext")),isFunction_1=tslib_1.__importDefault(require("lodash/isFunction")),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),getCardDetails_1=tslib_1.__importDefault(require("../../utils/getCardDetails"));function PlaceOrderButton(props){var _a,_b,_c,_d;const ref=(0,react_1.useRef)(null),{children,label="Place order",loadingLabel="Placing...",autoPlaceOrder=!0,disabled,onClick}=props,p=tslib_1.__rest(props,["children","label","loadingLabel","autoPlaceOrder","disabled","onClick"]),{isPermitted,setPlaceOrder,options,paymentType,setButtonRef}=(0,react_1.useContext)(PlaceOrderContext_1.default),[notPermitted,setNotPermitted]=(0,react_1.useState)(!0),[forceDisable,setForceDisable]=(0,react_1.useState)(disabled),[isLoading,setIsLoading]=(0,react_1.useState)(!1),{currentPaymentMethodRef,loading,currentPaymentMethodType,paymentSource,setPaymentSource,setPaymentMethodErrors,currentCustomerPaymentSourceId}=(0,react_1.useContext)(PaymentMethodContext_1.default),{order}=(0,react_1.useContext)(OrderContext_1.default),isFree=order?.total_amount_with_taxes_cents===0;(0,react_1.useEffect)(()=>{var _a2;if(loading)setNotPermitted(loading);else if(paymentType===currentPaymentMethodType&&paymentType){const card=(0,getCardDetails_1.default)({customerPayment:{payment_source:paymentSource},paymentType});currentCustomerPaymentSourceId!=null&&paymentSource?.id===currentCustomerPaymentSourceId&&card.brand===""&&(card.brand="credit-card"),(isFree&&isPermitted||!((_a2=currentPaymentMethodRef?.current)===null||_a2===void 0)&&_a2.onsubmit||card.brand)&&isPermitted&&setNotPermitted(!1)}else setNotPermitted(!(isFree&&isPermitted));return()=>{setNotPermitted(!0)}},[isPermitted,paymentType,(_a=currentPaymentMethodRef?.current)===null||_a===void 0?void 0:_a.onsubmit,loading,currentPaymentMethodType,order,paymentSource]),(0,react_1.useEffect)(()=>{paymentType==="paypal_payments"&&options?.paypalPayerId&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.paypalPayerId,paymentType]),(0,react_1.useEffect)(()=>{var _a2,_b2;paymentType==="stripe_payments"&&["succeeded","pending"].includes((_b2=(_a2=options?.stripe)===null||_a2===void 0?void 0:_a2.redirectStatus)!==null&&_b2!==void 0?_b2:"")&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[(_b=options?.stripe)===null||_b===void 0?void 0:_b.redirectStatus,paymentType]),(0,react_1.useEffect)(()=>{var _a2,_b2,_c2,_d2,_e,_f,_g;if(order?.status!=null&&["draft","pending"].includes(order?.status))if(paymentType==="adyen_payments"&&(!((_a2=options?.adyen)===null||_a2===void 0)&&_a2.redirectResult)&&paymentSource!=null){const attributes={payment_request_details:{details:{redirectResult:(_b2=options?.adyen)===null||_b2===void 0?void 0:_b2.redirectResult}},_details:1};setPaymentSource({paymentSourceId:paymentSource?.id,paymentResource:"adyen_payments",attributes}).then(res=>{var _a3,_b3,_c3;const resultCode=(_a3=res?.payment_response)===null||_a3===void 0?void 0:_a3.resultCode,errorCode=(_b3=res?.payment_response)===null||_b3===void 0?void 0:_b3.errorCode,message=(_c3=res?.payment_response)===null||_c3===void 0?void 0:_c3.message;["Authorised","Pending","Received"].includes(resultCode)&&autoPlaceOrder?handleClick():errorCode!=null&&setPaymentMethodErrors([{code:"PAYMENT_INTENT_AUTHENTICATION_FAILURE",resource:"payment_methods",field:currentPaymentMethodType,message}])})}else(paymentType==="adyen_payments"&&(!((_c2=options?.adyen)===null||_c2===void 0)&&_c2.MD)&&(!((_d2=options?.adyen)===null||_d2===void 0)&&_d2.PaRes)&&autoPlaceOrder||paymentType==="adyen_payments"&&((_f=(_e=order?.payment_source)===null||_e===void 0?void 0:_e.payment_response)===null||_f===void 0?void 0:_f.resultCode)==="Authorised"&&((_g=ref?.current)===null||_g===void 0?void 0:_g.disabled)===!1&¤tCustomerPaymentSourceId==null&&autoPlaceOrder)&&handleClick()},[options?.adyen,paymentType,(_d=(_c=order?.payment_source)===null||_c===void 0?void 0:_c.payment_response)===null||_d===void 0?void 0:_d.resultCode]),(0,react_1.useEffect)(()=>{var _a2;paymentType==="checkout_com_payments"&&(!((_a2=options?.checkoutCom)===null||_a2===void 0)&&_a2.session_id)&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.checkoutCom,paymentType]),(0,react_1.useEffect)(()=>{ref?.current!=null&&setButtonRef!=null&&setButtonRef(ref)},[ref]);const handleClick=e=>tslib_1.__awaiter(this,void 0,void 0,function*(){var _e,_f,_g,_h,_j;e?.preventDefault(),e?.stopPropagation(),setIsLoading(!0);let isValid=!0;setForceDisable(!0);const checkPaymentSource=yield setPaymentSource({paymentResource:paymentType,paymentSourceId:paymentSource?.id}),card=paymentType&&(0,getCardDetails_1.default)({paymentType,customerPayment:{payment_source:checkPaymentSource}});!((_e=currentPaymentMethodRef?.current)===null||_e===void 0)&&_e.onsubmit&&[!options?.paypalPayerId,!(!((_f=options?.adyen)===null||_f===void 0)&&_f.MD),!(!((_g=options?.checkoutCom)===null||_g===void 0)&&_g.session_id)].every(Boolean)?(isValid=yield(_h=currentPaymentMethodRef.current)===null||_h===void 0?void 0:_h.onsubmit({paymentSource:checkPaymentSource,setPlaceOrder,onclickCallback:onClick}),!isValid&&((_j=checkPaymentSource.payment_response)===null||_j===void 0?void 0:_j.resultCode)==="Authorised"&&(isValid=!0)):card?.brand&&(isValid=!0);const placed=isValid&&setPlaceOrder&&(checkPaymentSource||isFree)&&(yield setPlaceOrder({paymentSource:checkPaymentSource,currentCustomerPaymentSourceId}));setForceDisable(!1),onClick&&placed&&onClick(placed),setIsLoading(!1)}),disabledButton=disabled!==void 0?disabled:notPermitted,labelButton=isLoading?loadingLabel:(0,isFunction_1.default)(label)?label():label,parentProps=Object.assign(Object.assign({},p),{label,disabled:disabledButton,handleClick,parentRef:ref,isLoading});return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("button",Object.assign({ref,type:"button",disabled:disabledButton||forceDisable,onClick:e=>{handleClick(e)}},p,{children:labelButton}))}exports.PlaceOrderButton=PlaceOrderButton,exports.default=PlaceOrderButton;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useRapidForm}from"rapid-form";import{useContext,useEffect,useRef,useState}from"react";import CouponAndGiftCardFormContext from"../../context/CouponAndGiftCardFormContext";import OrderContext from"../../context/OrderContext";export function GiftCardOrCouponForm(props){const{children,codeType
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useRapidForm}from"rapid-form";import{useContext,useEffect,useRef,useState}from"react";import CouponAndGiftCardFormContext from"../../context/CouponAndGiftCardFormContext";import OrderContext from"../../context/OrderContext";export function GiftCardOrCouponForm(props){const{children,codeType,autoComplete="on",onSubmit,...p}=props,{validation,values,reset}=useRapidForm(),{setGiftCardOrCouponCode,order,errors,setOrderErrors}=useContext(OrderContext),ref=useRef(null),[type,setType]=useState(codeType);useEffect(()=>{if(type!=null&&values[type]?.value===""&&errors!=null&&errors.length>0){const err=errors.filter(e=>e.field===type);setOrderErrors(err),onSubmit&&onSubmit({value:values[type]?.value,success:!1})}type!=null&&values[type]?.value===""&&(setOrderErrors([]),onSubmit&&onSubmit({value:values[type]?.value,success:!1}))},[values]),useEffect(()=>{codeType==null?(order?.gift_card_code&&!order?.coupon_code&&setType("coupon_code"),!order?.gift_card_code&&order?.coupon_code&&setType("gift_card_code"),!order?.gift_card_code&&!order?.coupon_code&&setType("gift_card_or_coupon_code")):setType(codeType)},[order]);const handleSubmit=async e=>{e.preventDefault();const code=type!=null&&values[type]!=null?values[type].value:void 0;if(code!=null&&setGiftCardOrCouponCode!=null&&type!=null){const{success,order:order2}=await setGiftCardOrCouponCode({code,codeType:type}),value=values[type]?.value;onSubmit&&onSubmit({success,value,order:order2}),success&&reset(e)}};return codeType!=null&&order?.[codeType]!=null&&order?.[codeType]!==""||order?.gift_card_code&&order?.coupon_code||order==null?null:_jsx(CouponAndGiftCardFormContext.Provider,{value:{validation,codeType:type},children:_jsx("form",{ref,autoComplete,onSubmit:e=>{handleSubmit(e)},...p,children})})}export default GiftCardOrCouponForm;
|
|
@@ -14,6 +14,10 @@ interface Props extends Omit<JSX.IntrinsicElements['button'], 'children' | 'onCl
|
|
|
14
14
|
* The label of the button
|
|
15
15
|
*/
|
|
16
16
|
label?: string | ReactNode;
|
|
17
|
+
/**
|
|
18
|
+
* The label of the button when it's loading
|
|
19
|
+
*/
|
|
20
|
+
loadingLabel?: string | ReactNode;
|
|
17
21
|
/**
|
|
18
22
|
* If false, the button doesn't place the order automatically. Default: true
|
|
19
23
|
*/
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useContext,useEffect,useRef,useState}from"react";import Parent from"../utils/Parent";import PlaceOrderContext from"../../context/PlaceOrderContext";import isFunction from"lodash/isFunction";import PaymentMethodContext from"../../context/PaymentMethodContext";import OrderContext from"../../context/OrderContext";import getCardDetails from"../../utils/getCardDetails";export function PlaceOrderButton(props){const ref=useRef(null),{children,label="Place order",autoPlaceOrder=!0,disabled,onClick,...p}=props,{isPermitted,setPlaceOrder,options,paymentType,setButtonRef}=useContext(PlaceOrderContext),[notPermitted,setNotPermitted]=useState(!0),[forceDisable,setForceDisable]=useState(disabled),{currentPaymentMethodRef,loading,currentPaymentMethodType,paymentSource,setPaymentSource,setPaymentMethodErrors,currentCustomerPaymentSourceId}=useContext(PaymentMethodContext),{order}=useContext(OrderContext),isFree=order?.total_amount_with_taxes_cents===0;useEffect(()=>{if(loading)setNotPermitted(loading);else if(paymentType===currentPaymentMethodType&&paymentType){const card=getCardDetails({customerPayment:{payment_source:paymentSource},paymentType});currentCustomerPaymentSourceId!=null&&paymentSource?.id===currentCustomerPaymentSourceId&&card.brand===""&&(card.brand="credit-card"),(isFree&&isPermitted||currentPaymentMethodRef?.current?.onsubmit||card.brand)&&isPermitted&&setNotPermitted(!1)}else setNotPermitted(!(isFree&&isPermitted));return()=>{setNotPermitted(!0)}},[isPermitted,paymentType,currentPaymentMethodRef?.current?.onsubmit,loading,currentPaymentMethodType,order,paymentSource]),useEffect(()=>{paymentType==="paypal_payments"&&options?.paypalPayerId&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.paypalPayerId,paymentType]),useEffect(()=>{paymentType==="stripe_payments"&&["succeeded","pending"].includes(options?.stripe?.redirectStatus??"")&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.stripe?.redirectStatus,paymentType]),useEffect(()=>{if(order?.status!=null&&["draft","pending"].includes(order?.status))if(paymentType==="adyen_payments"&&options?.adyen?.redirectResult&&paymentSource!=null){const attributes={payment_request_details:{details:{redirectResult:options?.adyen?.redirectResult}},_details:1};setPaymentSource({paymentSourceId:paymentSource?.id,paymentResource:"adyen_payments",attributes}).then(res=>{const resultCode=res?.payment_response?.resultCode,errorCode=res?.payment_response?.errorCode,message=res?.payment_response?.message;["Authorised","Pending","Received"].includes(resultCode)&&autoPlaceOrder?handleClick():errorCode!=null&&setPaymentMethodErrors([{code:"PAYMENT_INTENT_AUTHENTICATION_FAILURE",resource:"payment_methods",field:currentPaymentMethodType,message}])})}else(paymentType==="adyen_payments"&&options?.adyen?.MD&&options?.adyen?.PaRes&&autoPlaceOrder||paymentType==="adyen_payments"&&order?.payment_source?.payment_response?.resultCode==="Authorised"&&ref?.current?.disabled===!1&¤tCustomerPaymentSourceId==null&&autoPlaceOrder)&&handleClick()},[options?.adyen,paymentType,order?.payment_source?.payment_response?.resultCode]),useEffect(()=>{paymentType==="checkout_com_payments"&&options?.checkoutCom?.session_id&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.checkoutCom,paymentType]),useEffect(()=>{ref?.current!=null&&setButtonRef!=null&&setButtonRef(ref)},[ref]);const handleClick=async e=>{e?.preventDefault(),e?.stopPropagation();let isValid=!0;setForceDisable(!0);const checkPaymentSource=await setPaymentSource({paymentResource:paymentType,paymentSourceId:paymentSource?.id}),card=paymentType&&getCardDetails({paymentType,customerPayment:{payment_source:checkPaymentSource}});currentPaymentMethodRef?.current?.onsubmit&&[!options?.paypalPayerId,!options?.adyen?.MD,!options?.checkoutCom?.session_id].every(Boolean)?(isValid=await currentPaymentMethodRef.current?.onsubmit({paymentSource:checkPaymentSource,setPlaceOrder,onclickCallback:onClick}),!isValid&&checkPaymentSource.payment_response?.resultCode==="Authorised"&&(isValid=!0)):card?.brand&&(isValid=!0);const placed=isValid&&setPlaceOrder&&(checkPaymentSource||isFree)&&await setPlaceOrder({paymentSource:checkPaymentSource,currentCustomerPaymentSourceId});setForceDisable(!1),onClick&&placed&&onClick(placed)},disabledButton=disabled!==void 0?disabled:notPermitted,parentProps={...p,label,disabled:disabledButton,handleClick,ref};return children?_jsx(Parent,{...parentProps,children}):_jsx("button",{ref,type:"button",disabled:disabledButton||forceDisable,onClick:e=>{handleClick(e)},...p,children:
|
|
2
|
+
import{jsx as _jsx}from"react/jsx-runtime";import{useContext,useEffect,useRef,useState}from"react";import Parent from"../utils/Parent";import PlaceOrderContext from"../../context/PlaceOrderContext";import isFunction from"lodash/isFunction";import PaymentMethodContext from"../../context/PaymentMethodContext";import OrderContext from"../../context/OrderContext";import getCardDetails from"../../utils/getCardDetails";export function PlaceOrderButton(props){const ref=useRef(null),{children,label="Place order",loadingLabel="Placing...",autoPlaceOrder=!0,disabled,onClick,...p}=props,{isPermitted,setPlaceOrder,options,paymentType,setButtonRef}=useContext(PlaceOrderContext),[notPermitted,setNotPermitted]=useState(!0),[forceDisable,setForceDisable]=useState(disabled),[isLoading,setIsLoading]=useState(!1),{currentPaymentMethodRef,loading,currentPaymentMethodType,paymentSource,setPaymentSource,setPaymentMethodErrors,currentCustomerPaymentSourceId}=useContext(PaymentMethodContext),{order}=useContext(OrderContext),isFree=order?.total_amount_with_taxes_cents===0;useEffect(()=>{if(loading)setNotPermitted(loading);else if(paymentType===currentPaymentMethodType&&paymentType){const card=getCardDetails({customerPayment:{payment_source:paymentSource},paymentType});currentCustomerPaymentSourceId!=null&&paymentSource?.id===currentCustomerPaymentSourceId&&card.brand===""&&(card.brand="credit-card"),(isFree&&isPermitted||currentPaymentMethodRef?.current?.onsubmit||card.brand)&&isPermitted&&setNotPermitted(!1)}else setNotPermitted(!(isFree&&isPermitted));return()=>{setNotPermitted(!0)}},[isPermitted,paymentType,currentPaymentMethodRef?.current?.onsubmit,loading,currentPaymentMethodType,order,paymentSource]),useEffect(()=>{paymentType==="paypal_payments"&&options?.paypalPayerId&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.paypalPayerId,paymentType]),useEffect(()=>{paymentType==="stripe_payments"&&["succeeded","pending"].includes(options?.stripe?.redirectStatus??"")&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.stripe?.redirectStatus,paymentType]),useEffect(()=>{if(order?.status!=null&&["draft","pending"].includes(order?.status))if(paymentType==="adyen_payments"&&options?.adyen?.redirectResult&&paymentSource!=null){const attributes={payment_request_details:{details:{redirectResult:options?.adyen?.redirectResult}},_details:1};setPaymentSource({paymentSourceId:paymentSource?.id,paymentResource:"adyen_payments",attributes}).then(res=>{const resultCode=res?.payment_response?.resultCode,errorCode=res?.payment_response?.errorCode,message=res?.payment_response?.message;["Authorised","Pending","Received"].includes(resultCode)&&autoPlaceOrder?handleClick():errorCode!=null&&setPaymentMethodErrors([{code:"PAYMENT_INTENT_AUTHENTICATION_FAILURE",resource:"payment_methods",field:currentPaymentMethodType,message}])})}else(paymentType==="adyen_payments"&&options?.adyen?.MD&&options?.adyen?.PaRes&&autoPlaceOrder||paymentType==="adyen_payments"&&order?.payment_source?.payment_response?.resultCode==="Authorised"&&ref?.current?.disabled===!1&¤tCustomerPaymentSourceId==null&&autoPlaceOrder)&&handleClick()},[options?.adyen,paymentType,order?.payment_source?.payment_response?.resultCode]),useEffect(()=>{paymentType==="checkout_com_payments"&&options?.checkoutCom?.session_id&&order?.status&&["draft","pending"].includes(order?.status)&&autoPlaceOrder&&handleClick()},[options?.checkoutCom,paymentType]),useEffect(()=>{ref?.current!=null&&setButtonRef!=null&&setButtonRef(ref)},[ref]);const handleClick=async e=>{e?.preventDefault(),e?.stopPropagation(),setIsLoading(!0);let isValid=!0;setForceDisable(!0);const checkPaymentSource=await setPaymentSource({paymentResource:paymentType,paymentSourceId:paymentSource?.id}),card=paymentType&&getCardDetails({paymentType,customerPayment:{payment_source:checkPaymentSource}});currentPaymentMethodRef?.current?.onsubmit&&[!options?.paypalPayerId,!options?.adyen?.MD,!options?.checkoutCom?.session_id].every(Boolean)?(isValid=await currentPaymentMethodRef.current?.onsubmit({paymentSource:checkPaymentSource,setPlaceOrder,onclickCallback:onClick}),!isValid&&checkPaymentSource.payment_response?.resultCode==="Authorised"&&(isValid=!0)):card?.brand&&(isValid=!0);const placed=isValid&&setPlaceOrder&&(checkPaymentSource||isFree)&&await setPlaceOrder({paymentSource:checkPaymentSource,currentCustomerPaymentSourceId});setForceDisable(!1),onClick&&placed&&onClick(placed),setIsLoading(!1)},disabledButton=disabled!==void 0?disabled:notPermitted,labelButton=isLoading?loadingLabel:isFunction(label)?label():label,parentProps={...p,label,disabled:disabledButton,handleClick,parentRef:ref,isLoading};return children?_jsx(Parent,{...parentProps,children}):_jsx("button",{ref,type:"button",disabled:disabledButton||forceDisable,onClick:e=>{handleClick(e)},...p,children:labelButton})}export default PlaceOrderButton;
|