Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.

Commit 1979b3c

Browse files
committed
task edit fully functional
1 parent ca65ad9 commit 1979b3c

5 files changed

Lines changed: 85 additions & 25 deletions

File tree

server/controllers/tasksController.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const tasksController = {
1010
status: req.body.status,
1111
notes: req.body.tasknotes,
1212
});
13-
res.locals.createdTask = createdTask;
13+
res.locals.task = createdTask;
1414

1515
return next();
1616
} catch (err) {
@@ -27,18 +27,18 @@ const tasksController = {
2727
const column = req.body.status;
2828
let updateQuery;
2929
if (column === "backlog") {
30-
updateQuery = { $push: { backlog: res.locals.createdTask._id } };
30+
updateQuery = { $push: { backlog: res.locals.task._id } };
3131
} else if (column === "inProgress") {
3232
updateQuery = {
33-
$push: { inProgress: res.locals.createdTask._id },
33+
$push: { inProgress: res.locals.task._id },
3434
};
3535
} else if (column === "inReview") {
3636
updateQuery = {
37-
$push: { inReview: res.locals.createdTask._id },
37+
$push: { inReview: res.locals.task._id },
3838
};
3939
} else {
4040
updateQuery = {
41-
$push: { completed: res.locals.createdTask._id },
41+
$push: { completed: res.locals.task._id },
4242
};
4343
}
4444
await Board.updateOne({ _id: req.body.boardId }, updateQuery);
@@ -72,12 +72,16 @@ const tasksController = {
7272
editTask: async (req: Request, res: Response, next: NextFunction) => {
7373
try {
7474
const taskEdits = req.body;
75-
const editedTask = await Card.findByIdAndUpdate({
76-
name: taskEdits.taskname,
77-
status: taskEdits.status,
78-
notes: taskEdits.tasknotes,
79-
});
80-
res.locals.editedTask = editedTask;
75+
const editedTask = await Card.findByIdAndUpdate(
76+
taskEdits.taskId,
77+
{
78+
name: taskEdits.taskname,
79+
status: taskEdits.status,
80+
notes: taskEdits.tasknotes,
81+
},
82+
{ new: true }
83+
);
84+
res.locals.task = editedTask;
8185

8286
return next();
8387
} catch (err) {
@@ -89,6 +93,36 @@ const tasksController = {
8993
});
9094
}
9195
},
96+
pullTask: async (req: Request, res: Response, next: NextFunction) => {
97+
try {
98+
const column = req.body.startColumn;
99+
let updateQuery;
100+
if (column === "backlog") {
101+
updateQuery = { $pull: { backlog: res.locals.task._id } };
102+
} else if (column === "inProgress") {
103+
updateQuery = {
104+
$pull: { inProgress: res.locals.task._id },
105+
};
106+
} else if (column === "inReview") {
107+
updateQuery = {
108+
$pull: { inReview: res.locals.task._id },
109+
};
110+
} else {
111+
updateQuery = {
112+
$pull: { completed: res.locals.task._id },
113+
};
114+
}
115+
await Board.updateOne({ _id: req.body.boardId }, updateQuery);
116+
return next();
117+
} catch (err) {
118+
// pass error through to global error handler
119+
return next({
120+
log: `tasksController.pullTask ERROR: ${err}`,
121+
status: 500,
122+
message: { err: "Error pulling Task" },
123+
});
124+
}
125+
},
92126
};
93127

94128
export default tasksController;

server/routes/tasksRouter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ router.post(
1313
tasksController.createTask,
1414
tasksController.assignTask,
1515
(_req: Request, res: Response) => {
16-
return res.status(200).json(res.locals.createdTask);
16+
return res.status(200).json(res.locals.task);
1717
}
1818
);
1919

2020
// route for editing a task card
2121
router.post(
2222
"/edit",
2323
tasksController.editTask,
24+
tasksController.pullTask,
25+
tasksController.assignTask,
2426
(_req: Request, res: Response) => {
25-
return res.status(200).json(res.locals.editedTask);
27+
return res.status(200).json(res.locals.task);
2628
}
2729
);
2830

src/components/Column.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const Column = ({
8080
currentBoard={currentBoard}
8181
task={editingTask}
8282
setBoardState={setBoardState}
83+
startColumn={name}
8384
/>
8485
) : null}
8586
</div>

src/components/EditTaskModal.tsx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const EditTaskModal = ({
1313
currentBoard,
1414
setBoardState,
1515
task,
16+
startColumn,
1617
}: EditTaskModalProps) => {
1718
const [formData, setFormData] = useState<TaskFormState>({
1819
taskname: task.name,
@@ -23,11 +24,13 @@ const EditTaskModal = ({
2324
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
2425
e.preventDefault;
2526
console.log("Edit Task Form Submitted: ", formData);
26-
// send POST request with the new task card, user and current board
27+
// send POST request with the new task card, edited task and current board
2728
const fetchEditTask = async () => {
2829
const body = {
2930
...formData,
30-
taskId: task?._id,
31+
taskId: task._id,
32+
boardId: currentBoard.id,
33+
startColumn: startColumn,
3134
};
3235
const response: Response = await fetch(`/tasks/edit`, {
3336
method: "POST",
@@ -36,14 +39,33 @@ const EditTaskModal = ({
3639
},
3740
body: JSON.stringify(body),
3841
});
39-
const task: TaskState = await response.json();
42+
const editedTask: TaskState = await response.json();
4043
if (response.status === 200) {
41-
console.log("edited task: ", task);
4244
// update the board state, removing from array if necessary
43-
setBoardState((prevState: BoardState) => ({
44-
...prevState,
45-
[task.status]: [...prevState[task.status], task],
46-
}));
45+
setBoardState((prevState: BoardState) => {
46+
const column = [...prevState[startColumn]];
47+
const idx = column.indexOf(task);
48+
// if changing columns, remove from startColumn and add to new column
49+
if (task.status !== editedTask.status) {
50+
column.splice(idx, 1);
51+
return {
52+
...prevState,
53+
[editedTask.status]: [
54+
...prevState[editedTask.status],
55+
editedTask,
56+
],
57+
[startColumn]: column,
58+
};
59+
}
60+
// else update the existing column with new task name and notes
61+
else {
62+
column[idx] = editedTask;
63+
return {
64+
...prevState,
65+
[startColumn]: column,
66+
};
67+
}
68+
});
4769
}
4870
};
4971
fetchEditTask().catch(console.error);
@@ -81,7 +103,7 @@ const EditTaskModal = ({
81103
id="backlog"
82104
value={"backlog"}
83105
onChange={handleInputChange}
84-
checked={task.status === "backlog"}
106+
defaultChecked={task.status === "backlog"}
85107
/>
86108
<label htmlFor="backlog">Backlog</label>
87109
<input
@@ -90,7 +112,7 @@ const EditTaskModal = ({
90112
id="in-progress"
91113
value={"inProgress"}
92114
onChange={handleInputChange}
93-
checked={task.status === "inProgress"}
115+
defaultChecked={task.status === "inProgress"}
94116
/>
95117
<label htmlFor="in-progress">In Progress</label>
96118
<input
@@ -99,7 +121,7 @@ const EditTaskModal = ({
99121
id="in-review"
100122
value={"inReview"}
101123
onChange={handleInputChange}
102-
checked={task.status === "inReview"}
124+
defaultChecked={task.status === "inReview"}
103125
/>
104126
<label htmlFor="in-review">In Review</label>
105127
<input
@@ -108,7 +130,7 @@ const EditTaskModal = ({
108130
id="completed"
109131
value={"completed"}
110132
onChange={handleInputChange}
111-
checked={task.status === "completed"}
133+
defaultChecked={task.status === "completed"}
112134
/>
113135
<label htmlFor="completed">Completed</label>
114136
</div>

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type EditTaskModalProps = {
4343
currentBoard: CurrentBoardState;
4444
setBoardState: React.Dispatch<React.SetStateAction<BoardState>>;
4545
task: TaskState;
46+
startColumn: "backlog" | "inProgress" | "inReview" | "completed";
4647
};
4748

4849
export type ColumnProps = {

0 commit comments

Comments
 (0)