geno/wp-content/plugins/woocommerce/packages/woocommerce-blocks/build/product-add-to-cart.js

3 lines
25 KiB
JavaScript
Raw Normal View History

2024-02-01 17:24:18 +05:30
(self.webpackChunkwebpackWcBlocksJsonp=self.webpackChunkwebpackWcBlocksJsonp||[]).push([[5800],{6638:(e,t,r)=>{"use strict";r.d(t,{Z:()=>s});const s={showFormElements:{type:"boolean",default:!1},productId:{type:"number",default:0}}},8936:(e,t,r)=>{"use strict";r.d(t,{Z:()=>We});var s=r(9196),o=r(4184),a=r.n(o),n=r(9307),c=r(5736),i=r(9075),l=r(6576),u=r(9818),d=r(4801);const p={PRISTINE:"pristine",IDLE:"idle",DISABLED:"disabled",PROCESSING:"processing",BEFORE_PROCESSING:"before_processing",AFTER_PROCESSING:"after_processing"},E={status:p.PRISTINE,hasError:!1,quantity:0,processingResponse:null,requestParams:{}},m={SET_PRISTINE:"set_pristine",SET_IDLE:"set_idle",SET_DISABLED:"set_disabled",SET_PROCESSING:"set_processing",SET_BEFORE_PROCESSING:"set_before_processing",SET_AFTER_PROCESSING:"set_after_processing",SET_PROCESSING_RESPONSE:"set_processing_response",SET_HAS_ERROR:"set_has_error",SET_NO_ERROR:"set_no_error",SET_QUANTITY:"set_quantity",SET_REQUEST_PARAMS:"set_request_params"},{SET_PRISTINE:_,SET_IDLE:h,SET_DISABLED:b,SET_PROCESSING:S,SET_BEFORE_PROCESSING:g,SET_AFTER_PROCESSING:v,SET_PROCESSING_RESPONSE:f,SET_HAS_ERROR:R,SET_NO_ERROR:C,SET_QUANTITY:y,SET_REQUEST_PARAMS:T}=m,w=()=>({type:h}),A=(e=!0)=>({type:e?R:C}),{SET_PRISTINE:P,SET_IDLE:O,SET_DISABLED:I,SET_PROCESSING:k,SET_BEFORE_PROCESSING:N,SET_AFTER_PROCESSING:D,SET_PROCESSING_RESPONSE:L,SET_HAS_ERROR:x,SET_NO_ERROR:F,SET_QUANTITY:B,SET_REQUEST_PARAMS:M}=m,{PRISTINE:q,IDLE:j,DISABLED:Q,PROCESSING:Y,BEFORE_PROCESSING:G,AFTER_PROCESSING:V}=p,K=(e=E,{quantity:t,type:r,data:s})=>{let o;switch(r){case P:o=E;break;case O:o=e.status!==j?{...e,status:j}:e;break;case I:o=e.status!==Q?{...e,status:Q}:e;break;case B:o=t!==e.quantity?{...e,quantity:t}:e;break;case M:o={...e,requestParams:{...e.requestParams,...s}};break;case L:o={...e,processingResponse:s};break;case k:o=e.status!==Y?{...e,status:Y,hasError:!1}:e,o=!1===o.hasError?o:{...o,hasError:!1};break;case N:o=e.status!==G?{...e,status:G,hasError:!1}:e;break;case D:o=e.status!==V?{...e,status:V}:e;break;case x:o=e.hasError?e:{...e,hasError:!0},o=e.status===Y||e.status===G?{...o,status:j}:o;break;case F:o=e.hasError?{...e,hasError:!1}:e}return o!==e&&r!==P&&o.status===q&&(o.status=j),o};let H=function(e){return e.ADD_EVENT_CALLBACK="add_event_callback",e.REMOVE_EVENT_CALLBACK="remove_event_callback",e}({});const W={},U=(e=W,{type:t,eventType:r,id:s,callback:o,priority:a})=>{const n=e.hasOwnProperty(r)?new Map(e[r]):new Map;switch(t){case H.ADD_EVENT_CALLBACK:return n.set(s,{priority:a,callback:o}),{...e,[r]:n};case H.REMOVE_EVENT_CALLBACK:return n.delete(s),{...e,[r]:n}}},Z=(e,t)=>(r,s=10)=>{const o=((e,t,r=10)=>({id:Math.floor(Math.random()*Date.now()).toString(),type:H.ADD_EVENT_CALLBACK,eventType:e,callback:t,priority:r}))(e,r,s);return t(o),()=>{var r;t((r=e,{id:o.id,type:H.REMOVE_EVENT_CALLBACK,eventType:r}))}},$="add_to_cart_before_processing",X="add_to_cart_after_processing_with_success",J="add_to_cart_after_processing_with_error",z=e=>({onAddToCartAfterProcessingWithSuccess:Z(X,e),onAddToCartProcessingWithError:Z(J,e),onAddToCartBeforeProcessing:Z($,e)});var ee=r(7884);const te=(e,t)=>e[t]?Array.from(e[t].values()).sort(((e,t)=>e.priority-t.priority)):[];let re=function(e){return e.SUCCESS="success",e.FAIL="failure",e.ERROR="error",e}({});const se=(e,t)=>(0,ee.Kn)(e)&&"type"in e&&e.type===t,oe=e=>se(e,re.ERROR),ae=e=>se(e,re.FAIL),ne=e=>(0,ee.Kn)(e)&&(0,ee.$n)(e,"type"),ce=async(e,t,r)=>{const s=[],o=te(e,t);for(const e of o)try{const t=await Promise.resolve(e.callback(r));if(!ne(t))continue;if(!t.hasOwnProperty("type"))throw new Error("Returned objects from event emitter observers must return an object with a type property");if(oe(t)||ae(t))return s.push(t),s;s.push(t)}catch(e){return console.error(e),s.push({type:re.ERROR}),s}return s},ie=(0,n.createContext)({product:{},productType:"simple",productIsPurchasable:!0,productHasOptions:!1,supportsFormElements:!0,showFormElements:!1,quantity:0,minQuantity:1,maxQuantity:99,requestParams:{},isIdle:!1,isDisabled:!1,isProcessing:!1,isBeforeProcessing:!1,isAfterPro
(0,c._n)("%d in cart","%d in cart",t,"woocommerce"),t):(0,c.__)("Add to cart","woocommerce"),!!a&&(0,s.createElement)(ye.Z,{icon:Te.Z})),Ie=()=>{const{showFormElements:e,productIsPurchasable:t,productHasOptions:r,product:o,productType:a,isDisabled:i,isProcessing:l,eventRegistration:u,hasError:d,dispatchActions:p}=le(),{parentName:E}=(0,Se.useInnerBlockLayoutContext)(),{dispatchStoreEvent:m}=(0,we.n)(),{cartQuantity:_}=(0,Ae.c)(o.id||0),[h,b]=(0,n.useState)(!1),S=o.add_to_cart||{url:"",text:""};return(0,n.useEffect)((()=>{const e=u.onAddToCartAfterProcessingWithSuccess((()=>(d||b(!0),!0)),0);return()=>{e()}}),[u,d]),(e||!r&&"simple"===a)&&t?(0,s.createElement)(Oe,{className:"wc-block-components-product-add-to-cart-button",quantityInCart:_,isDisabled:i,isProcessing:l,isDone:h,onClick:()=>{p.submitForm(`woocommerce/single-product/${(null==o?void 0:o.id)||0}`),m("cart-add-item",{product:o,listName:E})}}):(0,s.createElement)(Pe,{className:"wc-block-components-product-add-to-cart-button",href:S.url,text:S.text||(0,c.__)("View Product","woocommerce"),onClick:()=>{m("product-view-link",{product:o,listName:E})}})};var ke=r(4697);const Ne=({disabled:e,min:t,max:r,step:o=1,value:a,onChange:n})=>{const c=void 0!==r,i=(0,ke.y1)((e=>{let s=e;c&&(s=Math.min(s,Math.floor(r/o)*o)),s=Math.max(s,Math.ceil(t/o)*o),s=Math.floor(s/o)*o,s!==e&&(null==n||n(s))}),300);return(0,s.createElement)("input",{className:"wc-block-components-product-add-to-cart-quantity",type:"number",value:a,min:t,max:r,step:o,hidden:1===r,disabled:e,onChange:e=>{null==n||n(e.target.value),i(Number(e.target.value))}})},De=({reason:e=(0,c.__)("Sorry, this product cannot be purchased.","woocommerce")})=>(0,s.createElement)("div",{className:"wc-block-components-product-add-to-cart-unavailable"},e),Le=()=>{const{product:e,quantity:t,minQuantity:r,maxQuantity:o,multipleOf:a,dispatchActions:n,isDisabled:i}=le();return e.id&&!e.is_purchasable?(0,s.createElement)(De,null):e.id&&!e.is_in_stock?(0,s.createElement)(De,{reason:(0,c.__)("This product is currently out of stock and cannot be purchased.","woocommerce")}):(0,s.createElement)(s.Fragment,null,(0,s.createElement)(Ne,{value:t,min:r,max:o,step:a,disabled:i,onChange:n.setQuantity}),(0,s.createElement)(Ie,null))};r(461);var xe=r(3854),Fe=r(711);const Be={value:"",label:(0,c.__)("Select an option","woocommerce")},Me=({attributeName:e,options:t=[],value:r="",onChange:o=(()=>{}),errorMessage:i=(0,c.__)("Please select a value.","woocommerce")})=>{const l=e,{setValidationErrors:p,clearValidationError:E}=(0,u.useDispatch)(d.VALIDATION_STORE_KEY),{error:m}=(0,u.useSelect)((e=>({error:e(d.VALIDATION_STORE_KEY).getValidationError(l)||{}})));return(0,n.useEffect)((()=>{r?E(l):p({[l]:{message:i,hidden:!0}})}),[r,l,i,E,p]),(0,n.useEffect)((()=>()=>{E(l)}),[l,E]),(0,s.createElement)("div",{className:"wc-block-components-product-add-to-cart-attribute-picker__container"},(0,s.createElement)(xe.Z,{label:(0,Ee.decodeEntities)(e),value:r||"",options:[Be,...t],onChange:o,required:!0,className:a()("wc-block-components-product-add-to-cart-attribute-picker__select",{"has-error":(null==m?void 0:m.message)&&!(null!=m&&m.hidden)})}),(0,s.createElement)(Fe.ValidationInputError,{propertyName:l,elementId:l}))};var qe=r(8916);const je=(e,t,r)=>{const s=Object.values(t).map((({id:e})=>e));if(Object.values(r).every((e=>""===e)))return s;const o=Object.keys(e);return s.filter((e=>o.every((s=>{const o=r[s]||"",a=t["id:"+e].attributes[s];return""===o||null===a||a===o}))))},Qe=({attributes:e,variationAttributes:t,setRequestParams:r})=>{const o=(0,i.s)(e),a=(0,i.s)(t),[c,l]=(0,n.useState)(0),[u,d]=(0,n.useState)({}),[p,E]=(0,n.useState)(!1),m=(0,n.useMemo)((()=>((e,t,r)=>{const s={},o=Object.keys(e),a=Object.values(r).filter(Boolean).length>0;return o.forEach((o=>{const n=e[o],c={...r,[o]:null},i=a?je(e,t,c):null,l=null!==i?i.map((e=>t["id:"+e].attributes[o])):null;s[o]=((e,t=null)=>Object.values(e).map((({name:e,slug:r})=>null===t||t.includes(null)||t.includes(r)?{value:r,label:(0,Ee.decodeEntities)(e)}:null)).filter(Boolean))(n.terms,l)})),s})(o,a,u)),[u,o,a]
(0,o.__)("%s (optional)","woocommerce"),e.label)),e.priority&&((0,a.h)(e.priority)&&(t.index=e.priority),(0,n.H)(e.priority)&&(t.index=parseInt(e.priority,10))),e.hidden&&(t.required=!1),t},l=Object.entries(c.vr).map((([e,t])=>[e,Object.entries(t).map((([e,t])=>[e,i(t)])).reduce(((e,[t,r])=>(e[t]=r,e)),{})])).reduce(((e,[t,r])=>(e[t]=r,e)),{}),u=(e,t,r="")=>{const o=r&&void 0!==l[r]?l[r]:{};return e.map((e=>({key:e,...s.defaultAddressFields[e]||{},...o[e]||{},...t[e]||{}}))).sort(((e,t)=>e.index-t.index))}},6286:(e,t,r)=>{"use strict";r.d(t,{O:()=>d});var s=r(9307),o=r(4801),a=r(9818),n=r(1377),c=r(9456);const i=e=>{const t=null==e?void 0:e.detail;t&&t.preserveCartData||(0,a.dispatch)(o.CART_STORE_KEY).invalidateResolutionForStore()},l=e=>{(null!=e&&e.persisted||"back_forward"===(0,n.f)())&&(0,a.dispatch)(o.CART_STORE_KEY).invalidateResolutionForStore()},u=()=>{1===window.wcBlocksStoreCartListeners.count&&window.wcBlocksStoreCartListeners.remove(),window.wcBlocksStoreCartListeners.count--},d=()=>{(0,s.useEffect)((()=>((()=>{if(window.wcBlocksStoreCartListeners||(window.wcBlocksStoreCartListeners={count:0,remove:()=>{}}),(null===(e=window.wcBlocksStoreCartListeners)||void 0===e?void 0:e.count)>0)return void window.wcBlocksStoreCartListeners.count++;var e;document.body.addEventListener("wc-blocks_added_to_cart",i),document.body.addEventListener("wc-blocks_removed_from_cart",i),window.addEventListener("pageshow",l);const t=(0,c.Es)("added_to_cart","wc-blocks_added_to_cart"),r=(0,c.Es)("removed_from_cart","wc-blocks_removed_from_cart");window.wcBlocksStoreCartListeners.count=1,window.wcBlocksStoreCartListeners.remove=()=>{document.body.removeEventListener("wc-blocks_added_to_cart",i),document.body.removeEventListener("wc-blocks_removed_from_cart",i),window.removeEventListener("pageshow",l),t(),r()}})(),u)),[])}},9816:(e,t,r)=>{"use strict";r.d(t,{b:()=>b});var s=r(2991),o=r.n(s),a=r(9307),n=r(4801),c=r(9818),i=r(2629),l=r(3881),u=r(8832),d=r(6286);const p={first_name:"",last_name:"",company:"",address_1:"",address_2:"",city:"",state:"",postcode:"",country:"",phone:""},E={...p,email:""},m={total_items:"",total_items_tax:"",total_fees:"",total_fees_tax:"",total_discount:"",total_discount_tax:"",total_shipping:"",total_shipping_tax:"",total_price:"",total_tax:"",tax_lines:n.EMPTY_TAX_LINES,currency_code:"",currency_symbol:"",currency_minor_unit:2,currency_decimal_separator:"",currency_thousand_separator:"",currency_prefix:"",currency_suffix:""},_=e=>Object.fromEntries(Object.entries(e).map((([e,t])=>[e,(0,i.decodeEntities)(t)]))),h={cartCoupons:n.EMPTY_CART_COUPONS,cartItems:n.EMPTY_CART_ITEMS,cartFees:n.EMPTY_CART_FEES,cartItemsCount:0,cartItemsWeight:0,crossSellsProducts:n.EMPTY_CART_CROSS_SELLS,cartNeedsPayment:!0,cartNeedsShipping:!0,cartItemErrors:n.EMPTY_CART_ITEM_ERRORS,cartTotals:m,cartIsLoading:!0,cartErrors:n.EMPTY_CART_ERRORS,billingAddress:E,shippingAddress:p,shippingRates:n.EMPTY_SHIPPING_RATES,isLoadingRates:!1,cartHasCalculatedShipping:!1,paymentMethods:n.EMPTY_PAYMENT_METHODS,paymentRequirements:n.EMPTY_PAYMENT_REQUIREMENTS,receiveCart:()=>{},receiveCartContents:()=>{},extensions:n.EMPTY_EXTENSIONS},b=(e={shouldSelect:!0})=>{const{isEditor:t,previewData:r}=(0,u._)(),s=null==r?void 0:r.previewCart,{shouldSelect:i}=e,m=(0,a.useRef)();(0,d.O)();const b=(0,c.useSelect)(((e,{dispatch:r})=>{if(!i)return h;if(t)return{cartCoupons:s.coupons,cartItems:s.items,crossSellsProducts:s.cross_sells,cartFees:s.fees,cartItemsCount:s.items_count,cartItemsWeight:s.items_weight,cartNeedsPayment:s.needs_payment,cartNeedsShipping:s.needs_shipping,cartItemErrors:n.EMPTY_CART_ITEM_ERRORS,cartTotals:s.totals,cartIsLoading:!1,cartErrors:n.EMPTY_CART_ERRORS,billingData:E,billingAddress:E,shippingAddress:p,extensions:n.EMPTY_EXTENSIONS,shippingRates:s.shipping_rates,isLoadingRates:!1,cartHasCalculatedShipping:s.has_calculated_shipping,paymentRequirements:s.paymentRequirements,receiveCart:"function"==typeof(null==s?void 0:s.receiveCart)?s.receiveCart:()=>{},receiveCartContents:"function"==typeof(null==s?void 0:s.receiveCartContents)?s.recei