body.tsx 3.01 KB
Newer Older
백승민's avatar
백승민 committed
1
import React, { useEffect, MouseEvent, useState, useRef } from "react";
2
3
4
import { Outlet, useSearchParams } from "react-router-dom";
import Theme from "./theme";
import Citylist from "../Pages/citylist";
백승민's avatar
백승민 committed
5
import { MySlide } from "../Pages/myslide";
백승민's avatar
백승민 committed
6
7
import { mainimgApi } from "../apis";
import { MainimgType } from "../types";
8
9
10
11
12

const initSearchParams = { theme: "", city: "" };

export default function Body() {
  const [searchParams, setSearchParams] = useSearchParams(initSearchParams);
백승민's avatar
백승민 committed
13

백승민's avatar
백승민 committed
14
15
16
17
  const [getimgs, setGetimgs] = useState<MainimgType[]>([]);

  async function imgsData() {
    const imgs = await mainimgApi.getmainimg();
Kim, MinGyu's avatar
Kim, MinGyu committed
18
19
    setGetimgs(imgs);
  }
백승민's avatar
백승민 committed
20
21
22
  useEffect(() => {
    imgsData();
  }, []);
23
24
25

  useEffect(() => {
    console.log(searchParams.get("theme"), searchParams.get("city"));
26
    setSearchParams(searchParams);
27
28
29
30
31
32
33
34
  }, []);

  const themeHandleClick = (event: MouseEvent<HTMLButtonElement>) => {
    console.log(`theme id= ${event.currentTarget.id}`);
    setSearchParams({
      ...Object.fromEntries(searchParams),
      theme: event.currentTarget.id,
    });
백승민's avatar
백승민 committed
35
    // setPage(1)
36
37
38
39
40
41
42
43
44
45
  };

  const cityHandleClick = (event: MouseEvent<HTMLButtonElement>) => {
    console.log(`city id= ${event.currentTarget.id}`);
    setSearchParams({
      ...Object.fromEntries(searchParams),
      city: event.currentTarget.id,
    });
  };

46
  //picture insert
47
48
49
  let themechange = searchParams.get("theme");
  let citylistchange = searchParams.get("city");

백승민's avatar
백승민 committed
50
  const Idpics = getimgs.filter((p) => {
51
    return (
백승민's avatar
백승민 committed
52
53
54
      (p.theme == themechange && p.city == citylistchange) ||
      (p.theme == themechange && citylistchange == "") ||
      (themechange == "" && p.city == citylistchange) ||
55
56
57
58
      (themechange == "" && citylistchange == "")
    );
  });

백승민's avatar
백승민 committed
59
60
  let limit = 4;
  const numPages = Math.ceil(Idpics.length / limit);
백승민's avatar
백승민 committed
61

Kim, MinGyu's avatar
Kim, MinGyu committed
62
  const slides = [];
백승민's avatar
백승민 committed
63
64
  for (let i = 0; i < numPages; i++) {
    const k = [
Kim, MinGyu's avatar
Kim, MinGyu committed
65
66
67
68
69
70
71
72
73
74
75
76
77
      Idpics.slice(i * limit, i * limit + limit).map(
        (picture, index: number) => (
          <div
            className={`m-1 shrink-0 rounded shadow-md h-45 relative overflow-hidden`}
            key={index}
          >
            <img
              src={"http://localhost:3000/images/" + picture.pic.newfilename}
              className="w-full h-40 object-cover hover:scale-110 transition duration-0 hover:duration-500"
            />
            <div className="bg-transparent text-neutral-50 text-xs rounded-full absolute bottom-0 ml-1 mb-1 hover:scale-110 transition duration-0 hover:duration-500">
              <span>{picture.title}</span>
            </div>
백승민's avatar
백승민 committed
78
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
79
80
81
        )
      ),
    ];
백승민's avatar
백승민 committed
82
83
    slides.push(k);
  }
백승민's avatar
백승민 committed
84

85
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
86
    <div className="flex flex-col">
87
      <Theme handleClick={themeHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
88
      <div className="flex flex-col md:flex-row py-5 ">
Kim, MinGyu's avatar
Kim, MinGyu committed
89
        <div className="md:w-1/5 lg:w-1/6">
백승민's avatar
백승민 committed
90
91
          <Citylist handleClick={cityHandleClick} />
        </div>
백승민's avatar
백승민 committed
92
        <div className="flex flex-col">
Kim, MinGyu's avatar
Kim, MinGyu committed
93
          <MySlide key={Math.random()} slides={slides} />
94
95
96
97
98
99
100
        </div>
      </div>
      <Outlet />
    </div>
    // Body Page
  );
}