{"version":3,"file":"loginLojistas.js","mappings":";;;;;;AAAO;AACP;AACA;AACA;AACA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA;AACA,MAAM;AACN;AACA;AACA;AACA,IAAI,gBAAgB;AACpB;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,2BAA2B,EAAE;AAC7B;AACA;;;AC9BwC;AACxC;AACO;AACP;AACA,YAAY;AACZ;AACA;AACA;AACA;AACA;AACA;AACA;AACA,yBAAyB,SAAS;AAClC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAAG;AACH;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAQ;AACR;AACA;AACA,MAAM;AACN;AACA;AACA,GAAG;AACH;AACA;AACA;AACA,qCAAqC,GAAG;AACxC;AACA;AACA,2CAA2C,GAAG;AAC9C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO;AACP,KAAK;AACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA,GAAG;AACH;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,cAAc,oBAAoB;AAClC;AACA;AACA;AACA;AACA,4BAA4B,yBAAyB;AACrD;AACA;AACA,+BAA+B,8BAA8B;AAC7D;AACA;AACA,QAAQ;AACR;AACA,2BAA2B,yBAAyB;AACpD;AACA;AACA;AACA;AACA,KAAK;AACL,GAAG;AACH;AACA;AACA;AACA,GAAG;AACH;AACA;AACA;AACA;AACA,CAAC","sources":["webpack://build/../laviesports/assets/javascripts/utilities.js","webpack://build/../laviesports/assets/javascripts/loginLojistas.js"],"sourcesContent":["export async function getClient() {\r\n try {\r\n let response = await fetch(`/conta/cliente`, {\r\n headers: {\r\n accept: 'application/json'\r\n }\r\n });\r\n let client = await response.json();\r\n\r\n if (Object.keys(client).length > 0) {\r\n return client\r\n } else {\r\n return {}\r\n }\r\n\r\n } catch (error) { console.error(`getClient error`, error) }\r\n}\r\n\r\n// ===============================================================\r\n// FORMAT MONEY\r\n// ===============================================================\r\nexport function formatMoney(value) {\r\n // FORMATA UM VALOR\r\n return (\r\n 'R$ ' +\r\n value\r\n .toFixed(2)\r\n .replace('.', ',')\r\n .replace(/(\\d)(?=(\\d{3})+\\,)/g, '$1.')\r\n );\r\n}\r\n","import { getClient } from \"./utilities\";\r\n\r\nexport const LoginLojistas = {\r\n isLoggedIn: false,\r\n client: {},\r\n initialProducts: [...document.querySelectorAll('.product-block-lojista')],\r\n sectionWaitingAccess: document.querySelector('[data-section=\"waiting-access\"]'),\r\n sectionLogin: document.querySelector('[data-section=\"login\"]'),\r\n sectionProducts: document.querySelector('[data-section=\"products\"]'),\r\n\r\n // Seta as informações do cliente\r\n setClient: async function () {\r\n const client = await getClient();\r\n\r\n if (Object.keys(client).length > 0) {\r\n this.isLoggedIn = true;\r\n this.client = client;\r\n }\r\n\r\n this.showSectionByClientStatus();\r\n },\r\n\r\n // Controla qual seção exibir baseado no status do cliente\r\n showSectionByClientStatus: function () {\r\n if (this.isLoggedIn) {\r\n if (this.client.tags && this.client.tags.includes('lojistas')) {\r\n this.sectionProducts.classList.add('-visible');\r\n this.updateProductsPrice(this.initialProducts);\r\n } else {\r\n this.sectionWaitingAccess.classList.add('-visible');\r\n }\r\n } else {\r\n this.sectionLogin.classList.add('-visible');\r\n }\r\n },\r\n\r\n // Busca o preço baseado nas informações do cliente\r\n fetchPrice: async function (id, sku) {\r\n let priceURL = `/produto/preco/${id}`;\r\n\r\n if (this.isLoggedIn) {\r\n priceURL = `/produto/preco/cliente/${id}`;\r\n }\r\n\r\n const priceConfig = {}\r\n const response = await fetch(priceURL, {\r\n method: \"GET\",\r\n credentials: \"include\",\r\n headers: {\r\n Accept: \"application/json\",\r\n },\r\n });\r\n\r\n const data = await response.json();\r\n data.variants.forEach((variant) => {\r\n if (sku == variant.sku) {\r\n priceConfig.price = variant.price;\r\n priceConfig.sale_price = variant.sale_price;\r\n }\r\n })\r\n priceConfig.available = data.available;\r\n\r\n return priceConfig;\r\n },\r\n\r\n // Atualiza os produtos com o preço original e o preço remarcado baseado no cliente\r\n updateProductsPrice: function (products) {\r\n products.forEach(async function (product) {\r\n const id = product.getAttribute('data-product-id');\r\n const sku = product.getAttribute('data-product-sku');\r\n const priceWrapper = product.querySelector('[data-seller-price]');\r\n\r\n const { price, sale_price } = await LoginLojistas.fetchPrice(id, sku);\r\n\r\n if (price != sale_price) {\r\n priceWrapper.innerHTML = `\r\n \r\n Varejo: ${store.formatMoney(price)}\r\n \r\n \r\n Atacado: ${store.formatMoney(sale_price)}\r\n \r\n `;\r\n } else {\r\n priceWrapper.innerHTML = `\r\n Atacado: ${store.formatMoney(price)}\r\n `;\r\n }\r\n\r\n product.setAttribute('data-was-processed', true);\r\n });\r\n },\r\n\r\n init() {\r\n this.setClient();\r\n },\r\n};\r\n\r\nwindow.addEventListener('DOMContentLoaded', () => {\r\n LoginLojistas.init();\r\n});\r\n"],"names":[],"sourceRoot":""}