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

3 lines
24 KiB
JavaScript
Raw Normal View History

2024-02-01 17:24:18 +05:30
(self.webpackChunkwebpackWcBlocksJsonp=self.webpackChunkwebpackWcBlocksJsonp||[]).push([[5800],{2624:(e,t,r)=>{"use strict";r.r(t),r.d(t,{default:()=>He});var s=r(721),o=r(9196),a=r(4184),n=r.n(a),c=r(9307),i=r(5736),l=r(9075);const u=e=>e.is_purchasable||!1;var d=r(9818),p=r(4801);const E={PRISTINE:"pristine",IDLE:"idle",DISABLED:"disabled",PROCESSING:"processing",BEFORE_PROCESSING:"before_processing",AFTER_PROCESSING:"after_processing"},m={status:E.PRISTINE,hasError:!1,quantity:0,processingResponse:null,requestParams:{}},_={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:h,SET_IDLE:b,SET_DISABLED:S,SET_PROCESSING:g,SET_BEFORE_PROCESSING:v,SET_AFTER_PROCESSING:R,SET_PROCESSING_RESPONSE:f,SET_HAS_ERROR:y,SET_NO_ERROR:T,SET_QUANTITY:C,SET_REQUEST_PARAMS:w}=_,A=()=>({type:b}),P=(e=!0)=>({type:e?y:T}),{SET_PRISTINE:I,SET_IDLE:O,SET_DISABLED:N,SET_PROCESSING:k,SET_BEFORE_PROCESSING:D,SET_AFTER_PROCESSING:L,SET_PROCESSING_RESPONSE:F,SET_HAS_ERROR:x,SET_NO_ERROR:B,SET_QUANTITY:M,SET_REQUEST_PARAMS:q}=_,{PRISTINE:j,IDLE:Y,DISABLED:Q,PROCESSING:G,BEFORE_PROCESSING:V,AFTER_PROCESSING:K}=E,H=(e=m,{quantity:t,type:r,data:s})=>{let o;switch(r){case I:o=m;break;case O:o=e.status!==Y?{...e,status:Y}:e;break;case N:o=e.status!==Q?{...e,status:Q}:e;break;case M:o=t!==e.quantity?{...e,quantity:t}:e;break;case q:o={...e,requestParams:{...e.requestParams,...s}};break;case F:o={...e,processingResponse:s};break;case k:o=e.status!==G?{...e,status:G,hasError:!1}:e,o=!1===o.hasError?o:{...o,hasError:!1};break;case D:o=e.status!==V?{...e,status:V,hasError:!1}:e;break;case L:o=e.status!==K?{...e,status:K}:e;break;case x:o=e.hasError?e:{...e,hasError:!0},o=e.status===G||e.status===V?{...o,status:Y}:o;break;case B:o=e.hasError?{...e,hasError:!1}:e}return o!==e&&r!==I&&o.status===j&&(o.status=Y),o};let W=function(e){return e.ADD_EVENT_CALLBACK="add_event_callback",e.REMOVE_EVENT_CALLBACK="remove_event_callback",e}({});const U={},$=(e=U,{type:t,eventType:r,id:s,callback:o,priority:a})=>{const n=e.hasOwnProperty(r)?new Map(e[r]):new Map;switch(t){case W.ADD_EVENT_CALLBACK:return n.set(s,{priority:a,callback:o}),{...e,[r]:n};case W.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:W.ADD_EVENT_CALLBACK,eventType:e,callback:t,priority:r}))(e,r,s);return t(o),()=>{var r;t((r=e,{id:o.id,type:W.REMOVE_EVENT_CALLBACK,eventType:r}))}},X="add_to_cart_before_processing",J="add_to_cart_after_processing_with_success",z="add_to_cart_after_processing_with_error",ee=e=>({onAddToCartAfterProcessingWithSuccess:Z(J,e),onAddToCartProcessingWithError:Z(z,e),onAddToCartBeforeProcessing:Z(X,e)});var te=r(7884);const re=(e,t)=>e[t]?Array.from(e[t].values()).sort(((e,t)=>e.priority-t.priority)):[];let se=function(e){return e.SUCCESS="success",e.FAIL="failure",e.ERROR="error",e}({});const oe=(e,t)=>(0,te.Kn)(e)&&"type"in e&&e.type===t,ae=e=>oe(e,se.ERROR),ne=e=>oe(e,se.FAIL),ce=e=>(0,te.Kn)(e)&&(0,te.$n)(e,"type"),ie=async(e,t,r)=>{const s=[],o=re(e,t);for(const e of o)try{const t=await Promise.resolve(e.callback(r));if(!ce(t))continue;if(!t.hasOwnProperty("type"))throw new Error("Returned objects from event emitter observers must return an object with a type property");if(ae(t)||ne(t))return s.push(t),s;s.push(t)}catch(e){return console.error(e),s.push({type:se.ERROR}),s}return s},le=(0,c.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,isAfterProcessing:!1,hasError:!1,eventRegistration:{onAddToCartAfterProcessingWithSuccess:e=>{},on
(0,i._n)("%d in cart","%d in cart",t,"woocommerce"),t):(0,i.__)("Add to cart","woocommerce"),!!a&&(0,o.createElement)(ye.Z,{icon:Te.Z})),Ie=()=>{const{showFormElements:e,productIsPurchasable:t,productHasOptions:r,product:s,productType:a,isDisabled:n,isProcessing:l,eventRegistration:u,hasError:d,dispatchActions:p}=ue(),{parentName:E}=(0,ge.useInnerBlockLayoutContext)(),{dispatchStoreEvent:m}=(0,Ce.n)(),{cartQuantity:_}=(0,we.c)(s.id||0),[h,b]=(0,c.useState)(!1),S=s.add_to_cart||{url:"",text:""};return(0,c.useEffect)((()=>{const e=u.onAddToCartAfterProcessingWithSuccess((()=>(d||b(!0),!0)),0);return()=>{e()}}),[u,d]),(e||!r&&"simple"===a)&&t?(0,o.createElement)(Pe,{className:"wc-block-components-product-add-to-cart-button",quantityInCart:_,isDisabled:n,isProcessing:l,isDone:h,onClick:()=>{p.submitForm(`woocommerce/single-product/${(null==s?void 0:s.id)||0}`),m("cart-add-item",{product:s,listName:E})}}):(0,o.createElement)(Ae,{className:"wc-block-components-product-add-to-cart-button",href:S.url,text:S.text||(0,i.__)("View Product","woocommerce"),onClick:()=>{m("product-view-link",{product:s,listName:E})}})};var Oe=r(4697);const Ne=({disabled:e,min:t,max:r,step:s=1,value:a,onChange:n})=>{const c=void 0!==r,i=(0,Oe.y1)((e=>{let o=e;c&&(o=Math.min(o,Math.floor(r/s)*s)),o=Math.max(o,Math.ceil(t/s)*s),o=Math.floor(o/s)*s,o!==e&&(null==n||n(o))}),300);return(0,o.createElement)("input",{className:"wc-block-components-product-add-to-cart-quantity",type:"number",value:a,min:t,max:r,step:s,hidden:1===r,disabled:e,onChange:e=>{null==n||n(e.target.value),i(Number(e.target.value))}})},ke=({reason:e=(0,i.__)("Sorry, this product cannot be purchased.","woocommerce")})=>(0,o.createElement)("div",{className:"wc-block-components-product-add-to-cart-unavailable"},e),De=()=>{const{product:e,quantity:t,minQuantity:r,maxQuantity:s,multipleOf:a,dispatchActions:n,isDisabled:c}=ue();return e.id&&!e.is_purchasable?(0,o.createElement)(ke,null):e.id&&!e.is_in_stock?(0,o.createElement)(ke,{reason:(0,i.__)("This product is currently out of stock and cannot be purchased.","woocommerce")}):(0,o.createElement)(o.Fragment,null,(0,o.createElement)(Ne,{value:t,min:r,max:s,step:a,disabled:c,onChange:n.setQuantity}),(0,o.createElement)(Ie,null))};r(461);var Le=r(7313),Fe=r(711);const xe={value:"",label:(0,i.__)("Select an option","woocommerce")},Be=({attributeName:e,options:t=[],value:r="",onChange:s=(()=>{}),errorMessage:a=(0,i.__)("Please select a value.","woocommerce")})=>{const l=e,{setValidationErrors:u,clearValidationError:E}=(0,d.useDispatch)(p.VALIDATION_STORE_KEY),{error:m}=(0,d.useSelect)((e=>({error:e(p.VALIDATION_STORE_KEY).getValidationError(l)||{}})));return(0,c.useEffect)((()=>{r?E(l):u({[l]:{message:a,hidden:!0}})}),[r,l,a,E,u]),(0,c.useEffect)((()=>()=>{E(l)}),[l,E]),(0,o.createElement)("div",{className:"wc-block-components-product-add-to-cart-attribute-picker__container"},(0,o.createElement)(Le.Z,{label:(0,me.decodeEntities)(e),value:r||"",options:[xe,...t],onChange:s,required:!0,className:n()("wc-block-components-product-add-to-cart-attribute-picker__select",{"has-error":(null==m?void 0:m.message)&&!(null!=m&&m.hidden)})}),(0,o.createElement)(Fe.ValidationInputError,{propertyName:l,elementId:l}))},Me=(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 s=(0,l.s)(e),a=(0,l.s)(t),[n,i]=(0,c.useState)(0),[u,d]=(0,c.useState)({}),[p,E]=(0,c.useState)(!1),m=(0,c.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?Me(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,me.decodeEntities)(e)}:null)).filter(Boolean))(n.terms,l)})),s})(s,a,u)),[u,s,a]);return(0,c.useEffec
(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))}},4055:(e,t,r)=>{"use strict";r.d(t,{b:()=>v});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);var d=r(9456);const p=e=>{const t=null==e?void 0:e.detail;t&&t.preserveCartData||(0,c.dispatch)(n.CART_STORE_KEY).invalidateResolutionForStore()},E=e=>{(null!=e&&e.persisted||"back_forward"===(window.performance&&window.performance.getEntriesByType("navigation").length?window.performance.getEntriesByType("navigation")[0].type:""))&&(0,c.dispatch)(n.CART_STORE_KEY).invalidateResolutionForStore()},m=()=>{1===window.wcBlocksStoreCartListeners.count&&window.wcBlocksStoreCartListeners.remove(),window.wcBlocksStoreCartListeners.count--},_={first_name:"",last_name:"",company:"",address_1:"",address_2:"",city:"",state:"",postcode:"",country:"",phone:""},h={..._,email:""},b={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:""},S=e=>Object.fromEntries(Object.entries(e).map((([e,t])=>[e,(0,i.decodeEntities)(t)]))),g={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:b,cartIsLoading:!0,cartErrors:n.EMPTY_CART_ERRORS,billingAddress:h,shippingAddress:_,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},v=(e={shouldSelect:!0})=>{const{isEditor:t,previewData:r}=(0,u._)(),s=null==r?void 0:r.previewCart,{shouldSelect:i}=e,b=(0,a.useRef)();(0,a.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",p),document.body.addEventListener("wc-blocks_removed_from_cart",p),window.addEventListener("pageshow",E);const t=(0,d.Es)("added_to_cart","wc-blocks_added_to_cart"),r=(0,d.Es)("removed_from_cart","wc-blocks_removed_from_cart");window.wcBlocksStoreCartListeners.count=1,window.wcBlocksStoreCartListeners.remove=()=>{document.body.removeEventListener("wc-blocks_added_to_cart",p),document.body.removeEventListener("wc-blocks_removed_from_cart",p),window.removeEventListener("pageshow",E),t(),r()}})(),m)),[]);const v=(0,c.useSelect)(((e,{dispatch:r})=>{if(!i)return g;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:h,billingAddress:h,shippingAddress:_,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.receiveCartContent