async loadData(page = 1, container, body, targetDirectory, caller, callback) {
page = parseInt(page, 10);
if (typeof page === 'number' && page > 0) {
this.itemsPerPage = parseInt(this.itemsPerPage, 10);
const loader = container.previousElementSibling;
this.scrollToTarget(container);
container.style.display = "none";
loader.style.display = "";
const controller = new AbortController();
const signal = controller.signal;
const timeoutDuration = 10000;
const timeoutId = setTimeout(() => {
controller.abort();
}, timeoutDuration);
try {
body.append("page", page);
if (typeof this.itemsPerPage === 'number' && this.itemsPerPage > 0) {
body.append("itemsPerPage", this.itemsPerPage);
}
const url = window.location.origin;
const response = await fetch(url + targetDirectory, {
method: "POST",
body,
signal,
});
await this.sleep(500);
clearTimeout(timeoutId);
if (!response.ok) {
displayToastError();
throw new Error(`HTTP error: ${response.status}`);
} else {
const value = await response.text();
if (value) {
container.innerHTML = value;
loader.style.display = "none";
container.style.display = "";
enableTooltipsEverywhere();
this.setupPagination(container, caller, this.setPaginationItemsPerPage.bind(this));
this.setupTableHeader(container, caller, this.sortData.bind(this));
callback();
}
}
} catch (error) {
this.handleError(container, loader, error);
}
}
}
async loadStoreData(page = 1) {
const container = document.getElementById("store-container");
if (!container) {
return;
}
this.sortScope = parseInt(this.sortScope, 10);
const body = new FormData();
if (this.sortCategory) {
body.append("sortCategory", this.sortCategory);
}
if (typeof this.sortScope === 'number' && this.sortScope >= 0) {
body.append("sortScope", this.sortScope);
}
if (this.sortTag) {
body.append("sortTag", this.sortTag);
}
const targetDirectory = "/index.php/Store/loadStore";
this.loadData(page, container, body, targetDirectory, this.loadStoreData.bind(this), () => {});
}
async loadOrganizationsData(page = 1) {
const container = document.getElementById("organizations-container");
if (!container) {
return;
}
const body = new FormData();
if (this.sortField) {
body.append("sortField", this.sortField);
}
if (this.sortOrder) {
body.append("sortOrder", this.sortOrder);
}
const targetDirectory = "/index.php/Organization/loadOrganizations";
this.sortFieldArray = ["name", "type", "nb_members", "project_list", "domain_list", "created"];
this.loadData(page, container, body, targetDirectory, this.loadOrganizationsData.bind(this), () => {
const deleteOrganizations = container.querySelectorAll(".deleteOrganization");
deleteOrganizations.forEach((element) => {
element.addEventListener("click", () => {
deleteOrganization(element);
});
});
});
}
Qu'en pensez vous ?