diff --git a/api/data/data.js b/api/data/data.js index 64b34de..aad3db8 100644 --- a/api/data/data.js +++ b/api/data/data.js @@ -1,13 +1,15 @@ import fs from "fs" + const data = {} data.findTasks = (condition, callback) => { - fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { + fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => { if (error) { + console.log(error) - callback(error) + callback(error.message) return } @@ -25,7 +27,7 @@ data.findTasks = (condition, callback) => { data.insertTask = (task, callback) => { - fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { + fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => { if (error) { console.log(error) @@ -43,7 +45,7 @@ data.insertTask = (task, callback) => { const jsonTasks = JSON.stringify(tasks) - fs.writeFile('../api/data/tasks.json', jsonTasks, (error) => { + fs.writeFile('./data/tasks.json', jsonTasks, (error) => { if (error) { console.log(error) @@ -53,7 +55,59 @@ data.insertTask = (task, callback) => { callback(null) }) }) +} + +data.findOneTask = (condition, callback) => { + fs.readFile("./data/tasks.json", "utf-8", (error, tasksJson) => { + if (error) { + callback(new Error(error)) + + return + } + + if (!tasksJson) { + tasksJson = "[]" + } + + const tasks = JSON.parse(tasksJson) + const taskFind = tasks.find(condition) + + callback(null, taskFind) + }) +} + +data.deleteTask = (condition, callback) => { + + fs.readFile("./data/tasks.json", "utf8", (error, tasksJson) => { + if (error) { + callback(new Error(error.message)) + return + } + + if (!tasksJson) { + tasksJson = "[]" + } + const tasks = JSON.parse(tasksJson) + + const index = tasks.findIndex(condition) + + if (index > -1) { + const deletedTask = tasks.splice(index, 1)[0] + const newJson = JSON.stringify(tasks) + + fs.writeFile("./data/tasks.json", newJson, (error) => { + if (error) { + callback(new Error(error.message)) + return + } + + callback(null, deletedTask) + }) + } else { + callback(null) + } + }) } export default data \ No newline at end of file diff --git a/api/data/tasks.json b/api/data/tasks.json index 09316b7..20e9c8c 100644 --- a/api/data/tasks.json +++ b/api/data/tasks.json @@ -1,4 +1,4 @@ [ - { "text": "Test prueba", "id": "8322284392957846-1718037846180" }, - { "text": "Test 2", "id": "9554433903962911-1718038107817" } + { "text": "test", "id": "8315203615181515-1718125009567" }, + { "text": "test 2", "id": "03419535378202143-1718125011402" } ] diff --git a/api/index.js b/api/index.js index f0a59a2..d756786 100644 --- a/api/index.js +++ b/api/index.js @@ -1,12 +1,13 @@ import express from 'express' import cors from 'cors' -import logic from './logic/logic.js' +import logic from '../api/logic/logic.js' const api = express() api.use(cors()) + const jsonBodyParser = express.json({ strict: true, type: 'application/json' }) api.get('/', (req, res) => { @@ -14,11 +15,27 @@ api.get('/', (req, res) => { }) +api.get("/tasks", (req, res) => { + + try { + logic.getAllTasks((error, tasks) => { + if (error) { + res.status(500).json({ error: error.message }) + return + } + res.json(tasks) + }) + + } catch (error) { + res.status(500).json({ error: error.message }) + } +}) + + api.post("/tasks", jsonBodyParser, (req, res) => { const { text } = req.body - try { logic.createTask(text, (error) => { if (error) { @@ -28,7 +45,6 @@ api.post("/tasks", jsonBodyParser, (req, res) => { } res.status(201).send({ message: 'task inserted, seguimos Laiiifff!!!' }) - }) } catch (error) { @@ -36,7 +52,27 @@ api.post("/tasks", jsonBodyParser, (req, res) => { } }) -api.listen(3000, () => console.log('Tamosss Laifff!!! http://localhost:3000')) +api.delete("/tasks/:taskId", (req, res) => { + + const { taskId } = req.params + + try { + logic.deleteTask(taskId, (error) => { + if (error) { + res.status(500).json({ error: error }) + return + } + + res.status(204).send() + }) + + } catch (error) { + + res.status(500).json({ error: error }) + } +}) + +api.listen(3001, () => console.log('Tamosss Laifff!!! http://localhost:3001')) diff --git a/api/logic/index.test.js b/api/logic/index.test.js index 4a2da24..81ea2af 100644 --- a/api/logic/index.test.js +++ b/api/logic/index.test.js @@ -1,32 +1,47 @@ import logic from "./logic.js" -try { - logic.findTasks(() => true, (error, tasks) => { - if (error) { - console.log(error) - } +// try { +// logic.getAllTasks(() => true, (error, tasks) => { +// if (error) { +// console.log(error) - console.log(tasks) +// } - }) -} catch (error) { +// console.log(tasks) - console.log(error) -} +// }) +// } catch (error) { + +// console.log(error) +// } // try { -// logic.createTask(11, 'Prueba', (error) => { -// if (error) { -// console.log(error) +// logic.createTask('Prueba', (error) => { +// if (error) { +// console.log(error) -// return -// } -// console.log('task inserted, seguimos Laiiifff!!!') +// return +// } +// console.log('task inserted, seguimos Laiiifff!!!') -// }) +// }) // } catch (error) { -// console.log(error) -// } \ No newline at end of file +// console.log(error) +// } + +try { + logic.deleteTask("8322284392957846-1718037846180", (error) => { + if (error) { + console.log("error") + return + } + + console.log("task deleted") + }) + +} catch (error) { + console.log(error) +} \ No newline at end of file diff --git a/api/logic/logic.js b/api/logic/logic.js index 34e65b2..b39ecc7 100644 --- a/api/logic/logic.js +++ b/api/logic/logic.js @@ -21,7 +21,7 @@ logic.createTask = (text, callback) => { text: text, } - data.insertTask(task, (error) => { + data.insertTask(task, error => { if (error) { callback(error) @@ -29,7 +29,29 @@ logic.createTask = (text, callback) => { } callback(null) + }) +} + +logic.deleteTask = (taskId, callback) => { + + data.findOneTask(task => task.id === taskId, (error, task) => { + if (error) { + callback(error) + return + } + + if (!task) { + callback(console.error(error.message)) + } + + data.deleteTask(task => task.id === taskId, (error) => { + if (error) { + callback(error) + return + } + callback(null) + }) }) } diff --git a/api/package.json b/api/package.json index 3c38bc8..d214543 100644 --- a/api/package.json +++ b/api/package.json @@ -1,19 +1,20 @@ { - "dependencies": { - "cors": "^2.8.5", - "express": "^4.19.2" - }, - "name": "api", - "version": "1.0.0", - "type": "module", - "main": "index.js", - "devDependencies": {}, - "scripts": { - "start": "node .", - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "description": "" -} \ No newline at end of file + "dependencies": { + "cors": "^2.8.5", + "express": "^4.19.2" + }, + "name": "api", + "version": "1.0.0", + "type": "module", + "main": "index.js", + "devDependencies": {}, + "scripts": { + "debug": "node --inspect-brk .", + "start": "node .", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "" +} diff --git a/app/src/component/TaskList.jsx b/app/src/component/TaskList.jsx index 2d38a0b..181e26d 100644 --- a/app/src/component/TaskList.jsx +++ b/app/src/component/TaskList.jsx @@ -1,4 +1,4 @@ -import { useState } from "react" +import { useState, useEffect } from "react" import logic from "../logic" @@ -37,10 +37,40 @@ function TaskList() { setTask("") } - const handleDeleteTask = (indexToDelete) => { - const taskUpdated = [...tasks] - taskUpdated.splice(indexToDelete, 1) - setTasks(taskUpdated) + useEffect(() => { + try { + logic.getAllTasks((error, tasks) => { + if (error) { + console.error(error) + alert(error.message) + return + } + + setTasks(tasks) + }) + } catch (error) { + if (error) { + console.error(error) + alert(error.message) + } + } + }, []) + + const handleDeleteTask = (indexToDelete, taskId) => { + try { + logic.deleteTask(taskId, (error) => { + if (error) { + console.error(error) + alert(error.message) + } + const taskUpdated = [...tasks] + taskUpdated.splice(indexToDelete, 1) + setTasks(taskUpdated) + }) + } catch (error) { + console.error(error) + alert(error.message) + } } const handleTaskCompleted = (indexToComplete) => { @@ -67,7 +97,7 @@ function TaskList() {

handleTaskCompleted(index)} className={task.completed ? "Completed" : ""}> {task.text}

- handleDeleteTask(index)}>❌ + handleDeleteTask(index, task.id)}>❌ ))} diff --git a/app/src/logic.js b/app/src/logic.js index 4533b6a..0d2d933 100644 --- a/app/src/logic.js +++ b/app/src/logic.js @@ -1,7 +1,7 @@ const logic = {} logic.createTask = (text, callback) => { - const xhr = new XMLHttpRequest() + const xhr = new XMLHttpRequest xhr.onload = () => { @@ -12,19 +12,64 @@ logic.createTask = (text, callback) => { return } - const { error, message } = JSON.parse(xhr.responseText) + const { error, message } = JSON.parse(xhr.response) callback({ error, message }) } - xhr.open("POST", "http://localhost:3000/tasks") - - const body = { text } + const body = { + text: text + } const json = JSON.stringify(body) + + xhr.open("POST", "http://localhost:3001/tasks") xhr.setRequestHeader("Content-Type", "application/json") xhr.send(json) } + +logic.getAllTasks = (callback) => { + + const xhr = new XMLHttpRequest + + xhr.onload = () => { + if (xhr.status === 200) { + const tasks = JSON.parse(xhr.response) + + callback(null, tasks) + console.log("Tasks loaded") + return + } + + const { error, message } = JSON.parse(xhr.response) + + callback({ error, message }) + } + + xhr.open("GET", "http://localhost:3001/tasks") + xhr.send() +} + +logic.deleteTask = (taskId, callback) => { + + const xhr = new XMLHttpRequest + + xhr.onload = () => { + if (xhr.status === (204)) { + callback(null) + return + } + + const { error, message } = JSON.parse(xhr.response) + + callback({ error, message }) + } + + xhr.open("DELETE", `http://localhost:3001/tasks/${taskId}`) + xhr.send() + +} + export default logic \ No newline at end of file diff --git a/app/src/main.jsx b/app/src/main.jsx index bec5e7d..34075cc 100644 --- a/app/src/main.jsx +++ b/app/src/main.jsx @@ -1,5 +1,5 @@ -import ReactDOM from "react-dom/client"; -import App from "./App.jsx"; -import "./global.css"; +import ReactDOM from "react-dom/client" +import App from "./App.jsx" +import "./global.css" -ReactDOM.createRoot(document.getElementById("root")).render(); +ReactDOM.createRoot(document.getElementById("root")).render()