Search.js 8.41 KB
Newer Older
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
1
import React, { useState, useEffect } from 'react';
baesangjune's avatar
정리.    
baesangjune committed
2
import { Link } from 'react-router-dom';
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
3
import ohuh from '../ohuh-sm.PNG';
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
4
import Place from '../Pages/Place.js';
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
5
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Nav } from 'react-bootstrap';
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
6
7
8
import Paginations from '../Components/Paginations';
import axios from 'axios';
import queryString from 'query-string'
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
9
import * as Icon from 'react-bootstrap-icons';
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
10
import { isAuthenticated } from '../utils/auth';
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
11
import catchErrors from '../utils/catchErrors'
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
12
13

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

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
15
16
17
18
    const [state, setState] = useState(false);
    const [index, setIndex] = useState(1);
    const [showSet, setShowSet] = useState([false, false, false, false]);
    const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
19
    const [bookmark, setBookmark] = useState([false, false, false, false])
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
20
    const user = isAuthenticated()
21
    const [mobile, setMobile] = useState();
baesangjune's avatar
baesangjune committed
22
23
24
    const [association, setAssociation] = useState([{ name: " ", address: " ", img: " " }])
    const [pagePlace, setPagePlace] = useState([{ name: " ", address: " ", img: " " }, { name: " ", address: " ", img: " " }])
    const [endPage, setEndPage] = useState(1)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
25
26
27
28
29
30
31
32
33
34
    const [error, setError] = useState('')

    async function getBookmark() {
        try {
            const response = await axios.get(`/api/users/bookmark?ID=${user}`)
            // setBookmark(response.data.bookmark)
        } catch (error) {
            catchErrors(error, setError)
        }
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

    const getAssociation = () => {
        axios.get(`/api/search/association?keyword=${search}`)
            .then(res => {
                console.log("Associations = ", res.data)
                setAssociation(res.data)
            })
            .catch(err => {
                console.log("search.associations 에러 발생", err)
            })
    }



    useEffect(() => {
        getAssociation()
    }, []);

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
53
    useEffect(() => {
baesangjune's avatar
baesangjune committed
54
55
56
57
58
59
60
        if (association.length < 3) {
            setPagePlace(paginate(association, index, association.length))
        }
        else {
            setPagePlace(paginate(association, index, 4))
        }
        setEndPage(Math.floor((association.length / 4)))
61
        console.log("7489309484839", endPage)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
62

baesangjune's avatar
baesangjune committed
63
    }, [association, index])
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
64
65
66
67

    useEffect(() => {
        getAssociation()
        if (state) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
68
69
70
71
72
            // window.location.reload()
            // return <Redirect to={{
            //         pathname: `/search?keyword=${search}`,
            //         state: { id: search },
            //     }} />;
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
73
            props.history.push('/search?keyword=' + search)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
74
            setState(false)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
75
            // console.log("search야", search)
76
77
        } window.addEventListener("scroll", infiniteScroll);
        return () => { window.removeEventListener("scroll", infiniteScroll); }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
78
    }, [state]);
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
79

80
81
82
83
84
85
86
87
    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
88
        }
89
90
        console.log(scrollHeight, scrollTop, clientHeight)
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105


    if (state !== false) {
        // 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}`} />;
    }

    const handlePage = (num) => {
        setIndex(num);
baesangjune's avatar
baesangjune committed
106
107
        console.log("pagenation num", num)
        console.log(index)
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
108
109
110
111
112
113
114
115
116
    }


    const handleChange = (e) => {
        setSearch(e.target.value);
    }

    const handleSubmit = (e) => {
        e.preventDefault()
baesangjune's avatar
baesangjune committed
117
118
        setState(true)
        setIndex(1)
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
119
120
121
122
123
124
    }

    function paginate(items, pageNumber, itemNumber) {
        const page = [];
        const startIndex = (pageNumber - 1) * itemNumber
        for (var i = 0; i < itemNumber; i++) {
baesangjune's avatar
baesangjune committed
125

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

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
133
    async function handlebookmark(index) {
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
134
        if (!bookmark[index]) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
135
            console.log(pagePlace[index])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
136
137
138
139
140
141
            try {
                const response = await axios.put(`/api/users/bookmark?ID=${user}&place=${pagePlace[index]._id}`)
                alert(response.data, '북마크가 저장되었습니다.')
                const showArr = bookmark
                showArr[index] = true
                setBookmark(showArr)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
142
                console.log("bookmark=", bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
143
144
145
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
146
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
147
148
149
150
151
152
153
154
155
156
            try {
                const response = await axios.delete(`/api/users/bookmark?ID=${user}&place=${pagePlace[index]._id}`)
                alert(response.data, '저장된 북마크가 삭제되었습니다.')
                const showArr = bookmark
                showArr[index] = false
                setBookmark(showArr)
                console.log("bookmark=", bookmark)
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
157
158
        }
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
159
160
161
162
163
164


    let time = new Date()

    return (
        <Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
165
            <Link to="/path" className="d-flex justify-content-center"><Image src={ohuh} /></Link>
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
                    <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>
                </Form>
            </Row>
182
            {/* {time.toLocaleString()} */}
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
183
            <Row className="d-flex flex-wrap">
baesangjune's avatar
baesangjune committed
184
185

                {console.log("#####################33", pagePlace)}
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
186
187
188
189
                {pagePlace.map((place, index) => {
                    return (
                        <Col key={index} md={6} >
                            <Card align="center" border="info" style={{ margin: "3%" }}>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
190
191
192
193
194
195
196

                                <Card.Title className="d-flex justify-content-center" style={{ margin: "3%", fontSize: '200%', fontWeight: 'bold' }} >{place.name}
                                    {user ?
                                        <Button
                                            variant={bookmark[index] ? "primary" : "light"}
                                            onClick={() => handlebookmark(index, place)}>
                                            <Icon.BookmarkStarFill size={35} />
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
197
                                        </Button> : null}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
198
                                </Card.Title>
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
199
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
Kim, Chaerin's avatar
Kim, Chaerin committed
200
                                <Card.Body>
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
201
202
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
                                        {place.address} </Card.Text>
Kim, Chaerin's avatar
Kim, Chaerin committed
203
204
205
                                    <Link to={`/place?id=${index}&place=${place.name}`} >
                                        <Button variant="primary"> {place.name} 자세히 살펴보기</Button>
                                    </Link>
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
206
207
208
209
210
211
212
213
214
                                </Card.Body>
                            </Card>
                        </Col>
                    )
                })}
            </Row>
            <Row className="mt-2 d-flex justify-content-center">
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
            </Row>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
215
        </Container >
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
216
217
218
219
    );
}

export default Search;