Files
MH-DashBoard-organization/frontend/public/app.js
2026-03-25 12:57:15 +09:00

113 lines
3.5 KiB
JavaScript
Executable File

const sessionKey = "mh-dashboard-session";
const loginPanel = document.getElementById("login-panel");
const dashboardPanel = document.getElementById("dashboard-panel");
const loginForm = document.getElementById("login-form");
const loginMessage = document.getElementById("login-message");
const logoutBtn = document.getElementById("logout-btn");
const userBadge = document.getElementById("user-badge");
const currentViewTitle = document.getElementById("current-view-title");
const navButtons = Array.from(document.querySelectorAll(".header-center [data-view]"));
const organizationFrame = document.getElementById("organization-frame");
const organizationStage = document.getElementById("organization-stage");
const emptyStage = document.getElementById("empty-stage");
const viewLabels = {
ledger: "사업관리대장",
project: "프로젝트별 분석",
team: "팀/개인별 분석",
organization: "조직 현황",
};
let currentView = "organization";
function getSession() {
try {
return JSON.parse(sessionStorage.getItem(sessionKey) || "null");
} catch {
return null;
}
}
function setSession(session) {
sessionStorage.setItem(sessionKey, JSON.stringify(session));
}
function clearSession() {
sessionStorage.removeItem(sessionKey);
}
function setActiveView(view) {
const previousView = currentView;
currentView = view in viewLabels ? view : "organization";
if (currentViewTitle) {
currentViewTitle.textContent = viewLabels[currentView];
}
navButtons.forEach((button) => {
const active = button.dataset.view === currentView;
button.classList.toggle("active", active);
button.classList.toggle("muted", !active);
});
const isOrganization = currentView === "organization";
if (organizationStage) {
organizationStage.hidden = !isOrganization;
}
if (emptyStage) {
emptyStage.hidden = isOrganization;
}
if (isOrganization && previousView !== "organization" && organizationFrame) {
const frameSrc = organizationFrame.dataset.src || organizationFrame.src;
organizationFrame.src = frameSrc;
}
}
function renderAuth() {
const session = getSession();
const authenticated = Boolean(session?.user?.display_name);
loginPanel.classList.toggle("hidden", authenticated);
dashboardPanel.classList.toggle("hidden", !authenticated);
if (authenticated) {
const displayName = session.user.display_name || "접속자";
userBadge.innerHTML = `<span class="user-chip-icon">◎</span><span class="user-chip-text"><strong>${displayName}</strong><em>-</em></span>`;
userBadge.title = `${displayName} / -`;
}
}
if (loginForm) {
loginForm.addEventListener("submit", async (event) => {
event.preventDefault();
loginMessage.textContent = "로그인 처리 중입니다.";
const formData = new FormData(loginForm);
try {
const response = await fetch("/api/mock-login", {
method: "POST",
body: formData,
});
const payload = await response.json();
if (!response.ok) throw new Error(payload.detail || "login failed");
setSession(payload);
loginForm.reset();
renderAuth();
} catch (error) {
loginMessage.textContent = "로그인에 실패했습니다. backend 연결 상태를 확인해주세요.";
}
});
}
if (logoutBtn) {
logoutBtn.addEventListener("click", () => {
clearSession();
renderAuth();
});
}
navButtons.forEach((button) => {
button.addEventListener("click", () => {
setActiveView(button.dataset.view || "organization");
});
});
setActiveView(currentView);
renderAuth();