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

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

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

14
15
16
17
  let getPics = getPicure();

  useEffect(() => {
    console.log(searchParams.get("theme"), searchParams.get("city"));
18
    setSearchParams(searchParams);
19
20
21
22
23
24
25
26
  }, []);

  const themeHandleClick = (event: MouseEvent<HTMLButtonElement>) => {
    console.log(`theme id= ${event.currentTarget.id}`);
    setSearchParams({
      ...Object.fromEntries(searchParams),
      theme: event.currentTarget.id,
    });
백승민's avatar
백승민 committed
27
    // setPage(1)
28
29
30
31
32
33
34
35
36
37
  };

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

38
  //picture insert
39
40
41
42
43
44
45
46
47
48
49
50
  let themechange = searchParams.get("theme");
  let citylistchange = searchParams.get("city");

  const Idpics = getPics.filter((p) => {
    return (
      (p.themeid == themechange && p.cityid == citylistchange) ||
      (p.themeid == themechange && citylistchange == "") ||
      (themechange == "" && p.cityid == citylistchange) ||
      (themechange == "" && citylistchange == "")
    );
  });

백승민's avatar
백승민 committed
51
  const numPages = Math.ceil(Idpics.length / 15);
백승민's avatar
백승민 committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

  const slides = []
  for (let i = 0; i < numPages; i++) {
    const k = [
          Idpics.slice(i * limit, i * limit + limit).map((pic, index: number) => (
            <div
              className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}
              key={index}>
              <img
                src={pic.url}
                className="w-full h-10 md:h-20 object-center"
              />
              <p className="text-center text-xs">{pic.name}</p>
            </div>
          ))]
    slides.push(k);
  }
백승민's avatar
백승민 committed
69

70
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
71
    <div className="flex flex-col">
72
      <Theme handleClick={themeHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
73
      <div className="flex flex-col md:flex-row py-5 ">
백승민's avatar
백승민 committed
74
75
76
        <div className="w-50">
          <Citylist handleClick={cityHandleClick} />
        </div>
백승민's avatar
백승민 committed
77
78
79
        <div className="flex flex-col">
          <MySlide key={Math.random()}
            slides={slides}
80
          />
81
82
83
84
85
86
87
        </div>
      </div>
      <Outlet />
    </div>
    // Body Page
  );
}