-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathApp.js
More file actions
92 lines (83 loc) · 2.94 KB
/
Copy pathApp.js
File metadata and controls
92 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React, {useEffect, useState} from 'react';
import LogoIcon from "./images/logo-icon.svg";
import CheckIcon from "./images/group_4949.svg";
import './App.css';
const baseUrl = 'https://' + process.env.REACT_APP_API_HOST + '.onrender.com'
const ListItem = ({ title }) => {
return (
<div className="task-row"><img src={CheckIcon} alt="" className="task-check-icon"/>
<h3 className="task-text">{title}</h3>
</div>
)
}
const TodoList = ({items}) => {
return (
<div>
{items.map((item) => (
<ListItem title={item.title}/>
))}
</div>
)
}
function FetchData() {
return fetch(baseUrl + '/todos')
.then(items => items.json())
}
function App() {
const [todos, setTodos] = useState([] );
function CheckKey(e) {
if (e.key === 'Enter') {
e.preventDefault();
SubmitTodo()
}
}
function SubmitTodo() {
let element = document.getElementById('name-5')
const { value } = element
if (value === '') {
return
}
element.value = ''
fetch(baseUrl+ '/todos', {method: "POST", body: JSON.stringify({title: value}), headers: {'Content-Type': 'application/json'}} )
setTodos([{title: value}].concat(todos))
}
useEffect(() => {
async function loadTodos() {
const result = await FetchData()
setTodos(result.todos)
}
loadTodos()
}, []);
return (
<div className="banner-container">
<div className="widget-container-2 outline">
<div className="widget-body-2">
<div className="w-form">
<form id="email-form" name="email-form" data-name="Email Form" className="form">
<div className="task-title-group"><img src={LogoIcon} loading="lazy" alt="" className="task-logo"/>
<div className="task-overline">Powered by <a href="https://render.com">Render</a></div>
<h1 className="task-title">Todo List</h1>
</div>
<div className="task-container">
<div className="task-wrapper">
<input type="text" className="task-input w-input" onKeyPress={CheckKey} maxLength="256" name="name-5" data-name="Name 5" placeholder="enter a task description ..." id="name-5"/>
</div>
<a className="task-button w-inline-block" onClick={SubmitTodo}>
<div>Add Task</div>
</a>
</div>
<TodoList items={todos}/>
</form>
<div className="w-form-done">
<div>Thank you! Your submission has been received!</div>
</div>
<div className="w-form-fail">
<div>Oops! Something went wrong while submitting the form.</div>
</div>
</div>
</div>
</div>
</div>
)
}
export default App;