Skip to content

Commit

Permalink
refactor: add export file für utils
Browse files Browse the repository at this point in the history
  • Loading branch information
DonLars committed Feb 19, 2024
1 parent a075272 commit d1234b0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
29 changes: 29 additions & 0 deletions apiutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const apiUrl = "http://localhost:4730/todos";

/* FUNCTION - to get todos from the API*/

export function fetchData(path = "", options = {}) {
return fetch(apiUrl + path, options).then((response) => response.json());
}

export function postData(newTodo) {
const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTodo),
};
return fetchData("", options);
//return fetch(apiUrl + path, options).then((response) => response.json());
}
/*
export function deleteAllData(newTodo) {
Promise.all(
doneTodos.map((doneTodo) =>
fetch(apiUrl + `/${doneTodo.id}`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
})
)
);
}
*/
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ <h2 class="claim">Getting things done…</h2>
</article>
</main>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="script.js"></script>
<script type="module" src="script.js"></script>
</body>
</html>
17 changes: 7 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

import { fetchData, postData } from "./apiutils.js";

/* State and initializing
========================================================================== */

Expand All @@ -8,8 +10,8 @@ const taskForm = document.getElementById("task-form");
const input = document.getElementById("input");
const taskList = document.getElementById("task-list");
const clearButton = document.getElementById("clear-btn");
const apiUrl = "http://localhost:4730/todos";

/* const apiUrl = "http://localhost:4730/todos";
*/
// Modal Styling

const corner = Swal.mixin({
Expand Down Expand Up @@ -58,8 +60,7 @@ function render() {
/* FUNCTION - to get todos from the API
========================================================================== */
function getTodosFromApi() {
fetch(apiUrl)
.then((response) => response.json())
fetchData() // Nochmal anschauen
.then((todosFromApi) => {
state.tasks = todosFromApi;
render();
Expand All @@ -80,12 +81,7 @@ function saveTodoToApi() {
const newTodoText = input.value.trim();
const newTodo = { description: newTodoText, done: false };

fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTodo),
})
.then((response) => response.json())
postData(newTodo)
.then((newTodoFromApi) => {
state.tasks.push(newTodoFromApi);
render();
Expand Down Expand Up @@ -140,6 +136,7 @@ function removeDoneTodosFromApi() {
const doneTodos = state.tasks.filter((task) => task.done);

// Use Promise.all for multiple API requests and delete many completed todos
//Promise All Settled
Promise.all(
doneTodos.map((doneTodo) =>
fetch(apiUrl + `/${doneTodo.id}`, {
Expand Down

0 comments on commit d1234b0

Please sign in to comment.