2024-10-07

This commit is contained in:
gcch 2024-10-07 15:58:56 +02:00
commit 03d86b3a64
41 changed files with 610 additions and 418 deletions

View file

@ -1,22 +1,23 @@
import { r, z, N, p } from "./index.y02cst4L.js";
import { r } from "./chunk-RU7WR4KH.D5j7HXCF.js";
import { a as recupereElementsDocumentEither, r as recupereElementDocumentEither, p, b as propEither } from "./utils.js";
import { x } from "./chunk-7BKSRZNG.C39W3Wne.js";
import { p as parse } from "./index.DD7qm8S6.js";
import { R as ROUTE_API_RETIRE_ARTICLE_PANIER, b as ROUTE_API_MAJ_ARTICLE_PANIER } from "./api2.js";
import { a as SELECTEUR_ENTREES_PANIER, b as SELECTEUR_CONTENEUR_PANIER, c as SELECTEUR_TOTAL_PANIER, d as ATTRIBUT_CLE_PANIER, e as SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE, f as SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER, g as SELECTEUR_BOUTON_ADDITION_QUANTITE, h as SELECTEUR_BOUTON_SUPPRESSION_PANIER, A as ATTRIBUT_CONTIENT_ARTICLES, i as SELECTEUR_PRIX_LIGNE_PANIER } from "./dom.js";
import { N as NOM_CANAL_BOUTON_PANIER, a as NOM_CANAL_CONTENU_PANIER, T as TYPES_MESSAGES } from "./messages3.js";
import { a as SELECTEUR_ENTREES_PANIER, b as SELECTEUR_CONTENEUR_PANIER, c as SELECTEUR_SOUS_TOTAL_PANIER, d as SELECTEUR_TOTAL_PANIER, e as ATTRIBUT_CLE_PANIER, f as SELECTEUR_BOUTON_SOUSTRACTION_QUANTITE, g as SELECTEUR_CHAMP_QUANTITE_LIGNE_PANIER, h as SELECTEUR_BOUTON_ADDITION_QUANTITE, i as SELECTEUR_BOUTON_SUPPRESSION_PANIER, A as ATTRIBUT_CONTIENT_ARTICLES, j as SELECTEUR_PRIX_LIGNE_PANIER, k 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 recupereElementAvecSelecteur } 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 { l as leveServerError, r as reporteErreur } from "./erreurs.js";
import { e as estReponse500, a as estError } from "./gardes.js";
import { v as valideMessageMajBoutonPanier, a as valideMessageMajContenuPanier } from "./messages.js";
import { p as postBackendWC } from "./reseau.js";
import { v as valideMessageMajBoutonPanier, a as valideMessageMajContenuPanier, e as emetMessageMajBoutonPanier, b as emetMessageMajContenuPanier } from "./messages.js";
import { p as postBackend, t as traiteReponseBackendWCSelonCodesHTTP } from "./reseau.js";
import { W as WCStoreCartRemoveItemArgsSchema } from "./cart-remove-item.js";
import { W as WCStoreCartUpdateItemArgsSchema } from "./cart-update-item.js";
import { W as WCStoreCartSchema } from "./cart.js";
import { e as estWCError } from "./erreurs2.js";
import { a as recupereElementsDocumentEither, r as recupereElementDocumentEither } from "./utils.js";
import { M as Maybe, E as Either } from "./Either.wHNxn7Os.js";
import "./exports.DNZBdkMD.js";
import "./messages2.js";
import "./index.CeK6pfoJ.js";
import "./erreurs2.js";
import "./cart2.js";
const ETATS_PAGE = _etats;
const ENTREES_PANIER_EITHER = recupereElementsDocumentEither(
@ -26,21 +27,46 @@ const CONTENEUR_PANIER = x(
recupereElementDocumentEither(SELECTEUR_CONTENEUR_PANIER),
recupereElementOuLeve
);
const SOUS_TOTAL_PANIER = x(
recupereElementDocumentEither(SELECTEUR_SOUS_TOTAL_PANIER),
recupereElementOuLeve
);
const TOTAL_PANIER = x(
recupereElementDocumentEither(SELECTEUR_TOTAL_PANIER),
recupereElementOuLeve
);
const recupereElementDansEntreePanierOuLeve = (
/* eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- Nécessaire pour
documenter le type d'Element attendu */
(entree) => (selecteur) => x(
recupereElementAvecSelecteur(entree)(selecteur),
recupereElementOuLeve
)
const recupereElementDansEntreePanierOuLeve = (entree) => (selecteur) => x(
recupereElementAvecSelecteur(entree)(selecteur),
recupereElementOuLeve
);
const initialiseScriptsPagePanier = () => {
ENTREES_PANIER_EITHER.ifRight(
(entrees) => entrees.forEach((entree) => {
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();
@ -51,10 +77,12 @@ const initialiseScriptsPagePanier = () => {
const boutonAddition = recupereElementDansEntree(SELECTEUR_BOUTON_ADDITION_QUANTITE);
const boutonSuppression = recupereElementDansEntree(SELECTEUR_BOUTON_SUPPRESSION_PANIER);
boutonSuppression.addEventListener("click", () => {
boutonSuppression.textContent = "C= C= C= C= C=┌(;・ω・)┘";
Either.encase(() => parse(WCStoreCartRemoveItemArgsSchema, { key: clePanier })).map(
(args) => {
postBackendWC({
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
@ -64,18 +92,17 @@ const initialiseScriptsPagePanier = () => {
// 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)),
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
// Émets des Messages via BroadcastChannels pour la mise à jour de la page
r((panier) => {
new BroadcastChannel(NOM_CANAL_CONTENU_PANIER).postMessage({
donnees: {
produits: panier.items,
totalPanier: Number(panier.totals.total_items / 100)
},
type: TYPES_MESSAGES.MajContenuPanier
});
new BroadcastChannel(NOM_CANAL_BOUTON_PANIER).postMessage({
donnees: panier.items_count,
type: TYPES_MESSAGES.MajBoutonPanier
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();
})
@ -86,21 +113,19 @@ const initialiseScriptsPagePanier = () => {
} 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) => {
boutonSuppression.textContent = "C= C= C= C= C=┌(;・ω・)┘";
boutonSoustraction.toggleAttribute("disabled");
boutonAddition.toggleAttribute("disabled");
boutonSuppression.toggleAttribute("disabled");
Either.encase(
() => parse(WCStoreCartUpdateItemArgsSchema, { key: clePanier, quantity: valeur - 1 })
).map(
(args) => {
postBackendWC({
async (args) => {
desactiveBoutonsEntreesPanier(entrees);
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_MAJ_ARTICLE_PANIER
@ -110,23 +135,17 @@ const initialiseScriptsPagePanier = () => {
// 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)),
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
r((panier) => {
new BroadcastChannel(NOM_CANAL_BOUTON_PANIER).postMessage({
donnees: panier.items_count,
type: TYPES_MESSAGES.MajBoutonPanier
emetMessageMajBoutonPanier({ quantiteProduits: panier.items_count });
emetMessageMajContenuPanier({
produits: panier.items,
sousTotalPanier: Number(panier.totals.total_price) / 100,
totalPanier: Number(panier.totals.total_items) / 100
});
new BroadcastChannel(NOM_CANAL_CONTENU_PANIER).postMessage({
donnees: {
produits: panier.items,
totalPanier: Number(panier.totals.total_items / 100)
},
type: TYPES_MESSAGES.MajContenuPanier
});
boutonSoustraction.toggleAttribute("disabled");
boutonAddition.toggleAttribute("disabled");
boutonSuppression.toggleAttribute("disabled");
boutonSuppression.textContent = "Remove";
})
);
}).catch((e) => {
@ -135,6 +154,7 @@ const initialiseScriptsPagePanier = () => {
} else {
console.error("e n'est pas une Erreur ?!", e);
}
majEtatsActivationBoutons(entrees);
});
}
);
@ -142,15 +162,12 @@ const initialiseScriptsPagePanier = () => {
});
boutonAddition.addEventListener("click", () => {
Maybe.fromNullable(champQuantite.valueAsNumber).ifJust((valeur) => {
boutonSuppression.textContent = "C= C= C= C= C=┌(;・ω・)┘";
boutonSoustraction.toggleAttribute("disabled");
boutonAddition.toggleAttribute("disabled");
boutonSuppression.toggleAttribute("disabled");
desactiveBoutonsEntreesPanier(entrees);
Either.encase(
() => parse(WCStoreCartUpdateItemArgsSchema, { key: clePanier, quantity: valeur + 1 })
).map(
(args) => {
postBackendWC({
async (args) => {
await postBackend({
corps: JSON.stringify(args),
nonce: ETATS_PAGE.nonce,
route: ROUTE_API_MAJ_ARTICLE_PANIER
@ -160,23 +177,17 @@ const initialiseScriptsPagePanier = () => {
// 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)),
(corpsReponse) => traiteReponseBackendWCSelonCodesHTTP(
corpsReponse,
WCStoreCartSchema
),
r((panier) => {
new BroadcastChannel(NOM_CANAL_BOUTON_PANIER).postMessage({
donnees: panier.items_count,
type: TYPES_MESSAGES.MajBoutonPanier
emetMessageMajBoutonPanier({ quantiteProduits: panier.items_count });
emetMessageMajContenuPanier({
produits: panier.items,
sousTotalPanier: Number(panier.totals.total_price) / 100,
totalPanier: Number(panier.totals.total_items) / 100
});
new BroadcastChannel(NOM_CANAL_CONTENU_PANIER).postMessage({
donnees: {
produits: panier.items,
totalPanier: Number(panier.totals.total_items / 100)
},
type: TYPES_MESSAGES.MajContenuPanier
});
boutonSoustraction.toggleAttribute("disabled");
boutonAddition.toggleAttribute("disabled");
boutonSuppression.toggleAttribute("disabled");
boutonSuppression.textContent = "Remove";
})
);
}).catch((e) => {
@ -185,25 +196,25 @@ const initialiseScriptsPagePanier = () => {
} else {
console.error("e n'est pas une Erreur ?!", e);
}
majEtatsActivationBoutons(entrees);
});
}
);
});
});
})
);
});
});
};
const initialiseMajConteneurPanier = () => {
const CANAL_BOUTON_PANIER = new BroadcastChannel(NOM_CANAL_BOUTON_PANIER);
CANAL_BOUTON_PANIER.onmessage = (evenementMessage) => {
valideMessageMajBoutonPanier(evenementMessage).ifRight((message) => {
CONTENEUR_PANIER.setAttribute(ATTRIBUT_CONTIENT_ARTICLES, String(message.donnees !== 0));
new BroadcastChannel(NOM_CANAL_BOUTON_PANIER).onmessage = (evenementMessage) => {
valideMessageMajBoutonPanier(evenementMessage).map(p("donnees")).ifRight((donnees) => {
CONTENEUR_PANIER.setAttribute(ATTRIBUT_CONTIENT_ARTICLES, String(donnees.quantiteProduits !== 0));
});
};
};
const initialiseEcouteCanalMajContenuPanier = () => {
const initialiseMajContenuPanier = () => {
new BroadcastChannel(NOM_CANAL_CONTENU_PANIER).onmessage = (evenementMessage) => {
valideMessageMajContenuPanier(evenementMessage).map(p("donnees")).ifRight((donnees) => {
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) => {
@ -214,16 +225,21 @@ const initialiseEcouteCanalMajContenuPanier = () => {
);
prixLigne.textContent = `${String(Number(ligne.totals.line_total) / 100)} €`;
champQuantite.setAttribute("value", String(ligne.quantity));
TOTAL_PANIER.textContent = `${donnees.totalPanier} €`;
majEtatsActivationBoutons(entrees);
});
SOUS_TOTAL_PANIER.textContent = `${String(donnees.totalPanier)} €`;
TOTAL_PANIER.textContent = `${String(donnees.totalPanier)} €`;
});
});
}).ifLeft((erreur) => {
reporteErreur(erreur);
ENTREES_PANIER_EITHER.ifRight((entrees) => majEtatsActivationBoutons(entrees));
});
};
};
document.addEventListener("DOMContentLoaded", () => {
initialiseScriptsPagePanier();
initialiseMajEntreesPanier();
initialiseMajConteneurPanier();
initialiseEcouteCanalMajContenuPanier();
initialiseMajContenuPanier();
});
//# sourceMappingURL=scripts-page-panier.js.map