Commit f9ed2d93 authored by 백승민's avatar 백승민
Browse files

pic pagination

parent 58a76189
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
"webpack-dev-server": "^4.9.2" "webpack-dev-server": "^4.9.2"
}, },
"dependencies": { "dependencies": {
"axios": "^0.27.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-router-dom": "^6.3.0" "react-router-dom": "^6.3.0"
......
import React from "react";
type num = {
total: number,
page: number,
setPage: Function
}
export default function Pagination ({total, page, setPage} : num) {
const numPages = Math.ceil(total / 15);
return (
<div>
<button onClick={() => setPage(page - 1)} disabled={page === 1}>
&lt;
</button>
{Array(numPages)
.fill(1)
.map((_, i) => (
<button key={i + 1} onClick={() => setPage(i + 1)}>
{i + 1}
</button>
))}
<button onClick={() => setPage(page + 1)} disabled={page === numPages}>
&gt;
</button>
</div>
);
};
import React, { useEffect, MouseEvent } from "react"; import React, { useEffect, MouseEvent,useState } from "react";
import { Outlet, useSearchParams } from "react-router-dom"; import { Outlet, useSearchParams } from "react-router-dom";
import Theme from "./theme"; import Theme from "./theme";
import Citylist from "./citylist"; import Citylist from "./citylist";
import {getPicure} from "./pic"; import {getPicure} from "./pic";
import Pagination from "./picpagination"
import { LibManifestPlugin } from "webpack";
const initSearchParams = { "theme": "", "city": "" } const initSearchParams = { "theme": "", "city": "" }
export default function Body() { export default function Body() {
let limit = 15;
const [searchParams, setSearchParams] = useSearchParams(initSearchParams);
const [page, setPage] = useState(1);
const offset = (page-1) *limit;
const [searchParams, setSearchParams] = useSearchParams(initSearchParams)
let getPics = getPicure(); let getPics = getPicure();
useEffect(() => { useEffect(() => {
...@@ -32,10 +38,15 @@ export default function Body() { ...@@ -32,10 +38,15 @@ export default function Body() {
}) })
} }
let themechange = searchParams.get('theme');
let citylistchange = searchParams.get('city');
const Idpics = getPics.filter(p => { const Idpics = getPics.filter(p => {
return (p.themeid == searchParams.get('theme') && p.cityid == searchParams.get('city'))||(p.themeid == searchParams.get('theme') && searchParams.get('city') == "") return (p.themeid == themechange && p.cityid == citylistchange)||
||(searchParams.get('theme') == "" && p.cityid == searchParams.get('city'))||(searchParams.get('theme') == "" && searchParams.get('city') == "") (p.themeid == themechange && citylistchange == "")||
(themechange == "" && p.cityid == citylistchange)||
(themechange == "" && citylistchange == "")
}) })
...@@ -44,14 +55,16 @@ export default function Body() { ...@@ -44,14 +55,16 @@ export default function Body() {
<Theme handleClick={themeHandleClick} /> <Theme handleClick={themeHandleClick} />
<div className="flex flex-col md:flex-row py-10 "> <div className="flex flex-col md:flex-row py-10 ">
<Citylist handleClick={cityHandleClick} /> <Citylist handleClick={cityHandleClick} />
<div className="flex md:mr-10 md:basis-4/5 grid grid-rows-3 grid-cols-5"> <div className="flex-row md:mr-10 md:basis-4/5 grid grid-rows-3 grid-cols-5">
{Idpics.map((pic, index: number) => ( {Idpics.slice(offset, offset + limit).map((pic, index: number) => (
<div className="m-1 shrink-0 bg-gray-200 rounded overflow-hidden shadow-md" key={index}> <div className="m-1 shrink-0 bg-gray-200 rounded overflow-hidden shadow-md" key={index}>
<img src={pic.url} className="w-full h-10 md:h-20 object-cover" /> <img src={pic.url} className="w-full h-10 md:h-20 object-center" />
<p className="text-center text-xs">{pic.name}</p> <p className="text-center text-xs">{pic.name}</p>
</div> </div>
))} ))}
<Pagination total={Idpics.length} page = {page} setPage ={setPage}/>
</div> </div>
</div> </div>
<Outlet /> <Outlet />
</div> </div>
......
...@@ -6,5 +6,13 @@ const common = require("./webpack.common.js"); ...@@ -6,5 +6,13 @@ const common = require("./webpack.common.js");
module.exports = merge(common, { module.exports = merge(common, {
mode: "development", mode: "development",
devtool: "inline-source-map", devtool: "inline-source-map",
devServer: {historyApiFallback: true }, devServer: {
proxy: [
{
context : ["/api"],
target : "http://localhost:3000",
changeOrigin: true,
},
],
historyApiFallback: true },
}); });
...@@ -9,7 +9,6 @@ app.use(express.urlencoded({ extended: true })); ...@@ -9,7 +9,6 @@ app.use(express.urlencoded({ extended: true }));
app.use(cookieParser()); app.use(cookieParser());
app.use("/api", router); app.use("/api", router);
app.use((err: any, req: Request, res: Response, next: NextFunction) => { app.use((err: any, req: Request, res: Response, next: NextFunction) => {
console.log("익스프레스 에러: ", err); console.log("익스프레스 에러: ", err);
res.status(err.statusCode || 500).send(err.message || "서버 에러"); res.status(err.statusCode || 500).send(err.message || "서버 에러");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment