Search.js 8.04 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';
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);
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
17
    const [bookmark, setBookmark] = useState([false, false, false, false])
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
27
28
    const [error, setError] = useState('')

    async function getBookmark() {
        try {
            const response = await axios.get(`/api/users/bookmark?ID=${user}`)
            // setBookmark(response.data.bookmark)
        } 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

baesangjune's avatar
baesangjune committed
33
    const getAssociation = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
34
        axios.get(`/api/search/association?keyword=${search}`)
baesangjune's avatar
baesangjune 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

45
46

    useEffect(() => {
baesangjune's avatar
.    
baesangjune committed
47
        getAssociation()
baesangjune's avatar
baesangjune committed
48
49
50
    }, []);

    useEffect(() => {
baesangjune's avatar
baesangjune committed
51
52
53
54
55
56
57
58
59
        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
60
61
62

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

76
77
78
79
80
81
82
83
    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
84
        }
85
86
        console.log(scrollHeight, scrollTop, clientHeight)
    }
baesangjune's avatar
.    
baesangjune committed
87

88
89
    const handlePage = (num) => {
        setIndex(num);
baesangjune's avatar
baesangjune committed
90
91
        console.log("pagenation num", num)
        console.log(index)
92
93
94
    }


Kim, Chaerin's avatar
Kim, Chaerin committed
95
96
97
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
98

Kim, Chaerin's avatar
Kim, Chaerin committed
99
    const handleSubmit = (e) => {
baesangjune's avatar
baesangjune committed
100
        e.preventDefault()
baesangjune's avatar
baesangjune committed
101
102
        setState(true)
        setIndex(1)
baesangjune's avatar
.    
baesangjune committed
103
        setBookmark([false, false, false, false])
Kim, Chaerin's avatar
Kim, Chaerin committed
104
    }
105

Kim, Chaerin's avatar
Kim, Chaerin committed
106
    function paginate(items, pageNumber, itemNumber) {
107
        const page = [];
Kim, Chaerin's avatar
Kim, Chaerin committed
108
109
        const startIndex = (pageNumber - 1) * itemNumber
        for (var i = 0; i < itemNumber; i++) {
baesangjune's avatar
baesangjune committed
110

111
112
            page.push(items[(startIndex + i)])
        }
baesangjune's avatar
baesangjune committed
113
        console.log("뿌릴 data22222222222222222", page)
114
115
116
        return page
    }

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

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

201
export default Search;