Search.js 6.83 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';
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
10
11
12
import catchErrors from '../utils/catchErrors';
import _ from 'lodash';

Kim, Chaerin's avatar
Kim, Chaerin committed
13
14

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

    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

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
34
    const getAssociation = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
35
        axios.get(`/api/search/association?keyword=${search}`)
Kim, Chaerin's avatar
.    
Kim, Chaerin 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(() => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
46
47
        setPagePlace(paginate(association, index, 4))
        setEndPage(Math.ceil((association.length / 4)))
baesangjune's avatar
baesangjune committed
48
    }, [association, index])
baesangjune's avatar
.    
baesangjune committed
49

baesangjune's avatar
baesangjune committed
50
51
    useEffect(() => {
        getAssociation()
52
        getBookmark()
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
53
54
        if (state) {
            props.history.push('/search?keyword=' + search)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
55
            setState(false)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
56
        }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
57
    }, [state]);
baesangjune's avatar
.    
baesangjune committed
58

59
60
    const handlePage = (num) => {
        setIndex(num);
baesangjune's avatar
baesangjune committed
61
62
        console.log("pagenation num", num)
        console.log(index)
63
64
    }

Kim, Chaerin's avatar
Kim, Chaerin committed
65
66
67
    const handleChange = (e) => {
        setSearch(e.target.value);
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
68

Kim, Chaerin's avatar
Kim, Chaerin committed
69
    const handleSubmit = (e) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
70
        e.preventDefault()
baesangjune's avatar
baesangjune committed
71
72
        setState(true)
        setIndex(1)
Kim, Chaerin's avatar
Kim, Chaerin committed
73
    }
74

Kim, Chaerin's avatar
Kim, Chaerin committed
75
76
    function paginate(items, pageNumber, itemNumber) {
        const startIndex = (pageNumber - 1) * itemNumber
baesangjune's avatar
baesangjune committed
77

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
78
79
80
81
        return _(items)
            .slice(startIndex)
            .take(itemNumber)
            .value();
82
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
83

84
85
    async function handlebookmark(index, place) {
        if (!bookmark.includes(place.name)) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
86
            console.log(pagePlace[index])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
87
88
89
90
91
92
            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)
93
                console.log("bookmark=", bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
94
95
96
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
97
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
98
99
100
101
102
103
104
105
106
107
            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
108
109
        }
    }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
110

111

baesangjune's avatar
.    
baesangjune committed
112
    let time = new Date()
113

Kim, Chaerin's avatar
Kim, Chaerin committed
114
    return (
115
        <Container >
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
116
            <Link to="/"><Image src={ohuh} /></Link>
117
118
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
119
120
121
122
123
124
125
126
127
                    <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
128
                            <Button type="submit" variant="info" >검색</Button>
129
130
                        </InputGroup.Append>
                    </InputGroup>
131
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
132
            </Row>
133
            <Row className="d-flex flex-wrap">
baesangjune's avatar
baesangjune committed
134
135

                {console.log("#####################33", pagePlace)}
136
137
                {pagePlace.map((place, index) => {
                    return (
138
                        <Col key={index} md={6} >
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
139
                            <Card align="center" border="info" style={{ margin: "2%" }}>
140
141
142
                                <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
143
144
                                        {user ?
                                            <Button
145
                                                variant={bookmark.findIndex(i => i.name === place.name) !== -1 ? "info" : "light"}
baesangjune's avatar
baesangjune committed
146
147
                                                onClick={() => handlebookmark(index, place)}>
                                                <Icon.BookmarkStarFill size={35} />
148
                                            </Button> : null}
baesangjune's avatar
baesangjune committed
149
150
                                    </Card.Header>
                                </Row>
151
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
Kim, Chaerin's avatar
Kim, Chaerin committed
152
                                <Card.Body>
153
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
154
                                        {place.address} </Card.Text>
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
155
                                    <Link to={`/place?name=${place.name}&src=${place.img}&address=${place.address}`} >
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
156
                                        <Button variant="info"> {place.name} 자세히 살펴보기</Button>
Kim, Chaerin's avatar
Kim, Chaerin committed
157
                                    </Link>
158
159
160
                                </Card.Body>
                            </Card>
                        </Col>
161
162
163
                    )
                })}
            </Row>
164
            <Row className="mt-2 d-flex justify-content-center">
165
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
166
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
167
        </Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
168
169
170
    );
}

171
export default Search;