UNPKG

fm-lp-factory

Version:

package created to build web ui components for FidelizarMais LP services

206 lines (182 loc) 6.62 kB
import { updateFmTemplateCss } from "../index.js"; import { basicArrowSpinningLoading } from "../../../assets/components/loaders.js"; /** * @param {Object} component * @param {String} component.subtitle * @param {String} component.title * @param {String} component.image * @param {String} component.valueCoupon * @param {String} component.point * @param {String} component.name * @param {String} component.id */ export default (component) => { const section = document.createElement("section"); css(); section.classList.add("fm-section-container", "fm-list-of-all-awards"); section.innerHTML = template .replace("%subtitle%", component.subtitle) .replace("%title%", component.title); document.querySelector("#fm-landing-page-content").appendChild(section); const sectionBody = section.querySelector(".fm-awards-container-body"); setTimeout(() => { sectionBody.innerHTML = ""; getAllPossibleAwards() .then((awards) => { if (!awards.length) { section.style.display = "none"; return; } awards.forEach((award) => { const card = document.createElement("div"); card.classList.add("fm-awards-card"); card.innerHTML = cardTemplate .replace("%cardName%", award.name) .replace("%cardPoints%", `${award.point} ${LoyalJS.storeData.rules.customization.pointsName}`) .replaceAll("%couponImage%", component.image === "request-image" ? award.image : component.image) .replace("%couponValue%", `R$ ${award.valueCoupon}`); if (award.valueCoupon === 0 && !component.image === "request-image") card.querySelector(".card-coupon-img").remove(); if (component.image === "request-image") card.querySelector(".card-coupon-value-money").remove(); sectionBody.appendChild(card); }); }) .catch((error) => { section.style.display = "none"; console.error(error); }); }, 100); } async function getAllPossibleAwards() { let awards = []; const categoryAwardIds = [ { id: "15f24a30-1d0e-4eff-80da-e997dfb921f8", isCoupon: false, limit: 100, offset: 1, awardName: "" }, ]; for (const categoryAwardId of categoryAwardIds) { const response = await LoyalJS.public.services().getAwards( { categoryAwardId: categoryAwardId.id, limit: categoryAwardId.limit, offset: categoryAwardId.offset, isCoupon: categoryAwardId.isCoupon, awardName: categoryAwardId.awardName, xApplication: "lp-rewards-redemption-products", } ) if (response.body.data.items.length > 0) { awards = [...awards, ...response.body.data.items]; } } return awards; } const awardsLoader = /*html*/ ` <div class="fm-awards-loader"> ${basicArrowSpinningLoading} </div> `; const template = /*html*/ ` <div class="fm-awards-container"> <div class="fm-awards-container-header" > <span class="subtitle">%subtitle%</span> <span class="title">%title%</span> </div> <div class="fm-awards-container-body"> ${awardsLoader} </div> </div> `; const cardTemplate = /*html*/ ` <div class="card-coupon-img"> <img src="%couponImage%" alt="%couponImage%"> <span class="card-coupon-value-money">%couponValue%</span> </div> <p >%cardName%</p> <div class="card-coupon-detail-extra"> <span class="card-coupon-value-points">%cardPoints%</span> </div> `; const css = () => { updateFmTemplateCss(/*css*/ ` .fm-section-container.fm-list-of-all-awards{ width: 90%; background-color: var(--fm-primary-color); border-radius: var(--fm-base-border-radius); } .fm-list-of-all-awards .fm-awards-container-header { color: var(--fm-secondary-button-text-color); display: flex; flex-direction: column; align-items: center; justify-content: center; margin-bottom: 30px; text-align: center; } .fm-list-of-all-awards .fm-awards-container-header .title { font-size: 1.5rem; font-weight: bold; } .fm-list-of-all-awards .fm-awards-container-header .subtitle { font-size: 1rem; } .fm-list-of-all-awards .fm-awards-container { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; margin: 40px; } .fm-awards-container .fm-awards-loader{ margin: 15px auto; } .fm-awards-container .fm-awards-loader img { width: 75px; } .fm-awards-container-body { display: flex; gap: 15px; flex-wrap: wrap; align-items: stretch; justify-content: center; } .fm-awards-container-body .fm-awards-card .card-coupon-img{ background-repeat: no-repeat; display: flex; flex-direction: column; align-items: center; justify-content: center; } .fm-awards-container-body .fm-awards-card .card-coupon-img .card-coupon-value-money{ color: white; position: absolute; } .fm-awards-container-body .fm-awards-card { text-align: center; background: var(--fm-card-primary-background); border-radius: var(--fm-base-border-radius); padding: 20px; flex-basis: 20%; display: flex; flex-direction: column; align-items: center; justify-content: space-between; } .fm-awards-container-body .card-coupon-detail-extra { border: 2px solid var(--fm-card-primary-text-color); color: var(--fm-card-primary-text-color); border-radius: var(--fm-base-border-radius); font-weight: bold; padding: 5px; } @media (max-width: 768px) { .fm-awards-container-body .fm-awards-card { flex-basis: 100%; } } .fm-awards-container-body .fm-awards-card .card-coupon-img img{ max-width:100px; } ` ); }