body.tsx 5.26 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
  const [page, setPage] = useState(1);
백승민's avatar
백승민 committed
15
  const [style, setStyle] = useState("");
백승민's avatar
백승민 committed
16

백승민's avatar
백승민 committed
17
18
19
  const offset1 = (page - 1) * limit;
  const offset2 = page * limit;
  const offset3 = (page + 1) * limit;
20
21
22
23
24

  let getPics = getPicure();

  useEffect(() => {
    console.log(searchParams.get("theme"), searchParams.get("city"));
25
    setSearchParams(searchParams);
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  }, []);

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

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

44
  //picture insert
45
46
47
48
49
50
51
52
53
54
55
56
  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
57
  const numPages = Math.ceil(Idpics.length / 15);
58
  console.log(page);
백승민's avatar
백승민 committed
59
60
  // const slides = []
  // for (let i = 0; i < numPages + 1; i++) {
61
  //   const k =[
백승민's avatar
백승민 committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  //   <div key={Math.random()} className="min-w-full">
  //     <div className={`inline-grid grid-cols-5 ${style}`}>
  //       {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>
  //       ))}
  //     </div>
  //   </div>]
  //   slides.push(k);
  // }
백승민's avatar
백승민 committed
79

80
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
81
    <div className="flex flex-col">
82
      <Theme handleClick={themeHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
83
      <div className="flex flex-col md:flex-row py-5 ">
백승민's avatar
백승민 committed
84
85
86
        <div className="w-50">
          <Citylist handleClick={cityHandleClick} />
        </div>
백승민's avatar
백승민 committed
87
        <div className="flex flex-col  overflow-hidden">
88
89
90
91
92
93
94
95
96
97
          <MySlide
            total={Idpics.length}
            page={page}
            setPage={setPage}
            style={style}
            setStyle={setStyle}
          />
          <div
            className={`md:mr-10 md:basis-4/5 flex flex-row relative w-full `}
          >
백승민's avatar
백승민 committed
98
            <div key={Math.random()} className="min-w-full">
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
              <div className={`inline-grid grid-cols-5 ${style}`}>
                {Idpics.slice(offset1, offset1 + 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>
                  )
                )}
백승민's avatar
백승민 committed
114
115
              </div>
            </div>
백승민's avatar
백승민 committed
116
117
            <div key={Math.random()} className="min-w-full">
              <div className={`inline-grid grid-cols-5 ${style}`}>
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                {Idpics.slice(offset2, offset2 + 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>
                  )
                )}
백승민's avatar
백승민 committed
132
133
              </div>
            </div>
백승민's avatar
백승민 committed
134
135
            <div key={Math.random()} className="min-w-full">
              <div className={`inline-grid grid-cols-5 ${style}`}>
136
137
138
139
140
141
142
143
144
145
146
147
148
149
                {Idpics.slice(offset3, offset3 + 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>
                  )
                )}
백승민's avatar
백승민 committed
150
              </div>
151
            </div>
백승민's avatar
백승민 committed
152
          </div>
153
154
        </div>
      </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
155

156
157
158
159
160
      <Outlet />
    </div>
    // Body Page
  );
}