Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 1 7 #51

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
File renamed without changes.
27,413 changes: 27,413 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "ctd-react-albatross",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1"
},
"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"
]
}
}
Binary file added public/favicon.ico
Binary file not shown.
33 changes: 33 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Web site created using create-react-app" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
36 changes: 36 additions & 0 deletions src/AddTodoForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, {useState} from "react";
import InputWithLabel from "./InputWithLabel";

const AddTodoForm = ({onAddTodo}) => {
const [todoTitle, setTodoTitle]= useState("")

const handleTitleChange = (event) => {
const newTodoTitle = event.target.value;
setTodoTitle(newTodoTitle);
};
const handleAddTodo=(event) =>{
event.preventDefault();

onAddTodo({title: todoTitle, id: Date.now()});
setTodoTitle("");
};

return(
<form onSubmit={handleAddTodo}>
<InputWithLabel
id="todoTitle"
name="title"
value={todoTitle}
onChange={handleTitleChange}
children
inputRef
>
Title:
</InputWithLabel>
<button type="submit">Add</button>
</form>
)
};


export default AddTodoForm;
58 changes: 58 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, {useState, useEffect} from 'react';
import TodoList from './TodoList';
import AddTodoForm from './AddTodoForm';
// import TodoListItem from './TodoListItem';


function App() {


const [todoList, setTodoList] = useState([]);

const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
new Promise((resolve, reject) =>
setTimeout(
() =>resolve({data: {todoList: JSON.parse(localStorage.getItem('savedTodoList') || '[]'
) } } ),
2000
)
)

.then(result => {
setTodoList(result.data.todoList);
setIsLoading(false);
})
}, []);

useEffect(() => {
if (!isLoading) {
localStorage.setItem('savedTodoList', JSON.stringify(todoList));
}
},[todoList, isLoading])

const addTodo = (newTodo) => {
setTodoList([...todoList, newTodo])
}

const removeTodo = (id) => {
const newTodoList = todoList.filter((todo) => todo.title !== id);

setTodoList(newTodoList);
}

return (
<div className="App">

{isLoading ? (<p>Loading ...</p>):
(<TodoList todoList={todoList}
onRemoveTodo={removeTodo}/>)
}
<AddTodoForm
onAddTodo={addTodo}/>
</div>
);
}

export default App;
31 changes: 31 additions & 0 deletions src/InputWithLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, {useEffect, useRef} from "react"

const InputWithLabel = (props) => {

const inputRef = useRef();

useEffect(() => {
inputRef.current.focus();
},
[]);

return (
<>
<label htmlFor ="todoTitle">{props.children}</label>
<input
id={props.id}
name={props.name}
value={props.value}
onChange={props.onChange}
ref={inputRef}
/>
</>
)
}






export default InputWithLabel
25 changes: 25 additions & 0 deletions src/TodoForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";


function TodoForm() {
const handleChange = (e) => {
console.log(e);
}
return(
<form>
<label type="htmlFor" id="todoTitle"> Title </label>
<input type= "text" id="todoTitle" onChange={handleChange}></input>
<button>Add</button>
</form>
)
}









export default TodoForm;
22 changes: 22 additions & 0 deletions src/TodoList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react"
import TodoListItem from "./TodoListItem";

const TodoList =({todoList, onRemoveTodo}) => {

return (
<div>
<h1> To Do List</h1>
{todoList.map((todo ) =>

<TodoListItem
key={todo.id}
title={todo.title}

onRemoveTodo={onRemoveTodo}
/>
)}
</div>
)
};

export default TodoList;
17 changes: 17 additions & 0 deletions src/TodoListItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";


const TodoListItem = ({title, onRemoveTodo}) => {

return (
<li>
<span>
{title}
</span>
<button type="button" onClick={() => onRemoveTodo(title)}> Remove </button>
</li>
)
}


export default TodoListItem;
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import {createRoot} from 'react-dom/client';
import App from './App';


const root = createRoot(document.getElementById('root'));

root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);