Merge pull request 'fix: duplicated stores rendering' (#8) from dev into main

Reviewed-on: AnzhelaK/cash_report_system#8
This commit is contained in:
AnzhelaK
2025-07-30 23:58:11 +00:00
2 changed files with 36 additions and 39 deletions

View File

@@ -1,8 +1,8 @@
//API
const API_BASE_URL = "http://195.209.214.159/api";
// const API_BASE_URL = "http://195.209.214.159/api";
//API local
// const API_BASE_URL = "http://localhost:3001/api";
const API_BASE_URL = "http://localhost:3001/api";
//SHARED
@@ -38,16 +38,13 @@ window.appState = createReactiveState(
function (prop, value) {
// React to changes in critical state
if (prop === "storesList") {
// if (typeof loadStores === "function") loadStores();
if (typeof loadUserStores === "function") loadUserStores();
if (typeof updateDashboard === "function") updateDashboard();
}
if (prop === "usersList") {
// if (typeof loadUsers === "function") loadUsers();
if (typeof updateDashboard === "function") updateDashboard();
}
if (prop === "reportsList") {
// if (typeof loadReports === "function") loadReports();
if (typeof updateDashboard === "function") updateDashboard();
}
}

View File

@@ -181,7 +181,9 @@ async function showAdminInterface() {
"adminWelcome"
).textContent = `Добро пожаловать, ${appState.currentUser.username}!`;
await Promise.all([loadUsers(), loadStores(), loadReports()]);
await loadUsers(); // Load users
await loadStores(); // Load stores (first and only time!)
await loadReports(); // Only now load reports (uses storesList)
updateDashboard();
loadTodos();
@@ -363,7 +365,7 @@ function createCharts() {
const revenue = storeReports.reduce((sum, r) => sum + r.totalIncome, 0);
return { name: store.name, revenue };
});
console.log("storeData for bar chart:", storeData);
// console.log("storeData for bar chart:", storeData);
appState.storesChartInstance = new Chart(storesCtx, {
type: "bar",
@@ -466,13 +468,14 @@ function createCharts() {
async function loadReports() {
if (!appState.storesList || appState.storesList.length === 0) {
await loadStores();
return;
}
const tbody = document.getElementById("reportsTableBody");
const filterStore = document.getElementById("filterStore");
const result = await getReports();
console.log("getReports() result:", result);
// console.log("getReports() result:", result);
appState.reportsList = result.success ? result.reports : [];
@@ -560,7 +563,7 @@ function viewReport(reportId) {
}
function editReport(report) {
console.log("editReport() called with:", report);
// console.log("editReport() called with:", report);
const content = document.getElementById("reportViewContent");
const buttons = document.getElementById("reportModalButtons");
@@ -1103,6 +1106,7 @@ function loadUserStores() {
select.innerHTML =
'<option value="" disabled selected hidden>Выберите магазин</option>';
if (!appState.currentUser) return;
// For admin: show all
if (appState.currentUser.role === "admin") {
(appState.storesList || []).forEach((store) => {
@@ -1305,7 +1309,7 @@ document.getElementById("reportForm").addEventListener("submit", async (e) => {
(calculateTotalWages() + calculateTotalExpenses()) -
parseFloat(document.getElementById("envelope").value),
};
console.log("Sending report:", formData);
// console.log("Sending report:", formData);
const result = await createReport(formData);
if (result.success) {
showNotification("Отчет успешно создан!");
@@ -1333,7 +1337,7 @@ document.getElementById("todayReportBtn").addEventListener("click", () => {
//USERS
async function loadUsers() {
console.log("Loading users...");
// console.log("Loading users...");
const tbody = document.getElementById("usersTableBody");
tbody.innerHTML = "";
@@ -1536,33 +1540,31 @@ document.addEventListener("DOMContentLoaded", function () {
});
//SHOPS
// Загрузка магазинов
async function loadStores() {
console.log("loadStores CALLED");
const tbody = document.getElementById("storesTableBody");
tbody.innerHTML = "";
const token = localStorage.getItem("token");
try {
const response = await fetch(`${API_BASE_URL}/stores`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
const data = await response.json();
if (!response.ok || !data.stores) {
showNotification(data.error || "Ошибка загрузки магазинов", "error");
return;
}
const stores = data.stores;
// appState.usersList = users;
appState.storesList = stores;
const result = await getStores();
stores.forEach((store) => {
const row = document.createElement("tr");
row.className = "hover:bg-gray-50";
row.innerHTML = `
if (!result.success) {
showNotification(result.error || "Ошибка загрузки магазинов", "error");
appState.storesList = result.stores;
return;
}
const stores = result.stores;
appState.storesList = stores;
console.log("Fetched stores from API:", result.stores);
console.log("appState.storesList:", appState.storesList);
stores.forEach((store) => {
console.log("Rendering store:", store.name);
const row = document.createElement("tr");
row.className = "hover:bg-gray-50";
row.innerHTML = `
<td class="px-6 py-4 text-sm text-gray-900">${store.id}</td>
<td class="px-6 py-4 text-sm text-gray-900">${store.name}</td>
<td class="px-6 py-4 text-sm text-gray-900">${
@@ -1581,14 +1583,11 @@ async function loadStores() {
</button>
</td>
`;
tbody.appendChild(row);
});
tbody.appendChild(row);
});
if (stores.length === 0) {
showNotification("Нет магазинов для отображения", "info");
}
} catch (err) {
showNotification("Нет соединения с сервером", "error");
if (stores.length === 0) {
showNotification("Нет магазинов для отображения", "info");
}
}
@@ -1633,6 +1632,7 @@ async function saveStore() {
showNotification("Магазин добавлен!");
hideModal("storeEditModal");
await loadStores();
await loadReports();
if (typeof loadUsers === "function") loadUsers();
if (typeof loadUserStores === "function") loadUserStores();
if (typeof updateDashboard === "function") updateDashboard();