Skip to content

Commit

Permalink
iiitl#8 :Feature/task update
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushkumar079 committed Mar 16, 2024
1 parent 50a9b8b commit 910ab1f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
76 changes: 76 additions & 0 deletions frontend/src/components/EditForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { useState } from "react";
import { useAuthContext } from "../hooks/useAuthContext";
import { useTasksContext } from "../hooks/usetasksContext";

const EditForm = ({ task, setEdit }) => {
const [title, setTitle] = useState("");
const [description, setDescription] = useState("");
const [progress, setProgress] = useState("");
const { dispatch } = useTasksContext();
const { user } = useAuthContext();

const handleSubmit = async (e) => {
e.preventDefault();
const newtask = {
title: title || "Please Enter a title",
description: description || "Please Enter a description",
progress: progress || 0,
};

const response = await fetch("/api/tasks/" + task._id, {
method: "PATCH",
body: JSON.stringify(newtask),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${user.token}`,
},
});
const json = await response.json();
console.log(json);

if (response.ok) {
dispatch({ type: "CHANGE_TASK", payload: { _id: task._id, ...newtask } });
}
setEdit(false);
setProgress('')
setDescription('')
setTitle('');
};

return (
<div>
<input
type="text"
placeholder="TaskName"
onChange={(e) => setTitle(e.target.value)}
value={title}
required
></input>
<input
required
type="text"
placeholder="Description"
onChange={(e) => setDescription(e.target.value)}
value={description}
></input>
<input
type="number"
placeholder="Progress"
value={progress}
required
onChange={(e) => setProgress(e.target.value)}
></input>
<button
type="submit"
className="button"
onClick={handleSubmit}
value={progress}
disabled={!title || !description || !progress}
>
Change
</button>
</div>
);
};

export { EditForm };
15 changes: 12 additions & 3 deletions frontend/src/components/TaskDetails.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useTasksContext } from "../hooks/usetasksContext";
import { useAuthContext } from "../hooks/useAuthContext";

import { useState } from "react";
import { EditForm } from "./EditForm";
const TaskDetails = ({ task }) => {
const { dispatch } = useTasksContext();
const { user } = useAuthContext();

const [edit, setEdit] = useState(false);
const handleClick = async () => {
if (!user) {
return;
Expand All @@ -25,6 +26,7 @@ const TaskDetails = ({ task }) => {

return (
<div className="task-details">

<h3>{task.title}</h3>

<p>
Expand All @@ -35,9 +37,16 @@ const TaskDetails = ({ task }) => {
<strong>Progress: </strong>
{task.progress}
</p>
<span className="material-symbols-outlined" onClick={handleClick}>
<span className="material-symbols-outlined delete" onClick={handleClick}>
delete
</span>
<span
className="material-symbols-outlined"
onClick={() => setEdit(!edit)}
>
edit
</span>
{edit && <EditForm task={task} setEdit={setEdit} />}
</div>
);
};
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/context/TaskContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const tasksReducer = (state, action) => {
return {
tasks: state.tasks.filter((w) => w._id !== action.payload._id),
};
case "CHANGE_TASK":
return{
tasks: [...state.tasks.filter((w) => w._id !== action.payload._id), action.payload]
}
default:
return state;
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ form.signup, form.login {
padding: 20px;
background: #fff;
border-radius: 4px;
}

.delete{
margin-right: 50px;
}

0 comments on commit 910ab1f

Please sign in to comment.