-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
88 lines (76 loc) · 2.16 KB
/
App.jsx
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
import React, { useState, useEffect } from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { PostList } from "./components/PostList";
import { PostForm } from "./components/PostForm";
import { MySelector } from "./components/MySelector";
import { auth, login, logout } from "./firebase";
function App() {
const [user, isLoginUser] = useAuthState(auth);
const [isLoading, setIsLoading] = useState(false);
const [list, setList] = useState([
{ id: 1, title: "Alex", body: "Title1" },
{ id: 2, title: "Vasil", body: "Title2" },
{ id: 3, title: "Masha", body: "Title3" },
]);
const deleteById = (id) => {
console.log(id);
const newList = list.filter(function (item) {
return item.id !== id;
});
setList(newList);
};
const getUsers = function () {
setIsLoading(true);
return fetch("https://jsonplaceholder.typicode.com/posts/?userId=1");
};
const fetchUsers = async () => {
try {
const response = await getUsers();
setIsLoading(false);
if (!response.ok) {
throw new Error(`status: ${response.status}`);
}
const data = await response.json();
console.log(data);
setList(data);
} catch (error) {
console.log("Opps!", error);
}
};
useEffect(() => {
fetchUsers();
}, []);
const addNewPost = (post) => {
setList([...list, post]);
};
const handleSelector = (e) => {
console.log(e.target.value);
};
return !isLoginUser ? (
<div className="App">
{user ? (
<button onClick={logout}>Выйти</button>
) : (
<button onClick={login}>авторизоваться</button>
)}
<MySelector
onChange={handleSelector}
options={[
{ title: "Второй", value: "second" },
{ title: "Третий", value: "third" },
]}
/>
<PostForm addNewPost={addNewPost} />
{isLoading ? (
<div>идёт загрузка</div>
) : list.length ? (
<PostList deleteById={deleteById} list={list} />
) : (
<div>Ничего нету</div>
)}
</div>
) : (
<div>loading...</div>
);
}
export default App;