body.tsx 3.07 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

Kim, MinGyu's avatar
Kim, MinGyu committed
24
25
26
27
  // useEffect(() => {
  //   console.log(searchParams.get("theme"), searchParams.get("city"));
  //   setSearchParams(searchParams);
  // }, []);
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
      Idpics.slice(i * limit, i * limit + limit).map(
        (picture, index: number) => (
          <div
백승민's avatar
백승민 committed
68
            className={`m-1 shrink-0 rounded shadow-md h-50 relative overflow-hidden`}
Kim, MinGyu's avatar
Kim, MinGyu committed
69
70
71
            key={index}
          >
            <img
72
73
74
              src={
                "http://localhost:3000/images/" + picture.fileInfo.newfilename
              }
Kim, MinGyu's avatar
Kim, MinGyu committed
75
              className="w-full h-44 object-cover hover:scale-110 transition duration-0 hover:duration-500"
Kim, MinGyu's avatar
Kim, MinGyu committed
76
            />
백승민's avatar
백승민 committed
77
            <div className="bg-transparent text-neutral-50 text-xs md:text-lg rounded-full absolute bottom-0 ml-1 mb-1 hover:scale-110 transition duration-0 hover:duration-500">
Kim, MinGyu's avatar
Kim, MinGyu committed
78
79
              <span>{picture.title}</span>
            </div>
백승민's avatar
백승민 committed
80
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
81
82
83
        )
      ),
    ];
백승민's avatar
백승민 committed
84
85
    slides.push(k);
  }
백승민's avatar
백승민 committed
86

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