Initiated the process of refactoring the monolithic `work-report-calendar.js` file as outlined in the Phase 2 frontend modernization plan. - Created `CalendarAPI.js` to encapsulate all API calls related to the calendar, centralizing data fetching logic. - Created `CalendarState.js` to manage the component's state, removing global variables from the main script. - Refactored `work-report-calendar.js` to use the new state and API modules. - Refactored `manage-project.js` to use the existing global API helpers, providing a consistent example for API usage.
90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
// /js/manage-project.js
|
|
|
|
// The ensureAuthenticated, API, and getAuthHeaders functions are now handled by the global api-helper.js
|
|
|
|
function createRow(item, cols, delHandler) {
|
|
const tr = document.createElement('tr');
|
|
cols.forEach(key => {
|
|
const td = document.createElement('td');
|
|
td.textContent = item[key];
|
|
tr.appendChild(td);
|
|
});
|
|
const delBtn = document.createElement('button');
|
|
delBtn.textContent = '삭제';
|
|
delBtn.className = 'btn-delete';
|
|
delBtn.onclick = () => delHandler(item);
|
|
const td = document.createElement('td');
|
|
td.appendChild(delBtn);
|
|
tr.appendChild(td);
|
|
return tr;
|
|
}
|
|
|
|
const projectForm = document.getElementById('projectForm');
|
|
projectForm?.addEventListener('submit', async e => {
|
|
e.preventDefault();
|
|
const body = {
|
|
job_no: document.getElementById('job_no').value.trim(),
|
|
project_name: document.getElementById('project_name').value.trim(),
|
|
contract_date: document.getElementById('contract_date').value,
|
|
due_date: document.getElementById('due_date').value,
|
|
delivery_method: document.getElementById('delivery_method').value.trim(),
|
|
site: document.getElementById('site').value.trim(),
|
|
pm: document.getElementById('pm').value.trim()
|
|
};
|
|
|
|
if (!body.project_name || !body.job_no) {
|
|
return alert('필수 항목을 입력하세요.');
|
|
}
|
|
|
|
try {
|
|
const result = await apiPost('/projects', body);
|
|
if (result.success) {
|
|
alert('✅ 등록 완료');
|
|
projectForm.reset();
|
|
loadProjects();
|
|
} else {
|
|
alert('❌ 실패: ' + (result.error || '알 수 없는 오류'));
|
|
}
|
|
} catch (err) {
|
|
alert('🚨 서버 오류: ' + err.message);
|
|
}
|
|
});
|
|
|
|
async function loadProjects() {
|
|
const tbody = document.getElementById('projectTableBody');
|
|
tbody.innerHTML = '<tr><td colspan="9">불러오는 중...</td></tr>';
|
|
try {
|
|
const result = await apiGet('/projects');
|
|
|
|
tbody.innerHTML = '';
|
|
|
|
if (result.success && Array.isArray(result.data)) {
|
|
result.data.forEach(item => {
|
|
const row = createRow(item, [
|
|
'project_id', 'job_no', 'project_name', 'contract_date',
|
|
'due_date', 'delivery_method', 'site', 'pm'
|
|
], async p => {
|
|
if (!confirm('삭제하시겠습니까?')) return;
|
|
try {
|
|
const delRes = await apiDelete(`/projects/${p.project_id}`);
|
|
if (delRes.success) {
|
|
alert('✅ 삭제 완료');
|
|
loadProjects();
|
|
} else {
|
|
alert('❌ 삭제 실패: ' + (delRes.error || '알 수 없는 오류'));
|
|
}
|
|
} catch (err) {
|
|
alert('🚨 삭제 중 오류: ' + err.message);
|
|
}
|
|
});
|
|
tbody.appendChild(row);
|
|
});
|
|
} else {
|
|
tbody.innerHTML = '<tr><td colspan="9">데이터 형식 오류</td></tr>';
|
|
}
|
|
} catch (err) {
|
|
tbody.innerHTML = '<tr><td colspan="9">로드 실패: ' + err.message + '</td></tr>';
|
|
}
|
|
}
|
|
|
|
window.addEventListener('DOMContentLoaded', loadProjects); |