-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooks.jsx
More file actions
136 lines (125 loc) · 4.65 KB
/
Books.jsx
File metadata and controls
136 lines (125 loc) · 4.65 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { useEffect, useState } from "react";
import Styles from "./style.module.css";
const Books = () => {
const [valueInput, setValueInput] = useState("");
const [books, setBooks] = useState([]);
const [selectedBook, setSelectedBook] = useState(null);
const [isOpenModal, setIsOpenModal] = useState(false);
const getSearchBook = async () => {
try {
const searchBook = await fetch(
`https://openlibrary.org/search.json?q=${valueInput}`
);
let book = await searchBook.json();
setBooks(book.docs);
} catch (error) {
console.log("Ошибка");
}
};
const openModal = (book) => {
setIsOpenModal(true);
setSelectedBook(book);
};
const handleOverlayClick = (e) => {
console.log(e.currentTarget);
if (e.target === e.currentTarget) {
setIsOpenModal(false);
}
};
const handleKeyDown = (event) => {
if (event.key === "Escape") {
setIsOpenModal(false);
}
};
const handleEnter = (event) => {
if (event.key === "Enter") {
getSearchBook();
}
};
useEffect(() => {
if (isOpenModal) {
document.addEventListener("keydown", handleKeyDown);
}
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [isOpenModal]);
return (
<div className={Styles.container}>
<div className={Styles.search_container}>
<input
className={Styles.input_search}
placeholder="Поиск"
type="text"
onChange={(e) => setValueInput(e.target.value)}
value={valueInput}
onKeyDown={handleEnter}
/>
<button
className={Styles.button_search}
onClick={getSearchBook}
>
Найти
</button>
</div>
{books && (
<div className={Styles.books_content}>
{books.map((book) => (
<div
className={Styles.book_content_item}
onClick={() => openModal(book)}
>
<div className={Styles.book_img_contain}>
<img
className={Styles.book_img}
src={`https://covers.openlibrary.org/b/id/${
book.cover_i ? book.cover_i : 14625765
}-L.jpg`}
alt=""
/>
</div>
<span className={Styles.book_title}>
Название: {book.title}
</span>
<span className={Styles.book_author}>
Автор: {book.author_name}
</span>
</div>
))}
</div>
)}
{isOpenModal && selectedBook && (
<div
onClick={handleOverlayClick}
className={`${Styles.modal_window} ${
isOpenModal ? Styles.modal_window_active : ""
}`}
>
<div id="modal_window" className={Styles.modal_content}>
<button
className={Styles.close_modal}
onClick={() => setIsOpenModal(false)}
>
✖
</button>
<img
src={`https://covers.openlibrary.org/b/id/${
selectedBook.cover_i
? selectedBook.cover_i
: 14625765
}-L.jpg`}
alt={selectedBook.title}
className={Styles.modal_img}
/>
<div className={Styles.modal_text}>
<h2>{selectedBook.title}</h2>
<p>{selectedBook.author_name}</p>
<p>{selectedBook.first_publish_year}</p>
</div>
</div>
</div>
)}
</div>
);
};
export default Books;