udemy-go-web-2/static/js/all-sales.js
2024-08-22 14:33:36 +02:00

54 lines
2.0 KiB
JavaScript

import { formatCurrency } from "./common.js"
export function showTable(api) {
let token = localStorage.getItem("token");
let tbody = document.getElementById("sales-table").getElementsByTagName("tbody")[0];
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + "/api/admin/all-sales", requestOptions)
.then(response => response.json())
.then(function (data) {
if (data) {
data.forEach(function (i) {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.innerHTML = `<a href="/admin/sales/${i.id}">Order ${i.id}</a>`;
newCell = newRow.insertCell();
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
newCell.appendChild(item)
newCell = newRow.insertCell();
item = document.createTextNode(i.widget.name);
newCell.appendChild(item)
let cur = formatCurrency(i.transaction.amount)
newCell = newRow.insertCell();
item = document.createTextNode(cur);
newCell.appendChild(item)
newCell = newRow.insertCell();
if (i.status_id != 1) {
newCell.innerHTML = `<span class="badge bg-danger">Refunded</span>`
} else {
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
}
});
} else {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.setAttribute("colspan", "4");
newCell.innerHTML = "No data available";
}
});
}