udemy-go-web-2/static/js/all-subscriptions.js

74 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-08-23 08:51:20 +00:00
import { formatCurrency, paginator } from "./common.js"
2024-08-22 08:01:54 +00:00
2024-08-23 08:13:50 +00:00
export let currentPage = 1;
export function showTable(api, ps, cp) {
2024-08-21 21:27:33 +00:00
let token = localStorage.getItem("token");
let tbody = document.getElementById("subscriptions-table").getElementsByTagName("tbody")[0];
2024-08-23 08:13:50 +00:00
// reset tbody
tbody.innerHTML = ``
const body = {
page_size: parseInt(ps, 10),
page: parseInt(cp, 10),
};
2024-08-21 21:27:33 +00:00
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
2024-08-23 08:13:50 +00:00
body: JSON.stringify(body),
2024-08-21 21:27:33 +00:00
};
fetch(api + "/api/admin/all-subscriptions", requestOptions)
.then(response => response.json())
.then(function (data) {
2024-08-23 08:13:50 +00:00
if (data.orders) {
data.orders.forEach(function (i) {
2024-08-22 19:34:58 +00:00
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
2024-08-23 08:51:20 +00:00
newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`;
newCell.addEventListener("click", function (evt) {
// put the current_page into sessionStorage
sessionStorage.setItem("cur-page", data.current_page);
2024-08-23 08:51:20 +00:00
// redirect
location.href = `/admin/subscriptions/${i.id}`;
});
2024-08-22 19:34:58 +00:00
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 + "/month");
newCell.appendChild(item)
newCell = newRow.insertCell();
if (i.status_id != 1) {
newCell.innerHTML = `<span class="badge bg-danger">Cancelled</span>`
} else {
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
}
2024-08-23 08:51:20 +00:00
paginator(api, data.last_page, data.current_page, showTable)
2024-08-22 19:34:58 +00:00
});
2024-08-21 21:27:33 +00:00
} else {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.setAttribute("colspan", "4");
newCell.innerHTML = "No data available";
}
});
}