Search.js 7.64 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';
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';
baesangjune's avatar
baesangjune 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([])
baesangjune's avatar
baesangjune committed
18
19
20
    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
21
    const [error, setError] = useState('')
baesangjune's avatar
수정    
baesangjune committed
22
23
24
    
    const user = isAuthenticated()
    
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
25
26
27
    async function getBookmark() {
        try {
            const response = await axios.get(`/api/users/bookmark?ID=${user}`)
28
            setBookmark(response.data.bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
29
        } catch (error) {
baesangjune's avatar
baesangjune committed
30
            catchErrors(error, setError(error))
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
31
32
        }
    }
baesangjune's avatar
.    
baesangjune committed
33

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

baesangjune's avatar
baesangjune committed
45
    useEffect(() => {
baesangjune's avatar
baesangjune committed
46
47
48
49
50
51
52
53
54
        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
55
56
57

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

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

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


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

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

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

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

107
108
    async function handlebookmark(index, place) {
        if (!bookmark.includes(place.name)) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
109
            console.log(pagePlace[index])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
110
111
112
113
114
115
            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)
116
                console.log("bookmark=", bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
117
118
119
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
120
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
121
122
123
124
125
126
127
128
129
130
            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
131
132
        }
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
133
    return (
134
        <Container >
baesangjune's avatar
수정    
baesangjune committed
135
            <Link to="/" ><Image src={ohuh} /></Link>
136
137
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
138
139
140
141
142
143
144
145
146
                    <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
147
                            <Button type="submit" variant="info" >검색</Button>
148
149
                        </InputGroup.Append>
                    </InputGroup>
150
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
151
            </Row>
baesangjune's avatar
baesangjune committed
152
            <Row>
baesangjune's avatar
baesangjune committed
153
154

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

191
export default Search;