Last active
May 11, 2022 06:23
-
-
Save phptuts/9604eae6dc0ed3865da34a8a6b9331b7 to your computer and use it in GitHub Desktop.
React JS Crash Course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Header from './components/Header'; | |
import Footer from './components/Footer'; | |
import About from './components/About'; | |
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; | |
import { useState, useEffect } from 'react'; | |
import Home from './components/Home'; | |
function App() { | |
const [tasks, setTasks] = useState([]); | |
useEffect(() => { | |
const getTasks = async () => { | |
const taskFromServer = await fetchTasks(); | |
setTasks(taskFromServer); | |
}; | |
getTasks(); | |
}, []); | |
const fetchTasks = async () => { | |
const res = await fetch('http://localhost:5000/tasks'); | |
const data = await res.json(); | |
return data; | |
}; | |
const fetchTask = async (id) => { | |
const res = await fetch(`http://localhost:5000/tasks/${id}`); | |
const data = await res.json(); | |
return data; | |
}; | |
const [showAddTask, setShowAddTask] = useState(false); | |
const deleteTask = async (id) => { | |
await fetch(`http://localhost:5000/tasks/${id}`, { | |
method: 'DELETE', | |
}); | |
setTasks(tasks.filter((t) => t.id !== id)); | |
}; | |
const addTask = async (task) => { | |
// const id = Math.floor(Math.random() * 10000) + 1; | |
// const newTask = { id, ...task }; | |
// setTasks([...tasks, newTask]); | |
const res = await fetch(`http://localhost:5000/tasks`, { | |
method: 'POST', | |
headers: { | |
'Content-type': 'application/json', | |
}, | |
body: JSON.stringify(task), | |
}); | |
const data = await res.json(); | |
setTasks([...tasks, data]); | |
}; | |
const toggleReminder = async (id) => { | |
const taskToToggle = await fetchTask(id); | |
const updateTask = { ...taskToToggle, reminder: !taskToToggle.reminder }; | |
const res = await fetch(`http://localhost:5000/tasks/${id}`, { | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(updateTask), | |
}); | |
const data = await res.json(); | |
setTasks( | |
tasks.map((t) => { | |
if (t.id === id) { | |
return data; | |
} | |
return t; | |
}) | |
); | |
}; | |
return ( | |
<Router> | |
<div className="container"> | |
<Header | |
onAdd={() => setShowAddTask(!showAddTask)} | |
showAdd={showAddTask} | |
/> | |
<Routes> | |
<Route | |
path="/" | |
element={ | |
<Home | |
showAddTask={showAddTask} | |
addTask={addTask} | |
tasks={tasks} | |
deleteTask={deleteTask} | |
toggleReminder={toggleReminder} | |
/> | |
} | |
/> | |
<Route path="/about" component={About} /> | |
</Routes> | |
<Footer /> | |
</div> | |
</Router> | |
); | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Tasks from './Tasks'; | |
import AddTask from './AddTask'; | |
const Home = ({ showAddTask, addTask, deleteTask, toggleReminder, tasks }) => { | |
return ( | |
<> | |
{showAddTask && <AddTask onAdd={addTask} />} | |
<Tasks tasks={tasks} deleteTask={deleteTask} onToggle={toggleReminder} /> | |
</> | |
); | |
}; | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment