haiku-atelier-2024/web/app/themes/haiku-atelier-2024/assets/js/scripts-page-panier.js

1 line
5.6 KiB
JavaScript

import{z as $}from"./index.CuMLbxmh.js";import{p as I}from"./index.DFL3aKZD.js";import{a as z,b as x,c as X}from"./api.js";import{k as K,l as Y,m as Z,n as tt,o as et,p as ot,q as rt,r as st,s as at,t as nt,u as G,v as F,w as j,x as w,y as J,A as it,z as Et,j as D,B as m}from"./dom.js";import{N as ct,a as ut}from"./messages3.js";import{r as u,a as W,b as mt}from"./dom2.js";import{l as f,r as p}from"./erreurs.js";import{e as O,a as h}from"./gardes.js";import{e as g,a as y,v as lt,b as At}from"./messages.js";import{p as C,t as d}from"./reseau.js";import{W as L}from"./cart.js";import{W as _t}from"./cart-remove-item.js";import{W as Tt}from"./cart-update-customer.js";import{W as q}from"./cart-update-item.js";import{c as pt,r as l,y as H,p as St}from"./utils.js";import{p as a}from"./pipe.CtTFdEz3.js";import{l as P}from"./index-0eef19ec._7_ubBTi.js";import{M as T,E as U}from"./Either.DDEPhGsy.js";import"./exports.BOrjxC8q.js";import"./messages2.js";import"./erreurs2.js";import"./cart2.js";import"./adresses.js";import"./couts-livraison.js";import"./belt_Option-91f3b350.aHdMVyeC.js";const b=_etats,k=pt(K),Rt=a(l(Y),u),Nt=a(l(Z),u),It=a(l(tt),u),ft=a(l(et),u),Ot=a(l(ot),u),Q=a(l(rt),u),M=a(l(st),u),N=a(l(at),u),V=a(l(nt),u),v=o=>e=>a(mt(o)(e),u),B=o=>o.forEach(e=>{const r=v(e),t={boutonAddition:r(w),boutonSoustraction:r(F),boutonSuppression:r(J)};t.boutonSoustraction.setAttribute(m,""),t.boutonAddition.setAttribute(m,""),t.boutonSuppression.setAttribute(m,""),t.boutonSuppression.textContent="C= C= C= C= C=┌(;・ω・)┘"}),S=o=>o.forEach(e=>{const r=v(e),t={boutonAddition:r(w),boutonSoustraction:r(F),boutonSuppression:r(J),champQuantite:r(j)};Number(t.champQuantite?.value)===1?t.boutonSoustraction.setAttribute(m,""):t.boutonSoustraction.removeAttribute(m),t.boutonAddition.removeAttribute(m),t.boutonSuppression.removeAttribute(m),t.boutonSuppression.textContent="Remove"}),Ct=()=>{k.ifRight(o=>{o.forEach(e=>{const r=v(e),t=T.fromNullable(e.getAttribute(G)).ifNothing(()=>{e.remove()}).orDefault("CLE_PANIER_INEXISTANTE"),A=r(F),i=r(j),_=r(w);r(J).addEventListener("click",()=>{U.encase(()=>I(_t,{key:t})).map(async E=>{B(o),await C({corps:JSON.stringify(E),nonce:b.nonce,route:z}).then(async c=>{O(c)&&f("500 Server Error"),a(await c.json(),s=>d(s,L),P.tap(s=>{g({quantiteProduits:s.items_count}),y({produits:s.items,sousTotalPanier:Number(s.totals.total_price)/100,totalPanier:Number(s.totals.total_items)/100}),e.remove()}))}).catch(c=>{h(c)?p(c):console.error("e n'est pas une Erreur ?!",c),S(o)})}).ifLeft(E=>p(E))}),A.addEventListener("click",()=>{T.fromNullable(i.valueAsNumber).filter(E=>E>1).ifJust(E=>{U.encase(()=>I(q,{key:t,quantity:E-1})).map(async c=>{B(o),await C({corps:JSON.stringify(c),nonce:b.nonce,route:x}).then(async s=>{O(s)&&f("500 Server Error"),a(await s.json(),n=>d(n,L),P.tap(n=>{g({quantiteProduits:n.items_count}),y({produits:n.items,sousTotalPanier:Number(n.totals.total_price)/100,totalPanier:Number(n.totals.total_items)/100})}))}).catch(s=>{h(s)?p(s):console.error("e n'est pas une Erreur ?!",s),S(o)})})})}),_.addEventListener("click",()=>{T.fromNullable(i.valueAsNumber).ifJust(E=>{B(o),U.encase(()=>I(q,{key:t,quantity:E+1})).map(async c=>{await C({corps:JSON.stringify(c),nonce:b.nonce,route:x}).then(async s=>{O(s)&&f("500 Server Error"),a(await s.json(),n=>d(n,L),P.tap(n=>{g({quantiteProduits:n.items_count}),y({produits:n.items,sousTotalPanier:Number(n.totals.total_price)/100,totalPanier:Number(n.totals.total_items)/100})}))}).catch(s=>{h(s)?p(s):console.error("e n'est pas une Erreur ?!",s),S(o)})})})})})})},dt=()=>{const o=new BroadcastChannel(ct);o.onmessage=e=>{lt(e).map(H.getUnsafe("donnees")).ifRight(r=>{Rt.setAttribute(it,String(r.quantiteProduits!==0))})},document.addEventListener("visibilitychange",()=>{o.close()})},Lt=()=>{const o=new BroadcastChannel(ut);o.onmessage=e=>{At(e).chain(St("donnees")).ifRight(r=>{r.produits.forEach(t=>{k.ifRight(A=>{T.fromNullable(A.find(i=>i.getAttribute(G)===t.key)).ifJust(i=>{const _=v(i),R=_(Et),E=_(j);R.textContent=`${String(Number(t.totals.line_total)/100)} €`,E.setAttribute("value",String(t.quantity)),S(A)}),Nt.textContent=`${String(r.totalPanier)} €`,Ot.textContent=`${String(r.totalPanier)} €`})})}).ifLeft(r=>{p(r),k.ifRight(t=>S(t))})},document.addEventListener("visibilitychange",()=>{o.close()})},Pt=()=>{Q.addEventListener("click",()=>{T.fromFalsy(Q.checked).ifJust(()=>{N.removeAttribute(D),W(N)("input, select").ifRight(o=>o.forEach(e=>e.removeAttribute(m)))}).ifNothing(()=>{N.setAttribute(D,""),W(N)("input, select").ifRight(o=>o.forEach(e=>{e.setAttribute(m,""),e.value=""}))})})},Ut=()=>{V.addEventListener("click",o=>{T.fromFalsy(M.checkValidity()).ifJust(()=>{o.preventDefault(),M.removeAttribute(D),V.textContent="Check-out"}).ifJust(()=>{const e=a(new FormData(M),t=>t.entries(),t=>Array.from(t),t=>H.fromPairs(t)),r={shipping_address:{address_1:e["livraison-adresse"]??"",address_2:"",city:e["livraison-ville"]??"",company:"",country:"FR",first_name:e["livraison-prenom"]??"",last_name:e["livraison-nom"]??"",phone:e["livraison-telephone"]??"",postcode:e["livraison-code-postal"]??"",state:e["livraison-region-etat"]??""}};U.encase(()=>I(Tt,r)).map(async t=>{await C({corps:JSON.stringify(t),nonce:b.nonce,route:X}).then(async A=>{O(A)&&f("500 Server Error"),a(await A.json(),i=>d(i,L),P.tap(i=>{const _=i.totals.total_shipping==="0"?"Free":`${String(i.totals.total_shipping)} €`,R=$(i.shipping_rates[0]?.shipping_rates[0]?.name).with("Mondial Relay - Livraison Point Relais (Gratuit)",()=>"Mondial Relay - Pickup Point").otherwise(()=>"");It.textContent=_,ft.textContent=R}))})}).ifLeft(t=>console.error(t.issues))})})};document.addEventListener("DOMContentLoaded",()=>{Ct(),dt(),Lt(),Pt(),Ut()});