Search.js 7.7 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
baesangjune's avatar
정리.    
baesangjune committed
2
import { Link } from 'react-router-dom';
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
3
import ohuh from '../ohuh-sm.PNG';
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
4
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Nav } 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';
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
7
import queryString from 'query-string'
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
8
import * as Icon from 'react-bootstrap-icons';
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
9
import { isAuthenticated } from '../utils/auth';
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
10
import catchErrors from '../utils/catchErrors'
Kim, Chaerin's avatar
Kim, Chaerin committed
11
12

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

Kim, Chaerin's avatar
Kim, Chaerin committed
14
    const [state, setState] = useState(false);
15
    const [index, setIndex] = useState(1);
Kim, Chaerin's avatar
Kim, Chaerin committed
16
    const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
17
    const [bookmark, setBookmark] = useState([])
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
18
    const user = isAuthenticated()
baesangjune's avatar
baesangjune committed
19
20
21
    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
22
23
24
25
26
    const [error, setError] = useState('')

    async function getBookmark() {
        try {
            const response = await axios.get(`/api/users/bookmark?ID=${user}`)
27
            setBookmark(response.data.bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
28
        } catch (error) {
baesangjune's avatar
baesangjune committed
29
            catchErrors(error, setError(error))
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
30
31
        }
    }
baesangjune's avatar
.    
baesangjune committed
32

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
33
    const getAssociation = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
34
        axios.get(`/api/search/association?keyword=${search}`)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
35
36
37
38
39
40
41
            .then(res => {
                console.log("Associations = ", res.data)
                setAssociation(res.data)
            })
            .catch(err => {
                console.log("search.associations 에러 발생", err)
            })
baesangjune's avatar
.    
baesangjune committed
42
43
    }

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

baesangjune's avatar
baesangjune committed
53
    }, [association, index])
baesangjune's avatar
.    
baesangjune committed
54

baesangjune's avatar
baesangjune committed
55
56
    useEffect(() => {
        getAssociation()
57
        getBookmark()
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
58
59
        if (state) {
            props.history.push('/search?keyword=' + search)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
60
            setState(false)
61
62
        } window.addEventListener("scroll", infiniteScroll);
        return () => { window.removeEventListener("scroll", infiniteScroll); }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
63
    }, [state]);
baesangjune's avatar
baesangjune committed
64

65
66
67
68
69
70
71
72
    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
73
        }
74
75
        console.log(scrollHeight, scrollTop, clientHeight)
    }
baesangjune's avatar
.    
baesangjune committed
76

77
78
    const handlePage = (num) => {
        setIndex(num);
baesangjune's avatar
baesangjune committed
79
80
        console.log("pagenation num", num)
        console.log(index)
81
82
83
    }


Kim, Chaerin's avatar
Kim, Chaerin committed
84
85
86
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
87

Kim, Chaerin's avatar
Kim, Chaerin committed
88
    const handleSubmit = (e) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
89
        e.preventDefault()
baesangjune's avatar
baesangjune committed
90
91
        setState(true)
        setIndex(1)
Kim, Chaerin's avatar
Kim, Chaerin committed
92
    }
93

Kim, Chaerin's avatar
Kim, Chaerin committed
94
    function paginate(items, pageNumber, itemNumber) {
95
        const page = [];
Kim, Chaerin's avatar
Kim, Chaerin committed
96
97
        const startIndex = (pageNumber - 1) * itemNumber
        for (var i = 0; i < itemNumber; i++) {
baesangjune's avatar
baesangjune committed
98

99
100
            page.push(items[(startIndex + i)])
        }
baesangjune's avatar
baesangjune committed
101
        console.log("뿌릴 data22222222222222222", page)
102
103
        return page
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
104

105
106
    async function handlebookmark(index, place) {
        if (!bookmark.includes(place.name)) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
107
            console.log(pagePlace[index])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
108
109
110
111
112
113
            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)
114
                console.log("bookmark=", bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
115
116
117
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
118
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
119
120
121
122
123
124
125
126
127
128
            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
129
130
        }
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
131

132

baesangjune's avatar
.    
baesangjune committed
133
    let time = new Date()
134

Kim, Chaerin's avatar
Kim, Chaerin committed
135
    return (
136
        <Container >
137
            <Link to="/" className="d-flex justify-content-center" ><Image src={ohuh} /></Link>
138
139
            <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
                    <InputGroup size="lg">
                        <FormControl
                            placeholder="검색어를 입력하세요."
                            value={search}
                            aria-label="Large"
                            aria-describedby="inputGroup-sizing-sm"
                            onChange={handleChange}
                        />
                        <InputGroup.Append>
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
149
                            <Button type="submit" variant="info" >검색</Button>
150
151
                        </InputGroup.Append>
                    </InputGroup>
152
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
153
            </Row>
154
            <Row className="d-flex flex-wrap">
baesangjune's avatar
baesangjune committed
155
156

                {console.log("#####################33", pagePlace)}
157
158
                {pagePlace.map((place, index) => {
                    return (
159
                        <Col key={index} md={6} >
160
161
162
163
                            <Card align="right" border="info" style={{ margin: "2%" }}>
                                <Row className="d-flex justify-content-between">
                                    <Card.Header style={{ margin: "0", marginLeft: "3%", marginRight: "3%", fontSize: '200%', fontWeight: 'bold', width: "100vw" }} >{place.name}
                                        {console.log(bookmark.findIndex(i => i.name === place.name))}
baesangjune's avatar
baesangjune committed
164
165
                                        {user ?
                                            <Button
166
                                                variant={bookmark.findIndex(i => i.name === place.name) !== -1 ? "info" : "light"}
baesangjune's avatar
baesangjune committed
167
168
                                                onClick={() => handlebookmark(index, place)}>
                                                <Icon.BookmarkStarFill size={35} />
169
                                            </Button> : null}
baesangjune's avatar
baesangjune committed
170
171
                                    </Card.Header>
                                </Row>
172
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
Kim, Chaerin's avatar
Kim, Chaerin committed
173
                                <Card.Body>
174
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
175
                                        {place.address} </Card.Text>
Kim, Chaerin's avatar
Kim, Chaerin committed
176
                                    <Link to={`/place?id=${index}&place=${place.name}`} >
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
177
                                        <Button variant="info"> {place.name} 자세히 살펴보기</Button>
Kim, Chaerin's avatar
Kim, Chaerin committed
178
                                    </Link>
179
180
181
                                </Card.Body>
                            </Card>
                        </Col>
182
183
184
                    )
                })}
            </Row>
185
            <Row className="mt-2 d-flex justify-content-center">
186
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
187
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
188
        </Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
189
190
191
    );
}

192
export default Search;