Repository: ousecTic/pern-todo-app Branch: master Commit: feb4b26b1548 Files: 19 Total size: 15.7 KB Directory structure: gitextract_ie_02kel/ ├── README.md ├── client/ │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── public/ │ │ ├── index.html │ │ ├── manifest.json │ │ └── robots.txt │ └── src/ │ ├── App.css │ ├── App.js │ ├── components/ │ │ ├── EditTodo.js │ │ ├── InputTodo.js │ │ └── ListTodos.js │ ├── index.css │ └── index.js └── server/ ├── .gitignore ├── database.sql ├── db.js ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # pern-todo-app https://www.youtube.com/watch?v=ldYcgPKEZC8 ================================================ FILE: client/.gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # production /build # misc .DS_Store .env.local .env.development.local .env.test.local .env.production.local npm-debug.log* yarn-debug.log* yarn-error.log* ================================================ FILE: client/README.md ================================================ This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you can run: ### `yarn start` Runs the app in the development mode.
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. The page will reload if you make edits.
You will also see any lint errors in the console. ### `yarn test` Launches the test runner in the interactive watch mode.
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. ### `yarn build` Builds the app for production to the `build` folder.
It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.
Your app is ready to be deployed! See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. ### `yarn eject` **Note: this is a one-way operation. Once you `eject`, you can’t go back!** If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. ## Learn More You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). To learn React, check out the [React documentation](https://reactjs.org/). ### Code Splitting This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting ### Analyzing the Bundle Size This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size ### Making a Progressive Web App This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app ### Advanced Configuration This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration ### Deployment This section has moved here: https://facebook.github.io/create-react-app/docs/deployment ### `yarn build` fails to minify This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify ================================================ FILE: client/package.json ================================================ { "name": "client", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "react": "^16.13.0", "react-dom": "^16.13.0", "react-scripts": "3.4.0" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } } ================================================ FILE: client/public/index.html ================================================ React App
================================================ FILE: client/public/manifest.json ================================================ { "short_name": "React App", "name": "Create React App Sample", "icons": [ { "src": "favicon.ico", "sizes": "64x64 32x32 24x24 16x16", "type": "image/x-icon" }, { "src": "logo192.png", "type": "image/png", "sizes": "192x192" }, { "src": "logo512.png", "type": "image/png", "sizes": "512x512" } ], "start_url": ".", "display": "standalone", "theme_color": "#000000", "background_color": "#ffffff" } ================================================ FILE: client/public/robots.txt ================================================ # https://www.robotstxt.org/robotstxt.html User-agent: * Disallow: ================================================ FILE: client/src/App.css ================================================ .App { text-align: center; } .App-logo { height: 40vmin; pointer-events: none; } @media (prefers-reduced-motion: no-preference) { .App-logo { animation: App-logo-spin infinite 20s linear; } } .App-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); color: white; } .App-link { color: #61dafb; } @keyframes App-logo-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } ================================================ FILE: client/src/App.js ================================================ import React, { Fragment } from "react"; import "./App.css"; //components import InputTodo from "./components/InputTodo"; import ListTodos from "./components/ListTodos"; function App() { return (
); } export default App; ================================================ FILE: client/src/components/EditTodo.js ================================================ import React, { Fragment, useState } from "react"; const EditTodo = ({ todo }) => { const [description, setDescription] = useState(todo.description); //edit description function const updateDescription = async e => { e.preventDefault(); try { const body = { description }; const response = await fetch( `http://localhost:5000/todos/${todo.todo_id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } ); window.location = "/"; } catch (err) { console.error(err.message); } }; return ( {/* id = id10 */} ); }; export default EditTodo; ================================================ FILE: client/src/components/InputTodo.js ================================================ import React, { Fragment, useState } from "react"; const InputTodo = () => { const [description, setDescription] = useState(""); const onSubmitForm = async e => { e.preventDefault(); try { const body = { description }; const response = await fetch("http://localhost:5000/todos", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); window.location = "/"; } catch (err) { console.error(err.message); } }; return (

Pern Todo List

setDescription(e.target.value)} />
); }; export default InputTodo; ================================================ FILE: client/src/components/ListTodos.js ================================================ import React, { Fragment, useEffect, useState } from "react"; import EditTodo from "./EditTodo"; const ListTodos = () => { const [todos, setTodos] = useState([]); //delete todo function const deleteTodo = async id => { try { const deleteTodo = await fetch(`http://localhost:5000/todos/${id}`, { method: "DELETE" }); setTodos(todos.filter(todo => todo.todo_id !== id)); } catch (err) { console.error(err.message); } }; const getTodos = async () => { try { const response = await fetch("http://localhost:5000/todos"); const jsonData = await response.json(); setTodos(jsonData); } catch (err) { console.error(err.message); } }; useEffect(() => { getTodos(); }, []); console.log(todos); return ( {" "} {/* */} {todos.map(todo => ( ))}
Description Edit Delete
John Doe john@example.com
{todo.description}
); }; export default ListTodos; ================================================ FILE: client/src/index.css ================================================ body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } code { font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; } ================================================ FILE: client/src/index.js ================================================ import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; import App from "./App"; ReactDOM.render(, document.getElementById("root")); ================================================ FILE: server/.gitignore ================================================ node_modules ================================================ FILE: server/database.sql ================================================ CREATE DATABASE perntodo; CREATE TABLE todo( todo_id SERIAL PRIMARY KEY, description VARCHAR(255) ); ================================================ FILE: server/db.js ================================================ const Pool = require("pg").Pool; const pool = new Pool({ user: "postgres", password: "kthl8822", host: "localhost", port: 5432, database: "perntodo" }); module.exports = pool; ================================================ FILE: server/index.js ================================================ const express = require("express"); const app = express(); const cors = require("cors"); const pool = require("./db"); //middleware app.use(cors()); app.use(express.json()); //req.body //ROUTES// //create a todo app.post("/todos", async (req, res) => { try { const { description } = req.body; const newTodo = await pool.query( "INSERT INTO todo (description) VALUES($1) RETURNING *", [description] ); res.json(newTodo.rows[0]); } catch (err) { console.error(err.message); } }); //get all todos app.get("/todos", async (req, res) => { try { const allTodos = await pool.query("SELECT * FROM todo"); res.json(allTodos.rows); } catch (err) { console.error(err.message); } }); //get a todo app.get("/todos/:id", async (req, res) => { try { const { id } = req.params; const todo = await pool.query("SELECT * FROM todo WHERE todo_id = $1", [ id ]); res.json(todo.rows[0]); } catch (err) { console.error(err.message); } }); //update a todo app.put("/todos/:id", async (req, res) => { try { const { id } = req.params; const { description } = req.body; const updateTodo = await pool.query( "UPDATE todo SET description = $1 WHERE todo_id = $2", [description, id] ); res.json("Todo was updated!"); } catch (err) { console.error(err.message); } }); //delete a todo app.delete("/todos/:id", async (req, res) => { try { const { id } = req.params; const deleteTodo = await pool.query("DELETE FROM todo WHERE todo_id = $1", [ id ]); res.json("Todo was deleted!"); } catch (err) { console.log(err.message); } }); app.listen(5000, () => { console.log("server has started on port 5000"); }); ================================================ FILE: server/package.json ================================================ { "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "henry", "license": "ISC", "dependencies": { "cors": "^2.8.5", "express": "^4.17.1", "pg": "^7.18.2" } }