Search.js 7.52 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
4
import ohuh from '../ohuh-sm.PNG';
import Place from '../Components/Place';
baesangjune's avatar
baesangjune committed
5
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button } from 'react-bootstrap';
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
6
import Paginations from '../Components/Paginations';
baesangjune's avatar
.    
baesangjune committed
7
import axios from 'axios';
baesangjune's avatar
baesangjune committed
8
import queryString from 'query-string'
Kim, Chaerin's avatar
Kim, Chaerin committed
9
10

function Search(props) {
baesangjune's avatar
baesangjune committed
11

Kim, Chaerin's avatar
Kim, Chaerin committed
12
    const [state, setState] = useState(false);
13
    const [index, setIndex] = useState(1);
14
    const [showSet, setShowSet] = useState([false, false, false, false]);
Kim, Chaerin's avatar
Kim, Chaerin committed
15
    const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
16
    const [mobile, setMobile] = useState();
baesangjune's avatar
baesangjune committed
17
18
19
    const [association, setAssociation] = useState([{ name: " ", address: " ", img: " " }])
    const [pagePlace, setPagePlace] = useState([{ name: " ", address: " ", img: " " }, { name: " ", address: " ", img: " " }])
    const [endPage, setEndPage] = useState(1)
baesangjune's avatar
baesangjune committed
20

baesangjune's avatar
하하    
baesangjune committed
21

baesangjune's avatar
.    
baesangjune committed
22

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

baesangjune's avatar
.    
baesangjune committed
34

35
36

    useEffect(() => {
baesangjune's avatar
.    
baesangjune committed
37
        getAssociation()
38
39
40
41
42
        if (window.innerWidth < 960) {
            setMobile(true)
        } else {
            setMobile(false)
        }
baesangjune's avatar
.    
baesangjune committed
43

baesangjune's avatar
baesangjune committed
44
45
46
    }, []);

    useEffect(() => {
baesangjune's avatar
baesangjune committed
47
48
49
50
51
52
53
54
55
        if (association.length < 3) {
            setPagePlace(paginate(association, index, association.length))
        }
        else {
            setPagePlace(paginate(association, index, 4))
        }
        setEndPage(Math.floor((association.length / 4)))

    }, [association, index])
baesangjune's avatar
baesangjune committed
56
57
58

    useEffect(() => {
        getAssociation()
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
59
60
61
62
63
        if (state) {
            props.history.push('/search?keyword=' + search)
            setState(false)
            console.log("search야", search)
        }
baesangjune's avatar
baesangjune committed
64
65

    }, [state])
baesangjune's avatar
.    
baesangjune committed
66
67


68
    const places = [{
69
        name: "한라산",
70
        address: "제주 서귀포시 토평동 산15-1",
baesangjune's avatar
.    
baesangjune committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        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",
88
89
    }]

Kim, Chaerin's avatar
Kim, Chaerin committed
90
    if (state !== false) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
91
92
93
94
95
96
97
98
        // 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}`} />;
99
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
100

101
102
    const handlePage = (num) => {
        setIndex(num);
baesangjune's avatar
baesangjune committed
103
104
        console.log("pagenation num", num)
        console.log(index)
105
106
107
    }


Kim, Chaerin's avatar
Kim, Chaerin committed
108
109
110
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
111

Kim, Chaerin's avatar
Kim, Chaerin committed
112
    const handleSubmit = (e) => {
baesangjune's avatar
baesangjune committed
113
        e.preventDefault()
baesangjune's avatar
baesangjune committed
114
115
        setState(true)
        setIndex(1)
Kim, Chaerin's avatar
Kim, Chaerin committed
116
    }
117

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

123
124
            page.push(items[(startIndex + i)])
        }
baesangjune's avatar
baesangjune committed
125
        console.log("뿌릴 data22222222222222222", page)
126
127
        return page
    }
baesangjune's avatar
baesangjune committed
128
    //usestate 쓰거나 한번에 useeffect에 넣기
129

baesangjune's avatar
baesangjune committed
130
131


baesangjune's avatar
.    
baesangjune committed
132
133

    let time = new Date()
134

Kim, Chaerin's avatar
Kim, Chaerin committed
135
    return (
136
137
138
139
        <Container >
            <Link to="/" className="d-flex justify-content-center"><Image src={ohuh} /></Link>
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
140
141
142
143
144
145
146
147
148
149
150
151
                    <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>
152
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
153
            </Row>
154
            {/* {time.toLocaleString()} */}
155
            <Row className="d-flex flex-wrap">
baesangjune's avatar
baesangjune committed
156
157

                {console.log("#####################33", pagePlace)}
158
159
                {pagePlace.map((place, index) => {
                    return (
160
161
162
163
164
165
                        <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} />
                                <Card.Body >
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
166
                                        {place.address} </Card.Text>
167
168
169
170
171
                                    <Button variant="primary" onClick={() => {
                                        const showArr = [false, false, false, false]
                                        showArr[index] = true
                                        setShowSet(showArr)
                                    }}>{place.name} 자세히 살펴보기</Button>
baesangjune's avatar
baesangjune committed
172
                                    <Place place={place} index={index} show={showSet[index]} onHide={() => setShowSet([false, false, false, false])} />
173
174
175
                                </Card.Body>
                            </Card>
                        </Col>
176
177
                    )
                })}
baesangjune's avatar
baesangjune committed
178
179


180
            </Row>
181
            <Row className="mt-2 d-flex justify-content-center">
182
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
183
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
184
185
186
187
        </Container>
    );
}

188
export default Search;