Update index.html

Ryan Danenberg committed commitef49490e38673fc934f7f7098b0372a4b4ed9b5e Browse files
1 file changed+224−34
index.html+224−34View file
@@ -3373,7 +3373,7 @@
}
}
- function validateCurrentMonth() {
+ async function validateCurrentMonth() {
if (!inspectTimesheetId) return;
const ts = state.timesheets.find(t => t._id === inspectTimesheetId);
if (!ts) return;
@@ -3381,7 +3381,22 @@
ts.status = 'validated';
ts.adminFeedback = ''; // clear feedback on approval
saveStore();
- syncPostKallax('timesheets', ts);
+
+ if (!isTempId(ts._id)) {
+ try {
+ const res = await apiFetch(`/api/timesheets/${ts._id}`, {
+ method: 'PATCH',
+ body: JSON.stringify({ status: 'validated', adminFeedback: '' })
+ });
+ if (res.ok) {
+ const serverTs = await res.json();
+ Object.assign(ts, serverTs);
+ saveStore();
+ }
+ } catch (err) {
+ console.warn('PATCH /api/timesheets/:id failed, updated locally:', err);
+ }
+ }
showToast('Timesheet Approved', `${ts.freelanceName}'s timesheet for ${ts.month} has been validated!`, 'success');
closeInspectModal();
@@ -3398,7 +3413,7 @@
document.getElementById('modal-refuse-prompt').classList.add('hidden');
}
- function confirmRefuseTimesheet() {
+ async function confirmRefuseTimesheet() {
const feedback = document.getElementById('refusal-feedback-input').value.trim();
if (!feedback) {
alert("Please provide refusal feedback notes so the freelancer knows what to adjust.");
@@ -3411,7 +3426,22 @@
ts.status = 'refused';
ts.adminFeedback = feedback;
saveStore();
- syncPostKallax('timesheets', ts);
+
+ if (!isTempId(ts._id)) {
+ try {
+ const res = await apiFetch(`/api/timesheets/${ts._id}`, {
+ method: 'PATCH',
+ body: JSON.stringify({ status: 'refused', adminFeedback: feedback })
+ });
+ if (res.ok) {
+ const serverTs = await res.json();
+ Object.assign(ts, serverTs);
+ saveStore();
+ }
+ } catch (err) {
+ console.warn('PATCH /api/timesheets/:id failed, updated locally:', err);
+ }
+ }
showToast('Timesheet Refused', `Marked as refused with feedback. Freelancer can now edit & resubmit.`, 'error');
closeRefusePrompt();
@@ -3440,17 +3470,29 @@
<div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 border-b border-slate-100 pb-4">
<div>
<div class="flex items-center gap-2">
- <h3 class="text-lg font-bold text-slate-900">${p.name}</h3>
- <span class="text-[10px] uppercase font-bold px-2 py-0.5 rounded-full bg-emerald-50 text-emerald-700">${p.status || 'active'}</span>
+ <h3 class="text-lg font-bold text-slate-900">${escapeHtml(p.name)}</h3>
+ <span class="text-[10px] uppercase font-bold px-2 py-0.5 rounded-full ${p.status === 'completed' ? 'bg-blue-50 text-blue-700' : (p.status === 'archived' ? 'bg-slate-100 text-slate-600' : 'bg-emerald-50 text-emerald-700')}">${escapeHtml(p.status || 'active')}</span>
</div>
- <p class="text-xs text-slate-500 mt-0.5">${p.description || 'No description provided'}</p>
- <div class="text-xs text-slate-400 mt-1">Admin: <span class="font-medium text-slate-700">${p.adminName || 'Sarah Jenkins'}</span> &lt;${p.adminEmail || 'admin@example.com'}&gt;</div>
+ <p class="text-xs text-slate-500 mt-0.5">${escapeHtml(p.description || 'No description provided')}</p>
+ <div class="text-xs text-slate-400 mt-1">Admin: <span class="font-medium text-slate-700">${escapeHtml(p.adminName || 'Sarah Jenkins')}</span> &lt;${escapeHtml(p.adminEmail || 'admin@example.com')}&gt;</div>
+ </div>
+ <div class="flex items-center gap-2 flex-wrap self-start sm:self-auto">
+ <button onclick="openEditProjectModal('${p._id}')"
+ class="px-3 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 rounded-xl text-xs font-bold transition flex items-center gap-1.5 shadow-sm">
+ <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
+ Edit
+ </button>
+ <button onclick="deleteProject('${p._id}')"
+ class="px-3 py-2 bg-rose-50 hover:bg-rose-100 text-rose-600 border border-rose-200 rounded-xl text-xs font-bold transition flex items-center gap-1.5 shadow-sm">
+ <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
+ Delete
+ </button>
+ <button onclick="openAddAssignmentModal('${p._id}', '${escapeHtml(p.name)}')"
+ class="px-3.5 py-2 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 rounded-xl text-xs font-bold transition flex items-center gap-1.5 self-start sm:self-auto shadow-sm">
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
+ Assign Freelancer
+ </button>
</div>
- <button onclick="openAddAssignmentModal('${p._id}', '${escapeHtml(p.name)}')"
- class="px-3.5 py-2 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 rounded-xl text-xs font-bold transition flex items-center gap-1.5 self-start sm:self-auto">
- <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
- Assign Freelancer
- </button>
</div>
<!-- Freelancers Table -->
@@ -3474,19 +3516,19 @@
${assigned.map(a => `
<tr class="hover:bg-slate-50">
<td class="py-2.5 px-4 font-semibold text-slate-800">
- <div>${a.freelanceName}</div>
- <div class="text-[11px] font-normal text-slate-400">${a.freelanceEmail}</div>
+ <div>${escapeHtml(a.freelanceName)}</div>
+ <div class="text-[11px] font-normal text-slate-400">${escapeHtml(a.freelanceEmail)}</div>
</td>
- <td class="py-2.5 px-4 text-slate-700">${a.startDate}</td>
- <td class="py-2.5 px-4 text-slate-700">${a.endDate}</td>
+ <td class="py-2.5 px-4 text-slate-700">${escapeHtml(a.startDate)}</td>
+ <td class="py-2.5 px-4 text-slate-700">${escapeHtml(a.endDate)}</td>
<td class="py-2.5 px-4 font-bold text-emerald-700">€${a.dailyRate || 0}</td>
<td class="py-2.5 px-4 text-right space-x-2">
<button onclick="openEditAssignmentModal('${a._id}')"
- class="px-2.5 py-1 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 rounded-lg text-xs font-medium transition">
+ class="px-2.5 py-1 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 rounded-lg text-xs font-medium transition shadow-sm">
Edit Dates
</button>
<button onclick="removeAssignment('${a._id}')"
- class="px-2.5 py-1 bg-white hover:bg-rose-50 border border-rose-200 text-rose-600 rounded-lg text-xs font-medium transition">
+ class="px-2.5 py-1 bg-white hover:bg-rose-50 border border-rose-200 text-rose-600 rounded-lg text-xs font-medium transition shadow-sm">
Remove
</button>
</td>
@@ -3518,7 +3560,7 @@
document.getElementById('modal-add-assignment').classList.add('hidden');
}
- function handleSaveAssignment(e) {
+ async function handleSaveAssignment(e) {
e.preventDefault();
const projectId = document.getElementById('assign-project-id').value;
const project = state.projects.find(p => p._id === projectId);
@@ -3530,8 +3572,7 @@
if (!name || !email || !startDate || !endDate) return;
- const newAssignment = {
- _id: 'assign_' + Math.random().toString(36).substring(2, 10),
+ const payload = {
projectId: projectId,
projectName: project ? project.name : 'Project',
freelanceName: name,
@@ -3541,14 +3582,32 @@
dailyRate: dailyRate
};
+ let newAssignment = null;
+ try {
+ const res = await apiFetch('/api/assignments', {
+ method: 'POST',
+ body: JSON.stringify(payload)
+ });
+ if (res.ok) {
+ newAssignment = await res.json();
+ }
+ } catch (err) {
+ console.warn('POST /api/assignments failed, fallback to local store:', err);
+ }
+
+ if (!newAssignment || !newAssignment._id) {
+ newAssignment = {
+ _id: 'assign_' + Math.random().toString(36).substring(2, 10),
+ ...payload
+ };
+ }
+
state.assignments.push(newAssignment);
- // Also register into personas list if not already there
if (!FREELANCER_PERSONAS.some(p => p.email === email)) {
FREELANCER_PERSONAS.push({ name, email });
}
saveStore();
- syncPostKallax('assignments', newAssignment);
closeAddAssignmentModal();
renderAll();
showToast('Assignment Created', `${name} assigned to ${project ? project.name : 'project'}.`, 'success');
@@ -3572,24 +3631,42 @@
document.getElementById('modal-edit-assignment').classList.add('hidden');
}
- function handleUpdateAssignmentDates(e) {
+ async function handleUpdateAssignmentDates(e) {
e.preventDefault();
const aId = document.getElementById('edit-assignment-id').value;
const a = state.assignments.find(x => x._id === aId);
if (!a) return;
- a.startDate = document.getElementById('edit-assign-start-date').value;
- a.endDate = document.getElementById('edit-assign-end-date').value;
- a.dailyRate = parseInt(document.getElementById('edit-assign-daily-rate').value, 10) || 0;
+ const updateData = {
+ startDate: document.getElementById('edit-assign-start-date').value,
+ endDate: document.getElementById('edit-assign-end-date').value,
+ dailyRate: parseInt(document.getElementById('edit-assign-daily-rate').value, 10) || 0
+ };
+ Object.assign(a, updateData);
saveStore();
- syncPostKallax('assignments', a);
closeEditAssignmentModal();
renderAll();
+
+ if (!isTempId(aId)) {
+ try {
+ const res = await apiFetch(`/api/assignments/${aId}`, {
+ method: 'PATCH',
+ body: JSON.stringify(updateData)
+ });
+ if (res.ok) {
+ const serverData = await res.json();
+ Object.assign(a, serverData);
+ saveStore();
+ }
+ } catch (err) {
+ console.warn('PATCH /api/assignments/:id failed, maintained locally:', err);
+ }
+ }
showToast('Dates Updated', `Contract dates adjusted for ${a.freelanceName}.`, 'success');
}
- function removeAssignment(assignmentId) {
+ async function removeAssignment(assignmentId) {
const a = state.assignments.find(x => x._id === assignmentId);
if (!a) return;
if (!confirm(`Are you sure you want to remove ${a.freelanceName} from ${a.projectName}?`)) return;
@@ -3597,6 +3674,16 @@
state.assignments = state.assignments.filter(x => x._id !== assignmentId);
saveStore();
renderAll();
+
+ if (!isTempId(assignmentId)) {
+ try {
+ await apiFetch(`/api/assignments/${assignmentId}`, {
+ method: 'DELETE'
+ });
+ } catch (err) {
+ console.warn('DELETE /api/assignments/:id failed, removed locally:', err);
+ }
+ }
showToast('Assignment Removed', `${a.freelanceName} has been unassigned.`, 'info');
}
@@ -3613,7 +3700,7 @@
document.getElementById('modal-create-project').classList.add('hidden');
}
- function handleCreateProject(e) {
+ async function handleCreateProject(e) {
e.preventDefault();
const name = document.getElementById('new-project-name').value.trim();
const desc = document.getElementById('new-project-desc').value.trim();
@@ -3622,8 +3709,7 @@
if (!name) return;
- const newProj = {
- _id: 'proj_' + Math.random().toString(36).substring(2, 10),
+ const payload = {
name: name,
description: desc,
adminName: adminName,
@@ -3631,14 +3717,118 @@
status: 'active'
};
+ let newProj = null;
+ try {
+ const res = await apiFetch('/api/projects', {
+ method: 'POST',
+ body: JSON.stringify(payload)
+ });
+ if (res.ok) {
+ newProj = await res.json();
+ }
+ } catch (err) {
+ console.warn('POST /api/projects failed, fallback to local store:', err);
+ }
+
+ if (!newProj || !newProj._id) {
+ newProj = {
+ _id: 'proj_' + Math.random().toString(36).substring(2, 10),
+ ...payload
+ };
+ }
+
state.projects.push(newProj);
saveStore();
- syncPostKallax('projects', newProj);
closeNewProjectModal();
renderAll();
showToast('Project Created', `Project "${name}" is now ready for assignments.`, 'success');
}
+ // Modal: Edit Project
+ function openEditProjectModal(projectId) {
+ const p = state.projects.find(x => x._id === projectId);
+ if (!p) return;
+ document.getElementById('edit-project-id').value = p._id;
+ document.getElementById('edit-project-name').value = p.name || '';
+ document.getElementById('edit-project-desc').value = p.description || '';
+ document.getElementById('edit-project-status').value = p.status || 'active';
+ document.getElementById('edit-project-admin-name').value = p.adminName || '';
+ document.getElementById('edit-project-admin-email').value = p.adminEmail || '';
+ document.getElementById('modal-edit-project').classList.remove('hidden');
+ }
+
+ function closeEditProjectModal() {
+ document.getElementById('modal-edit-project').classList.add('hidden');
+ }
+
+ async function handleUpdateProject(e) {
+ e.preventDefault();
+ const pId = document.getElementById('edit-project-id').value;
+ const p = state.projects.find(x => x._id === pId);
+ if (!p) return;
+
+ const updatedData = {
+ name: document.getElementById('edit-project-name').value.trim(),
+ description: document.getElementById('edit-project-desc').value.trim(),
+ status: document.getElementById('edit-project-status').value,
+ adminName: document.getElementById('edit-project-admin-name').value.trim(),
+ adminEmail: document.getElementById('edit-project-admin-email').value.trim()
+ };
+
+ if (!updatedData.name) return;
+
+ Object.assign(p, updatedData);
+
+ state.assignments.forEach(a => {
+ if (a.projectId === pId) a.projectName = updatedData.name;
+ });
+ state.timesheets.forEach(t => {
+ if (t.projectId === pId) t.projectName = updatedData.name;
+ });
+
+ saveStore();
+ closeEditProjectModal();
+ renderAll();
+
+ if (!isTempId(pId)) {
+ try {
+ const res = await apiFetch(`/api/projects/${pId}`, {
+ method: 'PATCH',
+ body: JSON.stringify(cleanPayload(updatedData))
+ });
+ if (res.ok) {
+ const serverData = await res.json();
+ Object.assign(p, serverData);
+ saveStore();
+ }
+ } catch (err) {
+ console.warn('PATCH /api/projects/:id failed, maintained locally:', err);
+ }
+ }
+ showToast('Project Updated', `Project "${updatedData.name}" has been updated.`, 'success');
+ }
+
+ async function deleteProject(projectId) {
+ const p = state.projects.find(x => x._id === projectId);
+ if (!p) return;
+ if (!confirm(`Are you sure you want to delete project "${p.name}"? This action cannot be undone.`)) return;
+
+ state.projects = state.projects.filter(x => x._id !== projectId);
+ saveStore();
+ renderAll();
+
+ if (!isTempId(projectId)) {
+ try {
+ await apiFetch(`/api/projects/${projectId}`, {
+ method: 'DELETE'
+ });
+ } catch (err) {
+ console.warn('DELETE /api/projects/:id failed, removed locally:', err);
+ }
+ }
+ showToast('Project Deleted', `Project "${p.name}" was removed.`, 'info');
+ }
+
// -------------------------------------------------------------
// Helper Utilities
// -------------------------------------------------------------