-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewTask.js
More file actions
65 lines (58 loc) · 1.88 KB
/
NewTask.js
File metadata and controls
65 lines (58 loc) · 1.88 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
import { useState } from "react";
import Section from "../UI/Section";
import TaskForm from "./TaskForm";
import useHttp from "../../hooks/use-http";
const NewTask = (props) => {
const { isLoading, error, sendRequest: sendTaskRequest } = useHttp();
const createTask = (taskText, taskData) => {
const generatedId = taskData.name; // firebase-specific => "name" contains generated id
const createdTask = { id: generatedId, text: taskText };
props.onAddTask(createdTask);
};
const enterTaskHandler = async (taskText) => {
sendTaskRequest(
{
url: "https://react-tasks-731ef-default-rtdb.firebaseio.com/tasks.json",
method: "POST",
headers: { "Content-Type": "application/json" },
body: { text: taskText },
},
createTask.bind(null, taskText)
);
// setIsLoading(true);
// setError(null);
// try {
// const response = await fetch(
// "https://react-tasks-731ef-default-rtdb.firebaseio.com/tasks.json",
// {
// method: "POST",
// body: JSON.stringify({ text: taskText }),
// headers: {
// "Content-Type": "application/json",
// },
// }
// );
//
// if (!response.ok) {
// throw new Error("Request failed!");
// }
//
// const data = await response.json();
//
// const generatedId = data.name; // firebase-specific => "name" contains generated id
// const createdTask = { id: generatedId, text: taskText };
//
// props.onAddTask(createdTask);
// } catch (err) {
// setError(err.message || "Something went wrong!");
// }
// setIsLoading(false);
};
return (
<Section>
<TaskForm onEnterTask={enterTaskHandler} loading={isLoading} />
{error && <p>{error}</p>}
</Section>
);
};
export default NewTask;