| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { fetchUserSales, historyProducts } from "../service/user.js";
- const table = document.getElementById('historyTable');
- const rowTemplate = document.getElementById('historyRowTemplate');
- const cartHistoryTotal = document.getElementById('cartHistoryTotal');
- function setHistoryTable(tableData) {
- if (!tableData || tableData.length === 0) {
- console.warn("No hay datos para mostrar en el historial de compras.");
- table.querySelector('tbody').innerHTML = '<tr class="no-data"><td colspan="3" class="text-center text-gray-500">No hay historial de compras.</td></tr>';
- return;
- }
- table.querySelector('tbody').innerHTML = ''; // Clear existing rows
- tableData.forEach(item => {
- addHistoryRow(item);
- });
- }
- function addHistoryRow(sale) {
- const newRow = rowTemplate.content.cloneNode(true);
- if (table.querySelector(".no-data")) {
- table.querySelector(".no-data").remove(); // Remove no-data row if it exists
- }
- newRow.querySelector('.list-element-quantity').textContent = sale.quantity;
- newRow.querySelector('.list-element-name').textContent = sale.productName;
- newRow.querySelector('.list-element-price').textContent = `$${sale.price * sale.quantity}`;
- addCartHistoryTotal(sale.price * sale.quantity);
- table.querySelector('tbody').appendChild(newRow);
- }
- function addCartHistoryTotal(price) {
- const currentTotal = getHistoryTotal();
- const newTotal = currentTotal + Number(price);
- updateCartHistoryTotal(newTotal);
- }
- function updateCartHistoryTotal(total) {
- cartHistoryTotal.textContent = `$${total.toFixed(0)}`;
- }
- function getHistoryTotal() {
- return Number(cartHistoryTotal.textContent.replace(/[$,]/g, ''));
- }
- async function setupShoppingCart(userID, userToken, userName) {
- const name = document.querySelector('#usernameTable');
- if (name) {
- name.textContent = `Pedidos de: ${userName}`;
- }else{
- console.warn("No se encontró el elemento de nombre en la tabla de historial.");
- }
- const sales = await historyProducts(userID, userToken);
-
- if (sales){
- setHistoryTable(sales);
- }
- }
- export { setupShoppingCart, setHistoryTable, addHistoryRow, updateCartHistoryTotal, getHistoryTotal };
|