1 line
5.5 KiB
JavaScript
1 line
5.5 KiB
JavaScript
(()=>{"use strict";var e={20:(e,t,n)=>{var o=n(609),i=Symbol.for("react.element"),c=Symbol.for("react.fragment"),r=Object.prototype.hasOwnProperty,s=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,a={key:!0,ref:!0,__self:!0,__source:!0};function l(e,t,n){var o,c={},l=null,u=null;for(o in void 0!==n&&(l=""+n),void 0!==t.key&&(l=""+t.key),void 0!==t.ref&&(u=t.ref),t)r.call(t,o)&&!a.hasOwnProperty(o)&&(c[o]=t[o]);if(e&&e.defaultProps)for(o in t=e.defaultProps)void 0===c[o]&&(c[o]=t[o]);return{$$typeof:i,type:e,key:l,ref:u,props:c,_owner:s.current}}t.Fragment=c,t.jsx=l,t.jsxs=l},338:(e,t,n)=>{var o=n(795);t.H=o.createRoot,o.hydrateRoot},609:e=>{e.exports=window.React},795:e=>{e.exports=window.ReactDOM},848:(e,t,n)=>{e.exports=n(20)}},t={};function n(o){var i=t[o];if(void 0!==i)return i.exports;var c=t[o]={exports:{}};return e[o](c,c.exports,n),c.exports}var o=n(338);const i=window.wp.plugins;var c=n(609);const r=window.wp.i18n,s=window.wp.components,a=window.wp.data,l=window.wp.url,u=window.wc.blocksCheckout;var d=n(848);const m=({lineItem:e,itemKey:t})=>{const[n,o]=(0,c.useState)(!1),[i,m]=(0,c.useState)(!1),[p,w]=(0,c.useState)(""),[_,f]=(0,c.useState)(!0),[g,b]=(0,c.useState)(!1),h=(0,c.useId)(),v="invalid-gifting-recipient",{setValidationErrors:k,clearValidationError:x}=(0,a.useDispatch)("wc/store/validation"),y=(0,a.useSelect)((e=>{if(!t)return null;const n=e("wc/store/cart");if(!n)return null;try{const e=n.getCartData();return e?.items?.find((e=>e.key===t))||null}catch(e){return null}}),[t]),S=(0,c.useMemo)((()=>{if(!y?.item_data)return null;const e=y.item_data.find((e=>"gifting_to_hidden"===e.name));return e?.value||null}),[y]);(0,c.useEffect)((()=>{null!==S&&(w(S),o(!0))}),[S]),(0,c.useEffect)((()=>{if(!e)return;const t=e.querySelector(".wc-block-components-product-details__gifting-to-hidden");if(!t)return;const n=t.querySelector(".wc-block-components-product-details__value");w(n?n.textContent:""),o(!0)}),[e]);const E=e=>{if(!e&&!(0,l.isEmail)(p))return f(!1),void k({[v]:{message:(0,r.__)("Please enter a valid email address","woocommerce-subscriptions"),hidden:!1}});m(!1),f(!0),x(v),(0,u.extensionCartUpdate)({namespace:"wcsg-cart",data:{recipient:!0===e?"":p,itemKey:t}}).then((()=>{m(!1)})).catch((()=>{f(!1),m(!0),k({[v]:{message:(0,r.__)("Please enter a valid email address","woocommerce-subscriptions"),hidden:!1}})}))};return(0,d.jsxs)("div",{className:"wcsg-gifting-to-container",children:[(!n||i)&&(0,d.jsx)(s.CheckboxControl,{"aria-label":window.wcSettings.wcsg_subscriptions_data.gifting_checkbox_text||(0,r.__)("This is a gift","woocommerce-subscriptions"),label:window.wcSettings.wcsg_subscriptions_data.gifting_checkbox_text||(0,r.__)("This is a gift","woocommerce-subscriptions"),checked:n,onChange:e=>{o(e),m(e)}}),n&&!i&&(0,d.jsxs)("div",{className:"wcsg-gifting-to-container-view",children:[(0,d.jsxs)("span",{title:(0,r.__)("Gifting to","woocommerce-subscriptions"),children:[(0,r.__)("Gifting to:","woocommerce-subscriptions")," ",!i&&p]}),(0,d.jsx)(s.Button,{variant:"link",onClick:()=>m(!0),children:(0,r.__)("Edit","woocommerce-subscriptions")}),(0,d.jsx)(s.Button,{variant:"link","aria-label":(0,r.__)("Remove gift recipient","woocommerce-subscriptions"),onClick:()=>(o(!1),w(""),void E(!0)),children:(0,r.__)("Remove","woocommerce-subscriptions")})]}),i&&(0,d.jsxs)("div",{className:"wcsg-gifting-to-container-editing wc-block-components-form",children:[(0,d.jsxs)("div",{className:`wc-block-components-text-input ${_?"":"has-error"} ${g||p?"is-active":""}`,children:[(0,d.jsx)("input",{type:"email",value:p,onChange:e=>w(e.target.value),onFocus:()=>{b(!0),f(!0)},onBlur:()=>{b(!1)},"aria-label":(0,r.__)("Gifting recipient","woocommerce-subscriptions"),id:h,"data-testid":"recipient-input"}),(0,d.jsx)("label",{htmlFor:h,children:(0,r.__)("Recipient's email address","woocommerce-subscriptions")})]}),(0,d.jsx)("button",{className:"wc-block-components-button wp-element-button gifting-update-button",onClick:()=>E(!1),name:"Update",children:(0,r.__)("Update","woocommerce-subscriptions")})]}),i&&!_&&(0,d.jsx)(u.ValidationInputError,{elementId:v,propertyName:v})]})},p=()=>{const{__internalSetUseShippingAsBilling:e,setEditingShippingAddress:t}=(0,a.useDispatch)("wc/store/checkout"),n=(0,a.useSelect)((e=>{const t=e("wc/store/cart");if(!t)return null;try{const e=t.getCartData();return e?.items?.find((e=>e.item_data.find((e=>"gifting_to_hidden"===e.name))))||null}catch(e){return null}}),[]);return(0,c.useEffect)((()=>{n&&(e(!1),t(!0))}),[n,e,t]),(0,d.jsx)(d.Fragment,{})};window.addEventListener("load",(function(){let e=null;function t(){const t=document.querySelectorAll(".wc-block-components-product-metadata");0!==t.length&&(t.length&&e&&e.disconnect(),t.forEach((function(e,t){const n=e?.querySelector(".wc-block-components-product-details__item-key .wc-block-components-product-details__value")?.textContent||"";if(!n)return;const i="gifting-recipient-"+t;if(!document.getElementById(i)){const t=document.createElement("div");t.className="wcsg-block-recipient-container",t.id=i,e.parentNode.insertBefore(t,e)}(0,o.H)(document.getElementById(i)).render((0,d.jsx)(m,{lineItem:e,itemKey:n}))})))}function n(){setTimeout((()=>{e=new MutationObserver(t),t(),e.observe(document.body,{childList:!0,subtree:!0})}),200)}null!==document.querySelector(".wp-block-woocommerce-checkout")?(0,i.registerPlugin)("woocommerce-checkout-gifting",{render:p,icon:"gift",title:"Checkout Gifting Features",scope:"woocommerce-checkout"}):(n(),document.addEventListener("click",(function(e){e.target.matches(".wc-block-mini-cart__button, .wc-block-mini-cart__button *")&&n()})))}))})(); |