body.tsx 2.96 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
18
19
20
21
22
  const [getimgs, setGetimgs] = useState<MainimgType[]>([]);

  async function imgsData() {
    const imgs = await mainimgApi.getmainimg();
    setGetimgs(imgs)
  };
  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
62
63
64

  const slides = []
  for (let i = 0; i < numPages; i++) {
    const k = [
백승민's avatar
백승민 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>
        </div>
      ))]
백승민's avatar
백승민 committed
78
79
    slides.push(k);
  }
백승민's avatar
백승민 committed
80

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