Skip to content

Commit

Permalink
update projects
Browse files Browse the repository at this point in the history
  • Loading branch information
fawzi-iit committed Dec 7, 2023
1 parent f42b27b commit 5945c4a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
35 changes: 33 additions & 2 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function loadProjectsForProjectsPage() {
// Use existing logic from loadProjects but adapt for projects.html
}

// Start Updated 12/06 5:30PM
function loadProjectsForProjectsPage() {
fetch('/projects')
.then(response => response.json())
Expand All @@ -96,20 +97,50 @@ function loadProjectsForProjectsPage() {
projectDiv.innerHTML = `
<h2><a href="#" data-projectid="${project._id}" class="project-link">${project.name}</a></h2>
<p>${project.description}</p>
<p>${project.status}</p>
<p>Status:
<select onchange="updateProjectStatus('${project._id}', this.value)">
<option value="Active" ${project.status === 'Active' ? 'selected' : ''}>Active</option>
<option value="Completed" ${project.status === 'Completed' ? 'selected' : ''}>Completed</option>
<option value="On Hold" ${project.status === 'On Hold' ? 'selected' : ''}>On Hold</option>
</select>
</p>
`;
projectsContainer.appendChild(projectDiv);
});

// Hide the 'Project List' button
const projectListBackButton = document.getElementById('projectListBackButton');
if (projectListBackButton) {
projectListBackButton.style.display = 'none';
}
}

attachProjectClickHandlers();
})
.catch(error => console.error('Error:', error));
}

function updateProjectStatus(projectId, newStatus) {
fetch(`/projects/${projectId}/status`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ status: newStatus })
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(updatedProject => {
console.log('Project status updated:', updatedProject);
// Optionally, refresh the project list or update the UI as needed
})
.catch(error => console.error('Error:', error));
}
// End Update 12/06 5:30PM

// End of New functions Dec 5th 11:50

// Event listener for Create Task Button
Expand Down
15 changes: 15 additions & 0 deletions routes/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ router.delete('/:id', async (req, res) => {
}
});

// PUT route to update a project's status
router.put('/:id/status', async (req, res) => {
try {
const project = await Project.findById(req.params.id);
if (!project) {
return res.status(404).send('Project not found');
}
project.status = req.body.status;
await project.save();
res.status(200).json(project);
} catch (error) {
res.status(500).send('Error updating project status: ' + error.message);
}
});

module.exports = router;

0 comments on commit 5945c4a

Please sign in to comment.