forked from koh0074/corner4
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBestseller.js
More file actions
168 lines (150 loc) · 5.23 KB
/
Copy pathBestseller.js
File metadata and controls
168 lines (150 loc) · 5.23 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { useState, useEffect } from "react";
import { Link, useNavigate } from "react-router-dom";
import "../styles/Bestseller.css";
import bookIcon from "../assets/bookicon.png";
import lamp from "../assets/lamp.png";
import logo from "../assets/logo.png";
import axios from "axios";
const Bestseller = () => {
const [books, setBooks] = useState([]);
const [searchTerm, setSearchTerm] = useState("");
const [allBooks, setAllBooks] = useState([]);
const navigate = useNavigate();
const [currentPage, setCurrentPage] = useState(1);
// API 호출하여 베스트셀러 목록 가져오기
const fetchBestseller = async () => {
try {
const response = await axios.get('/book/bestseller?itemsPerPage=25', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0'
}
});
console.log('받아온 데이터:', response.data); // 데이터 확인
const items = response.data.item || response.data;
setAllBooks(items); // 전체 25개 저장
setBooks(items.slice(0, 5)); // 첫 페이지 1~5위 책만 표시
} catch (error) {
console.error("베스트셀러 목록 오류", error);
}
};
// 페이지 로드 시 API 호출
useEffect(() => {
fetchBestseller();
}, []);
const handlePageChange = (page) => {
setCurrentPage(page);
const startIndex = (page - 1) * 5;
setBooks(allBooks.slice(startIndex, startIndex + 5));
};
// 검색어가 변경될 때마다 자동으로 필터링
useEffect(() => {
if (searchTerm.trim() === "") {
const startIndex = (currentPage - 1) * 5;
setBooks(allBooks.slice(startIndex, startIndex + 5));
} else {
const filteredBooks = allBooks.filter((book) =>
book.title.toLowerCase().includes(searchTerm.toLowerCase())
);
setBooks(filteredBooks);
}
}, [searchTerm, allBooks, currentPage]);
return (
<div className="main-container">
<header className="header">
<div className="img-group">
<img src={lamp} className="lamp" alt="lamp" />
<img src={lamp} className="lamp" alt="lamp" />
<img src={lamp} className="lamp" alt="lamp" />
</div>
<div className="nav-group">
<div className="nav-item">
<Link to="/bestseller">베스트셀러</Link>
<img src={bookIcon} className="book-icon" alt="book icon" />
</div>
<div className="nav-item">
<Link to="/test">북루미 테스트</Link>
<div className="underline"></div>
</div>
<div className="nav-item">
<Link to="/community">북작북작</Link>
<div className="underline"></div>
</div>
<div className="nav-item">
<Link to="/myDrawer">나의 서랍</Link>
<div className="underline"></div>
</div>
</div>
</header>
<div className="bestseller-section">
<div className="bestseller-header">
<Link to="/">
<img src={logo} className="logo" alt="로고" />
</Link>
<h2>이달의 베스트셀러</h2>
<div className="search-bar">
<input
type="text"
placeholder="🔍 검색"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</div>
</div>
<p className="weather">🔔오늘의 날씨는 : 책📚 읽기에 완벽한 맑은 날☀️</p>
{/* 날씨 알림 */}
<div className="book-list">
{books.length > 0 ? (
books.map((book, index) => (
<div key={index} className="book-item">
<span className="rank">{(currentPage - 1) * 5 + index + 1}</span>
<img
src={book.cover}
alt={book.title}
className="book-cover"
onClick={() => navigate(`/book/${encodeURIComponent(book.title.replace(/[^a-zA-Z0-9 ]/g, ""))}`)}
style={{ cursor: "pointer" }}
/>
<p
className="book-title"
onClick={() => navigate(`/book/${encodeURIComponent(book.title.replace(/[^a-zA-Z0-9 ]/g, ""))}`)}
style={{ cursor: "pointer", fontSize: "15px" }}
>
{book.title.split("-")[0].trim()}
</p>
</div>
))
) : (
<p className="no-results">검색 결과가 없습니다.</p>
)}
</div>
<div className="pagination">
{[1, 2].map((page) => (
<button
key={page}
className={currentPage === page ? "active" : ""}
onClick={() => handlePageChange(page)}
>
{page}
</button>
))}
</div>
</div>
</div>
);
};
export default Bestseller;
/* 데이터 크기에 따라 동적 코딩
<div className="pagination">
{Array.from({ length: Math.ceil(allBooks.length / 5) }, (_, i) => i + 1).map((page) => (
<button
key={page}
className={currentPage === page ? "active" : ""}
onClick={() => handlePageChange(page)}
>
{page}
</button>
))}
</div>
*/