body.tsx 5.15 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 { Pagination} from "../Pages/picpagination";
7
8
9
10
11
12
13

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

export default function Body() {
  let limit = 15;
  const [searchParams, setSearchParams] = useSearchParams(initSearchParams);
  const [page, setPage] = useState(1);
백승민's avatar
백승민 committed
14
15
16
17
  const [style, setStyle] = useState("");
  const firstRightClick = useRef(true);
  const firstLeftClick = useRef(true);
  const slide = useRef(1);
백승민's avatar
백승민 committed
18
19
20
  const offset1 = (page - 1) * limit;
  const offset2 = page * limit;
  const offset3 = (page + 1) * limit;
21

백승민's avatar
백승민 committed
22
23
  let getPics = getPicure();
  
24
25
  useEffect(() => {
    console.log(searchParams.get("theme"), searchParams.get("city"));
백승민's avatar
백승민 committed
26
    setSearchParams(searchParams)
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,
    });
  };
백승민's avatar
백승민 committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  const leftClick = () => {
    if (firstLeftClick.current) {
      firstLeftClick.current = false;
      firstRightClick.current = true;
    } else {
      setPage(page - 1)
    }
    slide.current -= 1;
    setStyle("-translate-x-full animate-slidetoright");
  };
  
  const rightClick = () => {
    if (firstRightClick.current) {
      firstLeftClick.current = true;
      firstRightClick.current = false;
    } else {
      setPage(page + 1)
    }
    slide.current += 1;
    setStyle("animate-slidetoleft");
  };
  console.log(`page: ${slide.current}`);
  console.log(`style: ${style}`);
  
69
70
  let themechange = searchParams.get("theme");
  let citylistchange = searchParams.get("city");
백승민's avatar
백승민 committed
71
  
72
73
74
75
76
77
  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
78
79
      );
    });
80

백승민's avatar
백승민 committed
81
82
83
    const numPages = Math.ceil(Idpics.length / 15);
    
    return (
84
85
86
    <div className="flex flex-col px-1 py-1">
      <Theme handleClick={themeHandleClick} />
      <div className="flex flex-col md:flex-row py-10 ">
백승민's avatar
백승민 committed
87
88
89
        <div className="w-50">
          <Citylist handleClick={cityHandleClick} />
        </div>
백승민's avatar
백승민 committed
90
        <div className="flex flex-col  overflow-hidden">
백승민's avatar
백승민 committed
91
92
93
94
95
96
97
98
99
100
101
          <div>
            <button onClick={leftClick} disabled={slide.current === 1}>
              &lt;{slide.current}
            </button>
            <button onClick={rightClick} disabled={slide.current === numPages}>
              &gt;
            </button>
          </div>
          <div className={`md:mr-10 md:basis-4/5 flex flex-row relative w-full `}>  
            <div key={Math.random()} className="min-w-full">
              <div  className={`inline-grid grid-cols-5 ${style}`}>
백승민's avatar
백승민 committed
102
103
                {Idpics.slice(offset1, offset1 + limit).map((pic, index: number) => (
                  <div
백승민's avatar
백승민 committed
104
                    className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}
백승민's avatar
백승민 committed
105
106
107
108
109
110
111
                    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
112
113
114
                ))}
              </div>
            </div>
백승민's avatar
백승민 committed
115
116
            <div key={Math.random()} className="min-w-full">
              <div className={`inline-grid grid-cols-5 ${style}`}>
백승민's avatar
백승민 committed
117
118
119
                {Idpics.slice(offset2, offset2 + limit).map((pic, index: number) => (

                  <div
백승민's avatar
백승민 committed
120
                    className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}
백승민's avatar
백승민 committed
121
122
123
124
125
126
127
128
                    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
129
130
131
                ))}
              </div>
            </div>
백승민's avatar
백승민 committed
132
133
            <div key={Math.random()} className="min-w-full">
              <div className={`inline-grid grid-cols-5 ${style}`}>
백승민's avatar
백승민 committed
134
135
136
                {Idpics.slice(offset3, offset3 + limit).map((pic, index: number) => (

                  <div
백승민's avatar
백승민 committed
137
                    className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}
백승민's avatar
백승민 committed
138
139
140
141
142
143
144
145
                    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
146
                ))}
백승민's avatar
백승민 committed
147
              </div>
148
            </div>
백승민's avatar
백승민 committed
149
          </div>
150
151
        </div>
      </div>
백승민's avatar
백승민 committed
152

153
154
155
156
157
      <Outlet />
    </div>
    // Body Page
  );
}
백승민's avatar
백승민 committed
158