/** * Learning Kanban Native Integration for Homepage (v4 - Autonomous & Anchorless) */ function getApiBase() { return `https://learning.arch-services.mywire.org`; } const API_BASE = getApiBase(); let allCourses = []; async function fetchCourses() { try { const response = await fetch(`${API_BASE}/api/courses`); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); allCourses = await response.json(); return allCourses; } catch (e) { console.warn("Failed to fetch courses", e); return []; } } async function updateCourseStatus(courseId, newStatus) { try { const course = allCourses.find(c => c.id == courseId); if (!course) return; course.status = newStatus; await fetch(`${API_BASE}/api/courses/${courseId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(course) }); } catch (e) { console.error("Failed to update course", e); } } function createKanbanCard(course) { return `
${course.title}
${course.description || ''}
`; } async function renderKanban() { // Look for the main services grid - this is our insertion point const servicesGrid = document.getElementById('services'); if (!servicesGrid) return; // Check if we already created our section let section = document.getElementById('hp-learning-hub-section'); if (!section) { section = document.createElement('section'); section.id = 'hp-learning-hub-section'; section.className = 'hp-kanban-section'; // Insert after services grid servicesGrid.after(section); section.innerHTML = `

Learning Hub

🚀 In Progress

📅 Planning

📦 Archive

`; injectModal(); } await refreshBoardContent(section); } async function refreshBoardContent(section) { const courses = await fetchCourses(); const board = section.querySelector('.hp-kanban-board'); if (!board) return; board.querySelectorAll('.hp-kanban-list').forEach(l => l.innerHTML = ''); courses.forEach(course => { const list = board.querySelector(`.hp-kanban-col[data-status="${course.status}"] .hp-kanban-list`); if (list) list.innerHTML += createKanbanCard(course); }); // Re-bind Drag & Drop const cards = board.querySelectorAll('.hp-kanban-card'); cards.forEach(card => { card.addEventListener('dragstart', (e) => { e.dataTransfer.setData('text/plain', card.dataset.id); card.classList.add('dragging'); }); card.addEventListener('dragend', () => card.classList.remove('dragging')); }); const cols = board.querySelectorAll('.hp-kanban-col'); cols.forEach(col => { col.addEventListener('dragover', (e) => e.preventDefault()); col.addEventListener('drop', async (e) => { e.preventDefault(); const id = e.dataTransfer.getData('text/plain'); const status = col.dataset.status; const draggingCard = document.querySelector(`.hp-kanban-card[data-id="${id}"]`); if (draggingCard) col.querySelector('.hp-kanban-list').appendChild(draggingCard); await updateCourseStatus(id, status); }); }); } // Modal Logic window.hpShowCourseModal = function(id = null) { const modal = document.getElementById('hp-course-modal'); if (!modal) return; const form = document.getElementById('hp-course-form'); modal.style.display = 'flex'; if (id) { const course = allCourses.find(c => c.id == id); if (!course) return; document.getElementById('hp-modal-title').innerText = 'Edit Course'; document.getElementById('hp-course-id').value = course.id; document.getElementById('hp-title').value = course.title; document.getElementById('hp-description').value = course.description || ''; document.getElementById('hp-main_link').value = course.main_link; document.getElementById('hp-last_link').value = course.last_link || ''; document.getElementById('hp-status').value = course.status; } else { document.getElementById('hp-modal-title').innerText = 'Add New Course'; form.reset(); document.getElementById('hp-course-id').value = ''; } }; window.hpCloseModal = () => { const modal = document.getElementById('hp-course-modal'); if (modal) modal.style.display = 'none'; }; window.hpDeleteCourse = async (id) => { if (confirm('Delete this course?')) { await fetch(`${API_BASE}/api/courses/${id}`, { method: 'DELETE' }); const section = document.getElementById('hp-learning-hub-section'); if (section) refreshBoardContent(section); } }; function injectModal() { if (document.getElementById('hp-course-modal')) return; const modalHtml = `

Add Course

`; document.body.insertAdjacentHTML('beforeend', modalHtml); document.getElementById('hp-course-form').onsubmit = async (e) => { e.preventDefault(); const id = document.getElementById('hp-course-id').value; const data = { title: document.getElementById('hp-title').value, description: document.getElementById('hp-description').value, main_link: document.getElementById('hp-main_link').value, last_link: document.getElementById('hp-last_link').value, status: document.getElementById('hp-status').value }; const method = id ? 'PUT' : 'POST'; const url = id ? `${API_BASE}/api/courses/${id}` : `${API_BASE}/api/courses`; await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); window.hpCloseModal(); const section = document.getElementById('hp-learning-hub-section'); if (section) refreshBoardContent(section); }; } // Observer let debounceTimer; const observer = new MutationObserver(() => { clearTimeout(debounceTimer); debounceTimer = setTimeout(() => { const servicesGrid = document.getElementById('services'); if (servicesGrid && !document.getElementById('hp-learning-hub-section')) { renderKanban(); } }, 500); }); observer.observe(document.body, { childList: true, subtree: true }); renderKanban();