Search.js 8.08 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';
5
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Pagination } 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';
Kim, Chaerin's avatar
Kim, Chaerin committed
8
9

function Search(props) {
baesangjune's avatar
style    
baesangjune committed
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(props.location.state.id);
15
    const [mobile, setMobile] = useState();
baesangjune's avatar
.    
baesangjune committed
16
    const [place, setPlace] = useState([{ name: "", category: "", address: "" }])
baesangjune's avatar
.    
baesangjune committed
17
    const [imgUrl, setImgUrl] = useState([])
18

baesangjune's avatar
.    
baesangjune committed
19
20
21
22
23
24
25
26
27
28
    const getImg = () => {
        axios.get(`/api/search/imges/${search}`)
            .then(res => {
                console.log("images=", res.data)
                setImgUrl(res.data)
            })
            .catch(err => {
                console.log('search.images발생', err)
            })
    }
baesangjune's avatar
.    
baesangjune committed
29

baesangjune's avatar
.    
baesangjune committed
30
31
32
33
34
35
36
37
38
39
    const getPlace = () => {
        axios.get(`/api/search/places/${search}`)
            .then(res => {
                console.log("places=", res.data)
                setPlace(res.data)
            })
            .catch(err => {
                console.log('search.places발생', err)
            })
    }
baesangjune's avatar
.    
baesangjune committed
40
41
42



43
    useEffect(() => {
baesangjune's avatar
.    
baesangjune committed
44
        getPlace()
baesangjune's avatar
.    
baesangjune committed
45
        getImg()
46
47
48
49
50
51
        if (window.innerWidth < 960) {
            setMobile(true)
        } else {
            setMobile(false)
        }
    }, []);
Kim, Chaerin's avatar
Kim, Chaerin committed
52

baesangjune's avatar
.    
baesangjune committed
53
54
55
56
57





58
59
60
    const places = [{
        name: "한라산(hallasan)",
        address: "제주 서귀포시 토평동 산15-1",
baesangjune's avatar
.    
baesangjune committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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",
78
79
    }]

Kim, Chaerin's avatar
Kim, Chaerin committed
80
81
    if (state !== false) {
        return <Redirect to={{
82
83
            pathname: `/search/${search}`,
            state: { id: search },
Kim, Chaerin's avatar
Kim, Chaerin committed
84
        }} />;
85
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
86

baesangjune's avatar
style    
baesangjune committed
87
88
89
90
91
    const handlePage = (num) => {
        setIndex(num);
    }


Kim, Chaerin's avatar
Kim, Chaerin committed
92
93
94
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
95

Kim, Chaerin's avatar
Kim, Chaerin committed
96
    const handleSubmit = (e) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
97
        setState(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
98
    }
99

baesangjune's avatar
.    
baesangjune committed
100
    function paginate(items, pageNumber, itemNumber) {
101
        const page = [];
baesangjune's avatar
.    
baesangjune committed
102
        const startIndex = (pageNumber - 1) * itemNumber
Kim, Chaerin's avatar
Kim, Chaerin committed
103
        for (var i = 0; i < itemNumber; i++) {
104
105
106
107
108
            page.push(items[(startIndex + i)])
        }
        return page
    }

baesangjune's avatar
.    
baesangjune committed
109
110
111
112
113
114
115
116
117
118
119
    const pagePlace = paginate(places, index, 2)


    // function times (){
    //     let time = new Date()
    //     console.log(time)
    //     return time
    // }
    // console.log(times())

    let time = new Date()
120

Kim, Chaerin's avatar
Kim, Chaerin committed
121
    return (
baesangjune's avatar
s    
baesangjune committed
122
        <Container >
123
124
125
            <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}>
126
127
128
129
130
131
132
133
134
135
136
137
                    <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>
138
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
139
            </Row>
baesangjune's avatar
.    
baesangjune committed
140
141
142
143
            {time.toString()}****
            {time.toLocaleString()}****
            {time.toLocaleDateString()}****
            {time.toLocaleTimeString()}****
baesangjune's avatar
.    
baesangjune committed
144
            <Col md={6} >
baesangjune's avatar
.    
baesangjune committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
                <Card align="center" border="info" style={{ margin: "3%" }}>
                    <Card.Title style={{ margin: "3%", fontSize: '200%', fontWeight: 'bold' }} >{place[0].name}</Card.Title>
                    <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={imgUrl} />
                    <Card.Body >
                        <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
                            {place[0].address}{place[0].category} </Card.Text>
                        <Button variant="primary" onClick={() => {
                            // const showArr = [false, false, false, false]
                            // showArr[index] = true
                            // setShowSet(showArr)
                        }}>{place[0].name} 자세히 살펴보기</Button>
                        {/* <Place search={place}  show={show} onHide={() => setShowSet([false, false, false, false])} /> */}
                    </Card.Body>
                </Card>
            </Col>


162
163
164
            <Row className="d-flex flex-wrap">
                {pagePlace.map((place, index) => {
                    return (
baesangjune's avatar
style    
baesangjune committed
165
                        <Col key={index} md={6} >
166
167
168
169
170
                            <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" }} >
171
                                        {place.address} </Card.Text>
172
173
174
175
176
177
                                    <Button variant="primary" onClick={() => {
                                        const showArr = [false, false, false, false]
                                        showArr[index] = true
                                        setShowSet(showArr)
                                    }}>{place.name} 자세히 살펴보기</Button>
                                    <Place search={place} index={index} show={showSet[index]} onHide={() => setShowSet([false, false, false, false])} />
178
179
180
                                </Card.Body>
                            </Card>
                        </Col>
181
182
183
                    )
                })}
            </Row>
baesangjune's avatar
.    
baesangjune committed
184
            {/* {console.log(showSet)} */}
baesangjune's avatar
modal    
baesangjune committed
185
            {/* show가 전부 true로 바뀌어서 전부 다 보이게 되는 것이다. */}
186
            <Row className="mt-2 d-flex justify-content-center">
baesangjune's avatar
style    
baesangjune committed
187
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
188
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
189
190
191
192
        </Container>
    );
}

193
export default Search;