Search.js 7.33 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
Kim, Chaerin's avatar
Kim, Chaerin committed
2
import { Link, Redirect } from 'react-router-dom';
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
3
import ohuh from '../ohuh-sm.PNG';
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
4
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button } from 'react-bootstrap';
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
5
import Paginations from '../Components/Paginations';
baesangjune's avatar
.    
baesangjune committed
6
import axios from 'axios';
7
import queryString from 'query-string' // react route dom 에 있다. 확인해보기 
Kim, Chaerin's avatar
Kim, Chaerin committed
8
9

function Search(props) {
10
    const endPage = 10;
Kim, Chaerin's avatar
Kim, Chaerin committed
11
    const [state, setState] = useState(false);
12
    const [index, setIndex] = useState(1);
13
    const [showSet, setShowSet] = useState([false, false, false, false]);
Kim, Chaerin's avatar
Kim, Chaerin committed
14
    const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
Kim, Chaerin's avatar
Kim, Chaerin committed
15
    const [pagePlace, setPagePlace] = useState([]);
baesangjune's avatar
.    
baesangjune committed
16
    const [association, setAssociation] = useState([])
baesangjune's avatar
.    
baesangjune committed
17

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
18
    const getAssociation = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
19
        axios.get(`/api/search/association?keyword=${search}`)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
20
21
22
23
24
25
26
            .then(res => {
                console.log("Associations = ", res.data)
                setAssociation(res.data)
            })
            .catch(err => {
                console.log("search.associations 에러 발생", err)
            })
baesangjune's avatar
.    
baesangjune committed
27
28
    }

baesangjune's avatar
.    
baesangjune committed
29

30
31

    useEffect(() => {
baesangjune's avatar
.    
baesangjune committed
32
        getAssociation()
baesangjune's avatar
baesangjune committed
33
    }, []);
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
34

baesangjune's avatar
baesangjune committed
35
36
    useEffect(() => {
        setPagePlace(paginate(association, index, association.length))
37
    }, [association]);
baesangjune's avatar
.    
baesangjune committed
38

baesangjune's avatar
baesangjune committed
39
40
    useEffect(() => {
        getAssociation()
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
41
        if (state) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
42
43
44
45
46
            // window.location.reload()
            // return <Redirect to={{
            //         pathname: `/search?keyword=${search}`,
            //         state: { id: search },
            //     }} />;
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
47
            props.history.push('/search?keyword=' + search)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
48
49
            // setState(false)
            // console.log("search야", search)
50
51
52
        } window.addEventListener("scroll", infiniteScroll);
        return () => { window.removeEventListener("scroll", infiniteScroll); }
    }, []);
baesangjune's avatar
baesangjune committed
53

54
55
56
57
58
59
60
61
    const infiniteScroll = () => {
        const { documentElement, body } = document;
        const scrollHeight = Math.max(documentElement.scrollHeight, body.scrollHeight);
        const scrollTop = Math.max(documentElement.scrollTop, body.scrollTop);
        const clientHeight = documentElement.clientHeight;
        if (scrollTop + clientHeight >= scrollHeight) {
            // getReview();
            console.log("더불러")
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
62
        }
63
64
        console.log(scrollHeight, scrollTop, clientHeight)
    }
baesangjune's avatar
.    
baesangjune committed
65
66


67
    const places = [{
68
        name: "한라산",
69
        address: "제주 서귀포시 토평동 산15-1",
baesangjune's avatar
.    
baesangjune committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        img: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/KOCIS_Halla_Mountain_in_Jeju-do_%286387785543%29.jpg/269px-KOCIS_Halla_Mountain_in_Jeju-do_%286387785543%29.jpg?size=200x200",
    }, {
        name: "성산일출봉(sungsan)",
        address: "제주 서귀포시 성산읍 성산리 1",
        img: "https://www.jeju.go.kr/pub/site/geopark/images/sub/sub03/02%EC%A7%80%EC%A7%88%EB%A7%88%EC%9D%84%EC%9D%B4%EC%95%BC%EA%B8%B0/%EC%A7%80%EC%A7%88%EB%A7%88%EC%9D%84/%EC%A7%80%EC%A7%88%EB%A7%88%EC%9D%84_%EC%84%B1%EC%82%B0%EC%9D%BC%EC%B6%9C%EB%B4%89/1412402261.jpg?400/400",
    }, {
        name: "해녀의 집(haenyeo)",
        address: "제주 서귀포시 성산읍 한도로 141-13지번오조리 3 오조해녀의집",
        img: "https://mblogthumb-phinf.pstatic.net/MjAxNjExMTdfMTc0/MDAxNDc5MzU3ODU0ODQy.KZYXCjzsXT3rCsE4HXBfxyCg2buvluBvN_7NxVp7BSwg.loJc89d8JjGXdNCn-4yMd7aMWPjfrZn21TI9Hyzemkog.JPEG.icocam11/20161010_100205.jpg?type=w800",
    }, {
        name: "오설록 티 뮤지엄(osulloc)",
        address: "제주 서귀포시 안덕면 신화역사로 15 오설록지번서광리 1235-1 오설록",
        img: "https://cdnweb01.wikitree.co.kr/webdata/editor/202007/01/img_20200701143323_2ced7627.webp",
    }, {
        name: "오설록 티 뮤지엄(osulloc)",
        address: "제주 서귀포시 안덕면 신화역사로 15 오설록지번서광리 1235-1 오설록",
        img: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/KOCIS_Halla_Mountain_in_Jeju-do_%286387785543%29.jpg/269px-KOCIS_Halla_Mountain_in_Jeju-do_%286387785543%29.jpg",
87
88
    }]

Kim, Chaerin's avatar
Kim, Chaerin committed
89
    if (state !== false) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
90
91
92
93
94
95
96
97
        // console.log(queryString.parse(props.location.search))
        //  = {keyword:search}
        // return <Redirect to={{
        //     pathname: `/search?keyword=${search}`,
        //     state: { id: search },
        // }} />;
        // history.pushState(null, null, "?"+queryParams.toString());
        // return <Redirect to={`/search?keyword=${search}`} />;
98
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
99

100
101
102
103
104
    const handlePage = (num) => {
        setIndex(num);
    }


Kim, Chaerin's avatar
Kim, Chaerin committed
105
106
107
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
108

Kim, Chaerin's avatar
Kim, Chaerin committed
109
    const handleSubmit = (e) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
110
        e.preventDefault()
Kim, Chaerin's avatar
Kim, Chaerin committed
111
        setState(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
112
    }
113

Kim, Chaerin's avatar
Kim, Chaerin committed
114
    function paginate(items, pageNumber, itemNumber) {
115
        const page = [];
Kim, Chaerin's avatar
Kim, Chaerin committed
116
117
        const startIndex = (pageNumber - 1) * itemNumber
        for (var i = 0; i < itemNumber; i++) {
118
119
120
121
122
            page.push(items[(startIndex + i)])
        }
        return page
    }

baesangjune's avatar
.    
baesangjune committed
123
    let time = new Date()
124

Kim, Chaerin's avatar
Kim, Chaerin committed
125
    return (
126
        <Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
127
            <Link to="/path" className="d-flex justify-content-center"><Image src={ohuh} /></Link>
128
129
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
130
131
132
133
134
135
136
137
138
139
140
141
                    <InputGroup size="lg">
                        <FormControl
                            placeholder="검색어를 입력하세요."
                            value={search}
                            aria-label="Large"
                            aria-describedby="inputGroup-sizing-sm"
                            onChange={handleChange}
                        />
                        <InputGroup.Append>
                            <Button type="submit" variant="outline-secondary" >검색</Button>
                        </InputGroup.Append>
                    </InputGroup>
142
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
143
            </Row>
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
144
            {time.toLocaleString()}
145
146
147
            <Row className="d-flex flex-wrap">
                {pagePlace.map((place, index) => {
                    return (
148
149
150
151
                        <Col key={index} md={6} >
                            <Card align="center" border="info" style={{ margin: "3%" }}>
                                <Card.Title style={{ margin: "3%", fontSize: '200%', fontWeight: 'bold' }} >{place.name}</Card.Title>
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
Kim, Chaerin's avatar
Kim, Chaerin committed
152
                                <Card.Body>
153
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
154
                                        {place.address} </Card.Text>
Kim, Chaerin's avatar
Kim, Chaerin committed
155
156
157
                                    <Link to={`/place?id=${index}&place=${place.name}`} >
                                        <Button variant="primary"> {place.name} 자세히 살펴보기</Button>
                                    </Link>
158
159
160
                                </Card.Body>
                            </Card>
                        </Col>
161
162
163
                    )
                })}
            </Row>
164
            <Row className="mt-2 d-flex justify-content-center">
165
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
166
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
167
        </Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
168
169
170
    );
}

171
export default Search;