haiku-atelier-2024/web/app/themes/haiku-atelier-2024/assets/js/scripts-page-produit.js
2024-10-02 23:54:52 +02:00

2 lines
2.4 KiB
JavaScript

import{r as u,p as C,z as f,N as n}from"./index.Dxgx1GXj.js";import{x as o}from"./chunk-7BKSRZNG.F4GWJRq9.js";import{p as O}from"./index.C9ScFdVV.js";import{b as I,c as L}from"./api2.js";import{g,h,i as U,j as v,k as m,l as d,m as A}from"./dom.js";import{N as R,T as b}from"./messages3.js";import{r as T,a as p}from"./dom2.js";import{l as P,a as w,b as y,c as B,E as D,d as M,r as J}from"./erreurs.js";import{b as j,e as x,a as W}from"./gardes.js";import{p as G}from"./cart-add-item.js";import{W as k}from"./cart.js";import{i as c}from"./erreurs2.js";import{r as l,a as N}from"./utils.js";import{M as q,E as z}from"./Either.DDEPhGsy.js";import"./exports.BNibT8R5.js";const H=_etats,V=a=>{a.forEach(e=>{e[0].setAttribute(m,"false"),e[1].setAttribute(d,"true")})},s=o(l(g),T),i=o(l(h),T),F=o(N(U),p),Q=o(N(v),p),Y=()=>{const a=new Map;F.forEach((e,t)=>{const r=e.getAttribute("id"),E=Q[t];if(!r)throw new Error("Le lien ne dispose pas d'ID !");if(!E)throw new Error("Le lien ne dispose pas de section correspondante !");a.set(r,[e,E]),e.addEventListener("click",_=>{_.preventDefault();const S=e.getAttribute(m)==="true";V(o(a.values(),Array.from)),!S&&(e.setAttribute(m,"true"),E.removeAttribute(d))})}),i.addEventListener("change",e=>{o(e.target,q.fromNullable,t=>t.filter(j),t=>t.map(r=>r.validity.valid),t=>t.ifJust(r=>s.toggleAttribute(A,!r)))}),s.addEventListener("click",e=>K())},K=()=>{s.textContent="Adding...",z.encase(()=>G({id:Number(i.value),quantity:1})).map(a=>{fetch(I,{body:JSON.stringify(a),credentials:"same-origin",headers:{Accept:"application/json","Content-Type":"application/json",[L]:H.nonce},method:"POST",mode:"same-origin",signal:AbortSignal.timeout(5e3)}).then(async e=>{x(e)&&P("500 Server Error"),o(await e.json(),t=>f(t).with({body:n.select(),status:400},c,w).with({body:n.select(),status:401},c,y).with({body:n.select(),status:404},c,B).with(n._,r=>O(k,r)).otherwise(r=>o(r,D,M)),u(t=>console.debug("Panier",t)),C("items_count"),u(t=>{s.textContent="Added to cart!",new BroadcastChannel(R).postMessage({donnees:t,type:b.MiseAJourPanier}),setTimeout(()=>{i.value=i.options.item(0)?.value??"--",s.toggleAttribute(A,!0),s.textContent="Add to cart"},3e3)}))}).catch(e=>{W(e)?(J(e),console.error(e)):console.error("e n'est pas une Erreur ?!",e),s.textContent="Add to cart"})})};document.addEventListener("DOMContentLoaded",()=>{Y()});
//# sourceMappingURL=scripts-page-produit.js.map