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

356 lines
17 KiB
JavaScript

import { z } from "./index.CeK6pfoJ.js";
import { p as parse } from "./index.Dco04HYW.js";
import { a as ROUTE_API_RETIRE_ARTICLE_PANIER, b as ROUTE_API_MAJ_ARTICLE_PANIER, c as ROUTE_API_MAJ_CLIENT } from "./api.js";
import { j as SELECTEUR_ENTREES_PANIER, k as SELECTEUR_CONTENEUR_PANIER, l as SELECTEUR_SOUS_TOTAL_PRODUITS, m as SELECTEUR_SOUS_TOTAL_LIVRAISON_COUT, n as SELECTEUR_SOUS_TOTAL_LIVRAISON_PRESTATAIRE, o as SELECTEUR_TOTAL_PANIER, p as SELECTEUR_BOUTON_SEPARATION_ADRESSES, q as SELECTEUR_FORMULAIRE_PANIER, r as SELECTEUR_FORMULAIRE_FACTURATION, s as SELECTEUR_BOUTON_ACTIONS_FORMULAIRE, t as ATTRIBUT_CLE_PANIER, u as SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE, v as SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER, w as SELECTEUR_BOUTON_ADDITION_QUANTITE, x as SELECTEUR_BOUTON_SUPPRESSION_PANIER, A as ATTRIBUT_CONTIENT_ARTICLES, y as SELECTEUR_PRIX_LIGNE_PANIER, i as ATTRIBUT_HIDDEN, z as ATTRIBUT_DESACTIVE } from "./dom.js";
import { N as NOM_CANAL_BOUTON_PANIER, a as NOM_CANAL_CONTENU_PANIER } from "./messages3.js";
import { r as recupereElementOuLeve, a as recupereElementsAvecSelecteur, b as recupereElementAvecSelecteur } from "./dom2.js";
import { l as leveServerError, r as reporteErreur } from "./erreurs.js";
import { e as estReponse500, a as estError } from "./gardes.js";
import { e as emetMessageMajBoutonPanier, a as emetMessageMajContenuPanier, v as valideMessageMajBoutonPanier, b as valideMessageMajContenuPanier } from "./messages.js";
import { p as postBackend, t as traiteReponseBackendWCSelonCodesHTTP } from "./reseau.js";
import { W as WCStoreCartSchema } from "./cart.js";
import { W as WCStoreCartRemoveItemArgsSchema } from "./cart-remove-item.js";
import { W as WCStoreCartUpdateCustomerArgsSchema } from "./cart-update-customer.js";
import { W as WCStoreCartUpdateItemArgsSchema } from "./cart-update-item.js";
import { c as recupereElementsDocumentEither, r as recupereElementDocumentEither, y, p as propEither } from "./utils.js";
import { p as pipe } from "./pipe.XPB0wEfw.js";
import { l } from "./index-0eef19ec.DJngGX7K.js";
import { M as Maybe, E as Either } from "./Either.wHNxn7Os.js";
import "./exports.BuUzFliQ.js";
import "./messages2.js";
import "./erreurs2.js";
import "./cart2.js";
import "./adresses.js";
import "./couts-livraison.js";
const ETATS_PAGE = _etats;
const ENTREES_PANIER_EITHER = recupereElementsDocumentEither(
SELECTEUR_ENTREES_PANIER
);
const CONTENEUR_PANIER = pipe(
recupereElementDocumentEither(SELECTEUR_CONTENEUR_PANIER),
recupereElementOuLeve
);
const SOUS_TOTAL_PRODUITS = pipe(
recupereElementDocumentEither(SELECTEUR_SOUS_TOTAL_PRODUITS),
recupereElementOuLeve
);
const SOUS_TOTAL_LIVRAISON_COUT = pipe(
recupereElementDocumentEither(SELECTEUR_SOUS_TOTAL_LIVRAISON_COUT),
recupereElementOuLeve
);
const SOUS_TOTAL_LIVRAISON_PRESTATAIRE = pipe(
recupereElementDocumentEither(SELECTEUR_SOUS_TOTAL_LIVRAISON_PRESTATAIRE),
recupereElementOuLeve
);
const TOTAL_PANIER = pipe(
recupereElementDocumentEither(SELECTEUR_TOTAL_PANIER),
recupereElementOuLeve
);
const BOUTON_SEPARATION_ADRESSES = pipe(
recupereElementDocumentEither(SELECTEUR_BOUTON_SEPARATION_ADRESSES),
recupereElementOuLeve
);
const FORMULAIRE_PANIER = pipe(
recupereElementDocumentEither(SELECTEUR_FORMULAIRE_PANIER),
recupereElementOuLeve
);
const FORMULAIRE_FACTURATION = pipe(
recupereElementDocumentEither(SELECTEUR_FORMULAIRE_FACTURATION),
recupereElementOuLeve
);
const BOUTON_ACTIONS_FORMULAIRE = pipe(
recupereElementDocumentEither(SELECTEUR_BOUTON_ACTIONS_FORMULAIRE),
recupereElementOuLeve
);
const recupereElementDansEntreePanierOuLeve = (entree) => (selecteur) => pipe(
recupereElementAvecSelecteur(entree)(selecteur),
recupereElementOuLeve
);
const desactiveBoutonsEntreesPanier = (entrees) => entrees.forEach((entree) => {
const recupereElementDansEntree = recupereElementDansEntreePanierOuLeve(entree);
const elements = {
boutonAddition: recupereElementDansEntree(SELECTEUR_BOUTON_ADDITION_QUANTITE),
boutonSoustraction: recupereElementDansEntree(SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE),
boutonSuppression: recupereElementDansEntree(SELECTEUR_BOUTON_SUPPRESSION_PANIER)
};
elements.boutonSoustraction.setAttribute(ATTRIBUT_DESACTIVE, "");
elements.boutonAddition.setAttribute(ATTRIBUT_DESACTIVE, "");
elements.boutonSuppression.setAttribute(ATTRIBUT_DESACTIVE, "");
elements.boutonSuppression.textContent = "C= C= C= C= C=┌(;・ω・)┘";
});
const majEtatsActivationBoutons = (entrees) => entrees.forEach((entree) => {
const recupereElementDansEntree = recupereElementDansEntreePanierOuLeve(entree);
const elements = {
boutonAddition: recupereElementDansEntree(SELECTEUR_BOUTON_ADDITION_QUANTITE),
boutonSoustraction: recupereElementDansEntree(SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE),
boutonSuppression: recupereElementDansEntree(SELECTEUR_BOUTON_SUPPRESSION_PANIER),
champQuantite: recupereElementDansEntree(SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER)
};
Number(elements.champQuantite?.value) === 1 ? elements.boutonSoustraction.setAttribute(ATTRIBUT_DESACTIVE, "") : elements.boutonSoustraction.removeAttribute(ATTRIBUT_DESACTIVE);
elements.boutonAddition.removeAttribute(ATTRIBUT_DESACTIVE);
elements.boutonSuppression.removeAttribute(ATTRIBUT_DESACTIVE);
elements.boutonSuppression.textContent = "Remove";
});
const initialiseMajEntreesPanier = () => {
ENTREES_PANIER_EITHER.ifRight((entrees) => {
entrees.forEach((entree) => {
const recupereElementDansEntree = recupereElementDansEntreePanierOuLeve(entree);
const clePanier = Maybe.fromNullable(entree.getAttribute(ATTRIBUT_CLE_PANIER)).ifNothing(() => {
entree.remove();
return;
}).orDefault("CLE_PANIER_INEXISTANTE");
const boutonSoustraction = recupereElementDansEntree(SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE);
const champQuantite = recupereElementDansEntree(SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER);
const boutonAddition = recupereElementDansEntree(SELECTEUR_BOUTON_ADDITION_QUANTITE);
const boutonSuppression = recupereElementDansEntree(SELECTEUR_BOUTON_SUPPRESSION_PANIER);
boutonSuppression.addEventListener("click", () => {
Either.encase(
() => parse(WCStoreCartRemoveItemArgsSchema, { key: clePanier })
).map(
async (args) => {
desactiveBoutonsEntreesPanier(entrees);
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_RETIRE_ARTICLE_PANIER
}).then(async (reponse) => {
if (estReponse500(reponse)) leveServerError("500 Server Error");
pipe(
// Récupère la Réponse
await reponse.json(),
// Traite tous les codes HTTPs possibles
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
// Émets des Messages via BroadcastChannels pour la mise à jour de la page
l.tap((panier) => {
emetMessageMajBoutonPanier({ quantiteProduits: panier.items_count });
emetMessageMajContenuPanier({
produits: panier.items,
sousTotalPanier: Number(panier.totals.total_price) / 100,
totalPanier: Number(panier.totals.total_items) / 100
});
entree.remove();
})
);
}).catch((e) => {
if (estError(e)) {
reporteErreur(e);
} else {
console.error("e n'est pas une Erreur ?!", e);
}
majEtatsActivationBoutons(entrees);
});
}
).ifLeft((erreur) => reporteErreur(erreur));
});
boutonSoustraction.addEventListener("click", () => {
Maybe.fromNullable(champQuantite.valueAsNumber).filter((valeur) => valeur > 1).ifJust((valeur) => {
Either.encase(
() => parse(WCStoreCartUpdateItemArgsSchema, { key: clePanier, quantity: valeur - 1 })
).map(
async (args) => {
desactiveBoutonsEntreesPanier(entrees);
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_MAJ_ARTICLE_PANIER
}).then(async (reponse) => {
if (estReponse500(reponse)) leveServerError("500 Server Error");
pipe(
// Récupère la Réponse
await reponse.json(),
// Traite tous les codes HTTPs possibles
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
l.tap((panier) => {
emetMessageMajBoutonPanier({ quantiteProduits: panier.items_count });
emetMessageMajContenuPanier({
produits: panier.items,
sousTotalPanier: Number(panier.totals.total_price) / 100,
totalPanier: Number(panier.totals.total_items) / 100
});
})
);
}).catch((e) => {
if (estError(e)) {
reporteErreur(e);
} else {
console.error("e n'est pas une Erreur ?!", e);
}
majEtatsActivationBoutons(entrees);
});
}
);
});
});
boutonAddition.addEventListener("click", () => {
Maybe.fromNullable(champQuantite.valueAsNumber).ifJust((valeur) => {
desactiveBoutonsEntreesPanier(entrees);
Either.encase(
() => parse(WCStoreCartUpdateItemArgsSchema, { key: clePanier, quantity: valeur + 1 })
).map(
async (args) => {
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_MAJ_ARTICLE_PANIER
}).then(async (reponse) => {
if (estReponse500(reponse)) leveServerError("500 Server Error");
pipe(
// Récupère la Réponse
await reponse.json(),
// Traite tous les codes HTTPs possibles
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
l.tap((panier) => {
emetMessageMajBoutonPanier({ quantiteProduits: panier.items_count });
emetMessageMajContenuPanier({
produits: panier.items,
sousTotalPanier: Number(panier.totals.total_price) / 100,
totalPanier: Number(panier.totals.total_items) / 100
});
})
);
}).catch((e) => {
if (estError(e)) {
reporteErreur(e);
} else {
console.error("e n'est pas une Erreur ?!", e);
}
majEtatsActivationBoutons(entrees);
});
}
);
});
});
});
});
};
const initialiseMajConteneurPanier = () => {
const canal = new BroadcastChannel(NOM_CANAL_BOUTON_PANIER);
canal.onmessage = (evenementMessage) => {
valideMessageMajBoutonPanier(evenementMessage).map(y.getUnsafe("donnees")).ifRight((donnees) => {
CONTENEUR_PANIER.setAttribute(ATTRIBUT_CONTIENT_ARTICLES, String(donnees.quantiteProduits !== 0));
});
};
document.addEventListener("visibilitychange", () => {
canal.close();
});
};
const initialiseMajContenuPanier = () => {
const canal = new BroadcastChannel(NOM_CANAL_CONTENU_PANIER);
canal.onmessage = (evenementMessage) => {
valideMessageMajContenuPanier(evenementMessage).chain(propEither("donnees")).ifRight((donnees) => {
donnees.produits.forEach((ligne) => {
ENTREES_PANIER_EITHER.ifRight((entrees) => {
Maybe.fromNullable(entrees.find((entree) => entree.getAttribute(ATTRIBUT_CLE_PANIER) === ligne.key)).ifJust((entree) => {
const recupereElementDansEntree = recupereElementDansEntreePanierOuLeve(entree);
const prixLigne = recupereElementDansEntree(SELECTEUR_PRIX_LIGNE_PANIER);
const champQuantite = recupereElementDansEntree(
SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER
);
prixLigne.textContent = `${String(Number(ligne.totals.line_total) / 100)} €`;
champQuantite.setAttribute("value", String(ligne.quantity));
majEtatsActivationBoutons(entrees);
});
SOUS_TOTAL_PRODUITS.textContent = `${String(donnees.totalPanier)} €`;
TOTAL_PANIER.textContent = `${String(donnees.totalPanier)} €`;
});
});
}).ifLeft((erreur) => {
reporteErreur(erreur);
ENTREES_PANIER_EITHER.ifRight((entrees) => majEtatsActivationBoutons(entrees));
});
};
document.addEventListener("visibilitychange", () => {
canal.close();
});
};
const initialiseMajFormulairesPanier = () => {
BOUTON_SEPARATION_ADRESSES.addEventListener("click", () => {
Maybe.fromFalsy(BOUTON_SEPARATION_ADRESSES.checked).ifJust(() => {
FORMULAIRE_FACTURATION.removeAttribute(ATTRIBUT_HIDDEN);
recupereElementsAvecSelecteur(FORMULAIRE_FACTURATION)("input, select").ifRight((champs) => champs.forEach((champ) => champ.removeAttribute(ATTRIBUT_DESACTIVE)));
}).ifNothing(() => {
FORMULAIRE_FACTURATION.setAttribute(ATTRIBUT_HIDDEN, "");
recupereElementsAvecSelecteur(FORMULAIRE_FACTURATION)("input, select").ifRight(
(champs) => champs.forEach((champ) => {
champ.setAttribute(ATTRIBUT_DESACTIVE, "");
champ.value = "";
})
);
});
});
};
const initialiseBoutonActions = () => {
BOUTON_ACTIONS_FORMULAIRE.addEventListener("click", (evenement) => {
Maybe.fromFalsy(FORMULAIRE_PANIER.checkValidity()).ifJust(() => {
evenement.preventDefault();
FORMULAIRE_PANIER.removeAttribute(ATTRIBUT_HIDDEN);
BOUTON_ACTIONS_FORMULAIRE.textContent = "Check-out";
}).ifJust(() => {
const donneesFormulaireLivraison = pipe(
new FormData(FORMULAIRE_PANIER),
(formData) => formData.entries(),
(entrees) => Array.from(entrees),
(tuple) => y.fromPairs(tuple)
);
const argumentsFormulaire = {
shipping_address: {
address_1: donneesFormulaireLivraison["livraison-adresse"] ?? "",
address_2: "",
city: donneesFormulaireLivraison["livraison-ville"] ?? "",
company: "",
country: "FR",
first_name: donneesFormulaireLivraison["livraison-prenom"] ?? "",
last_name: donneesFormulaireLivraison["livraison-nom"] ?? "",
phone: donneesFormulaireLivraison["livraison-telephone"] ?? "",
postcode: donneesFormulaireLivraison["livraison-code-postal"] ?? "",
state: donneesFormulaireLivraison["livraison-region-etat"] ?? ""
}
};
Either.encase(
() => parse(WCStoreCartUpdateCustomerArgsSchema, argumentsFormulaire)
).map(
async (args) => {
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_MAJ_CLIENT
}).then(async (reponse) => {
if (estReponse500(reponse)) leveServerError("500 Server Error");
pipe(
// Récupère la Réponse
await reponse.json(),
// Traite tous les codes HTTPs possibles
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
l.tap((donnees) => {
const sousTotalLivraison = donnees.totals.total_shipping === "0" ? "Free" : `${String(donnees.totals.total_shipping)} €`;
const sousTotalPrestataire = z(donnees.shipping_rates[0]?.shipping_rates[0]?.name).with("Mondial Relay - Livraison Point Relais (Gratuit)", () => "Mondial Relay - Pickup Point").otherwise(() => "");
SOUS_TOTAL_LIVRAISON_COUT.textContent = sousTotalLivraison;
SOUS_TOTAL_LIVRAISON_PRESTATAIRE.textContent = sousTotalPrestataire;
})
);
});
}
).ifLeft((a) => console.error(a.issues));
});
});
};
document.addEventListener("DOMContentLoaded", () => {
initialiseMajEntreesPanier();
initialiseMajConteneurPanier();
initialiseMajContenuPanier();
initialiseMajFormulairesPanier();
initialiseBoutonActions();
});
//# sourceMappingURL=scripts-page-panier.js.map