Skip to content

Commit

Permalink
➕ feat: CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
Linder Hassinger committed Apr 13, 2024
1 parent dbd6707 commit f3fae2d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
11 changes: 9 additions & 2 deletions semana-9/todolist/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
UpdateForm,
CheckForm,
} from "./components";
import { getTasks, createTask } from "./services/httpAPI";
import {
getTasks,
createTask,
updateTask,
deleteTask,
} from "./services/httpAPI";

export default function App() {
const [listTasks, setListTask] = useState([]);
Expand Down Expand Up @@ -56,10 +61,12 @@ export default function App() {
handleOpen("check");
};

const handleUpdateTask = (task, newText) => {
const handleUpdateTask = async (task, newText) => {
const searchTask = listTasks.find((element) => element.id === task.id);
searchTask.text = newText;

await updateTask(searchTask);

handleOpen("edit");
};

Expand Down
4 changes: 2 additions & 2 deletions semana-9/todolist/src/components/UpdateForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export default function UpdateForm(props) {
setEditedTask(event.target.value);
};

const handleFormEditSubmit = (event) => {
const handleFormEditSubmit = async (event) => {
event.preventDefault();

props.handleUpdateTask(props.currentTask, editedTask);
await props.handleUpdateTask(props.currentTask, editedTask);
};

useEffect(() => {
Expand Down
26 changes: 25 additions & 1 deletion semana-9/todolist/src/services/httpAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ export async function createTask(task) {
"Content-Type": "application/json",
},
});
const data = response.json();
const data = await response.json();
return data;
}

export async function updateTask(task) {
const response = await fetch(`${BASE_URL}/${task.id}`, {
method: "PUT",
body: JSON.stringify(task),
headers: {
"Content-Type": "application/json",
},
});
const data = await response.json();
return data;
}

export async function deleteTask(task) {
const response = await fetch(`${BASE_URL}/${task.id}`, {
method: "DELETE",
headers: {
"Contet-Type": "application/json",
},
});
const data = await response.json();
return data;
}

0 comments on commit f3fae2d

Please sign in to comment.