Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fawzi-iit committed Dec 2, 2023
1 parent 31ee013 commit 1035b84
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 20 deletions.
17 changes: 11 additions & 6 deletions models/task.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose');

const taskSchema = new mongoose.Schema({
// ... existing fields ...

title: {
type: String,
required: true // Assuming each task should have a title
Expand All @@ -25,17 +25,22 @@ const taskSchema = new mongoose.Schema({
enum: ['High', 'Medium', 'Low'],
default: 'Medium'
},

status: {
type: String,
enum: ['Not Started', 'In Progress', 'Completed'],
enum: ['Not Started', 'In Progress', 'Completed'],
default: 'Not Started'
},
deadline: Date,
progress: {
type: Number,
default: 0 // Percentage of task completion
}
// Add any other fields as necessary
default: 0,
validate: {
validator: function(v) {
return v >= 0 && v <= 100;
},
message: props => `${props.value} is not a valid progress percentage!`
}
},
});

module.exports = mongoose.model('Task', taskSchema);
5 changes: 2 additions & 3 deletions populateData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');
const Task = require('./models/task'); // Adjust path as necessary
const Project = require('./models/project'); // Adjust path as necessary
const Task = require('./models/task');
const Project = require('./models/project');

mongoose.connect('mongodb://localhost:27017/iitappdb', { useNewUrlParser: true, useUnifiedTopology: true });

Expand All @@ -13,7 +13,6 @@ async function populateData() {
const project = new Project({
name: 'Project 1',
description: 'Sample Project',
// Add other project fields as necessary
});
await project.save();

Expand Down
4 changes: 4 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ footer {
color: green;
}

.btn-primary {
background-color: #4cae4c; /* Darker shade for hover effect */
}


/* Media Queries for Responsive Design */
@media (max-width: 700px) {
Expand Down
45 changes: 45 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ document.addEventListener('DOMContentLoaded', function() {
event.preventDefault();
showCreateProjectForm();
});

document.querySelectorAll('.task-priority, .task-status, .task-progress').forEach(element => {
element.addEventListener('change', (event) => {
const taskId = event.target.dataset.taskId;
const updatedValue = { [event.target.className]: event.target.value };
// Send PATCH request to server
// ...
});
});

document.querySelectorAll('.task-priority, .task-status, .task-progress').forEach(element => {
element.addEventListener('change', async (event) => {
const taskId = event.target.dataset.taskId;
const updatedValue = { [event.target.className.split('-')[1]]: event.target.value };

try {
const response = await fetch(`/tasks/${taskId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedValue),
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

// Handle successful response here, if needed
} catch (error) {
console.error('Fetch error:', error);
}
});
});
});

function createProject() {
Expand Down Expand Up @@ -106,6 +140,17 @@ function fetchTasksForProject(projectId) {
taskDiv.innerHTML = `
<h3>${task.title}</h3>
<p>${task.description}</p>
<select class="task-priority" data-task-id="${task._id}">
<option value="High" ${task.priority === 'High' ? 'selected' : ''}>High</option>
<option value="Medium" ${task.priority === 'Medium' ? 'selected' : ''}>Medium</option>
<option value="Low" ${task.priority === 'Low' ? 'selected' : ''}>Low</option>
</select>
<select class="task-status" data-task-id="${task._id}">
<option value="Not Started" ${task.status === 'Not Started' ? 'selected' : ''}>Not Started</option>
<option value="In Progress" ${task.status === 'In Progress' ? 'selected' : ''}>In Progress</option>
<option value="Completed" ${task.status === 'Completed' ? 'selected' : ''}>Completed</option>
</select>
<input type="range" class="task-progress" data-task-id="${task._id}" value="${task.progress}" min="0" max="100">
<!-- ... other task details ... -->
`;
projectsContainer.appendChild(taskDiv);
Expand Down
42 changes: 39 additions & 3 deletions routes/tasks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const Task = require('../models/task'); // Adjust the path as necessary
const Task = require('../models/task');

// GET route to list all tasks (general)
router.get('/', async (req, res) => {
Expand All @@ -15,13 +15,23 @@ router.get('/', async (req, res) => {
// GET route to list tasks for a specific project
router.get('/project/:projectId', async (req, res) => {
try {
const tasks = await Task.find({ projectId: req.params.projectId });
//const tasks = await Task.find({ projectId: req.params.projectId });
const tasks = await Task.find({ projectId: req.params.projectId }).populate('assignee');
res.json(tasks);
} catch (error) {
res.status(500).send('Error fetching tasks for project: ' + error.message);
}
});

router.get('/tasks', async (req, res) => {
try {
const tasks = await Task.find().populate('assignee');
res.render('taskList', { tasks: tasks });
} catch (err) {
res.status(500).send(err);
}
});

// POST route to create a new task
router.post('/', async (req, res) => {
const task = new Task({
Expand All @@ -40,6 +50,32 @@ router.post('/', async (req, res) => {
}
});

// Additional routes for updating and deleting tasks can also be added here
router.patch('/tasks/:id', async (req, res) => {
try {
const { id } = req.params;
const update = {};

// Validate and set the fields if they exist in the request body
if (req.body.priority) update.priority = req.body.priority;
if (req.body.status) update.status = req.body.status;
if (req.body.progress) update.progress = req.body.progress;

// Log the incoming update for debugging
console.log("PATCH request received for task ID:", req.params.id);
console.log("Request body:", req.body);

const updatedTask = await Task.findByIdAndUpdate(id, update, { new: true });

if (!updatedTask) {
return res.status(404).send('Task not found');
}

res.json(updatedTask);
} catch (err) {
console.error('Error updating task:', err);
res.status(500).send(err);
}
});


module.exports = router;
42 changes: 42 additions & 0 deletions views/createTask.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extends layout

block content
h1 Create New Task
form(action="/tasks" method="post")
// Include projectId as a hidden field if it exists
if projectId
input(type="hidden" name="projectId" value=projectId)

// Field for Task Title
div.form-group
label(for="title") Title
input#title.form-control(type="text" name="title" required)

// Field for Task Description
div.form-group
label(for="description") Description
textarea#description.form-control(name="description" required)

// Field for Task Priority
div.form-group
label(for="priority") Priority
select#priority.form-control(name="priority")
option(value="High") High
option(value="Medium") Medium
option(value="Low") Low

// Field for Task Status
div.form-group
label(for="status") Status
select#status.form-control(name="status")
option(value="Not Started") Not Started
option(value="In Progress") In Progress
option(value="Completed") Completed

// Field for Task Progress
div.form-group
label(for="progress") Progress
input#progress.form-control(type="number" name="progress" min="0" max="100")

// Submit Button
button.btn.btn-primary(type="submit") Create Task
4 changes: 3 additions & 1 deletion views/project.pug
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ block content
ul
each task in project.tasks
li #{task.name} - #{task.status}
a(href=`/projects/${project._id}/tasks/new`) Add New Task

// Updated "Create Task" button
a(href=`/tasks/create?projectId=${project._id}`).button-like Create Task
31 changes: 24 additions & 7 deletions views/projectTasks.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ block content
ul
each task in tasks
li
#{task.name}
- if (task.status === 'Pending')
form(action=`/tasks/${task._id}/complete` method="post")
button Complete
- if (task.status === 'Completed')
| Completed
| Task: #{task.title}
br
| Description: #{task.description}
br
| Priority: #{task.priority}
br
| Progress: #{task.progress}%
br
if task.assignee
| Assignee: #{task.assignee.name} // Adjust based on your user model
else
| Assignee: Not assigned
br
- if (task.status === 'Not Started')
| Status: Not Started
- else if (task.status === 'In Progress')
| Status: In Progress
- else if (task.status === 'Completed')
| Status: Completed
br
form(action=`/tasks/${task._id}/update` method="post")
// Add form fields for updating task details
button Update Task
form(action=`/tasks/${task._id}/delete` method="post")
button Delete
button Delete Task
a(href=`/projects/${project._id}/tasks/new`) Add New Task
12 changes: 12 additions & 0 deletions views/taskList.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends layout

block content
h1 Task List
ul
each task in tasks
li
| Task: #{task.name} - Priority: #{task.priority} - Progress: #{task.progress}%
if task.assignee
| - Assignee: #{task.assignee.name}
else
| - Assignee: Not Assigned

0 comments on commit 1035b84

Please sign in to comment.