128 lines
5.6 KiB
JavaScript
128 lines
5.6 KiB
JavaScript
import { r } from "./chunk-RU7WR4KH.D5j7HXCF.js";
|
|
import { r as recupereElementDocumentEither, a as recupereElementsDocumentEither, p } from "./utils.js";
|
|
import { x } from "./chunk-7BKSRZNG.C39W3Wne.js";
|
|
import { z, N } from "./index.CeK6pfoJ.js";
|
|
import { p as parse } from "./index.DD7qm8S6.js";
|
|
import { c as ROUTE_API_AJOUTE_ARTICLE_PANIER, d as ENTETE_WC_NONCE } from "./api2.js";
|
|
import { l as SELECTEUR_BOUTON_AJOUT_PANIER, m as SELECTEUR_SELECTEUR_QUANTITE, n as SELECTEUR_LIENS_ONGLETS, o as SELECTEUR_SECTIONS_CONTENUS, p as ATTRIBUT_ARIA_SELECTED, q as ATTRIBUT_HIDDEN, k as ATTRIBUT_DESACTIVE } from "./dom.js";
|
|
import { r as recupereElementOuLeve, b as recupereElementsOuLeve } from "./dom2.js";
|
|
import { l as leveServerError, a as leveBadRequestError, b as leveUnauthorizedError, c as leveNotFoundError, E as ErreurInconnue, d as leveErreur, r as reporteErreur } from "./erreurs.js";
|
|
import { b as estHTMLSelectElement, e as estReponse500, a as estError } from "./gardes.js";
|
|
import { e as emetMessageMajBoutonPanier } from "./messages.js";
|
|
import { p as parseWCStoreCartAddItemArgs } from "./cart-add-item.js";
|
|
import { W as WCStoreCartSchema } from "./cart.js";
|
|
import { e as estWCError } from "./erreurs2.js";
|
|
import { M as Maybe, E as Either } from "./Either.wHNxn7Os.js";
|
|
import "./exports.DNZBdkMD.js";
|
|
import "./messages3.js";
|
|
import "./messages2.js";
|
|
import "./cart2.js";
|
|
const ETATS_PAGE = _etats;
|
|
const deplieToutesSections = (ensembleLiensContenus) => {
|
|
ensembleLiensContenus.forEach((ensemble) => {
|
|
ensemble[0].setAttribute(ATTRIBUT_ARIA_SELECTED, "false");
|
|
ensemble[1].setAttribute(ATTRIBUT_HIDDEN, "true");
|
|
});
|
|
};
|
|
const BOUTON_AJOUT_PANIER = x(
|
|
recupereElementDocumentEither(SELECTEUR_BOUTON_AJOUT_PANIER),
|
|
recupereElementOuLeve
|
|
);
|
|
const SELECTEUR_VARIATION = x(
|
|
recupereElementDocumentEither(SELECTEUR_SELECTEUR_QUANTITE),
|
|
recupereElementOuLeve
|
|
);
|
|
const LIENS_ONGLETS = x(
|
|
recupereElementsDocumentEither(SELECTEUR_LIENS_ONGLETS),
|
|
recupereElementsOuLeve
|
|
);
|
|
const SECTIONS_CONTENUS = x(
|
|
recupereElementsDocumentEither(SELECTEUR_SECTIONS_CONTENUS),
|
|
recupereElementsOuLeve
|
|
);
|
|
const gereBoiteInformationsProduit = () => {
|
|
const onglets = /* @__PURE__ */ new Map();
|
|
LIENS_ONGLETS.forEach((lien, index) => {
|
|
const idOnglet = lien.getAttribute("id");
|
|
const sectionCorrespondante = SECTIONS_CONTENUS[index];
|
|
if (!idOnglet) throw new Error("Le lien ne dispose pas d'ID !");
|
|
if (!sectionCorrespondante) throw new Error("Le lien ne dispose pas de section correspondante !");
|
|
onglets.set(idOnglet, [lien, sectionCorrespondante]);
|
|
lien.addEventListener("click", (e) => {
|
|
e.preventDefault();
|
|
const estAncienOngletCourant = lien.getAttribute(ATTRIBUT_ARIA_SELECTED) === "true";
|
|
deplieToutesSections(x(onglets.values(), Array.from));
|
|
if (estAncienOngletCourant) return;
|
|
lien.setAttribute(ATTRIBUT_ARIA_SELECTED, "true");
|
|
sectionCorrespondante.removeAttribute(ATTRIBUT_HIDDEN);
|
|
});
|
|
});
|
|
SELECTEUR_VARIATION.addEventListener("change", (evenement) => {
|
|
x(
|
|
evenement.target,
|
|
Maybe.fromNullable,
|
|
(cibleEvenement) => cibleEvenement.filter(estHTMLSelectElement),
|
|
(element) => element.map((v) => v.validity.valid),
|
|
(validite) => validite.ifJust((v) => BOUTON_AJOUT_PANIER.toggleAttribute(ATTRIBUT_DESACTIVE, !v))
|
|
);
|
|
});
|
|
BOUTON_AJOUT_PANIER.addEventListener("click", (_) => ajouteProduitAuPanier());
|
|
};
|
|
const ajouteProduitAuPanier = () => {
|
|
BOUTON_AJOUT_PANIER.textContent = "Adding...";
|
|
Either.encase(
|
|
() => parseWCStoreCartAddItemArgs({
|
|
id: Number(SELECTEUR_VARIATION.value),
|
|
quantity: 1
|
|
})
|
|
).map((args) => {
|
|
fetch(
|
|
ROUTE_API_AJOUTE_ARTICLE_PANIER,
|
|
{
|
|
body: JSON.stringify(args),
|
|
credentials: "same-origin",
|
|
headers: {
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
[ENTETE_WC_NONCE]: ETATS_PAGE.nonce
|
|
},
|
|
method: "POST",
|
|
mode: "same-origin",
|
|
signal: AbortSignal.timeout(5e3)
|
|
}
|
|
).then(async (reponse) => {
|
|
if (estReponse500(reponse)) leveServerError("500 Server Error");
|
|
x(
|
|
// Récupère la Réponse
|
|
await reponse.json(),
|
|
// Traite tous les codes HTTPs possibles
|
|
(corpsReponse) => z(corpsReponse).with({ body: N.select(), status: 400 }, estWCError, leveBadRequestError).with({ body: N.select(), status: 401 }, estWCError, leveUnauthorizedError).with({ body: N.select(), status: 404 }, estWCError, leveNotFoundError).with(N._, (corpsOkInconnu) => parse(WCStoreCartSchema, corpsOkInconnu)).otherwise((e) => x(e, ErreurInconnue, leveErreur)),
|
|
// tap((panier: WCStoreCart) => console.debug("Panier", panier)),
|
|
// Récupère le nombre de Produits dans la Panier
|
|
p("items_count"),
|
|
// Déclenche les effets pour la mise à jour de l'IU
|
|
r((nombreArticlesPanier) => {
|
|
BOUTON_AJOUT_PANIER.textContent = "Added to cart!";
|
|
emetMessageMajBoutonPanier({ quantiteProduits: nombreArticlesPanier });
|
|
setTimeout(() => {
|
|
SELECTEUR_VARIATION.value = SELECTEUR_VARIATION.options.item(0)?.value ?? "--";
|
|
BOUTON_AJOUT_PANIER.toggleAttribute(ATTRIBUT_DESACTIVE, true);
|
|
BOUTON_AJOUT_PANIER.textContent = "Add to cart";
|
|
}, 3e3);
|
|
})
|
|
);
|
|
}).catch((e) => {
|
|
if (estError(e)) {
|
|
reporteErreur(e);
|
|
console.error(e);
|
|
} else {
|
|
console.error("e n'est pas une Erreur ?!", e);
|
|
}
|
|
BOUTON_AJOUT_PANIER.textContent = "Add to cart";
|
|
});
|
|
});
|
|
};
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
gereBoiteInformationsProduit();
|
|
});
|
|
//# sourceMappingURL=scripts-page-produit.js.map
|