Search.js 6.33 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';
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([])
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
    const [error, setError] = useState('')
23

baesangjune's avatar
수정    
baesangjune committed
24
    const user = isAuthenticated()
25

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

baesangjune's avatar
baesangjune committed
35
    const getAssociation = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
36
        axios.get(`/api/search/association?keyword=${search}`)
baesangjune's avatar
baesangjune committed
37
38
39
40
41
42
            .then(res => {
                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
50
51

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

59
60
61
62
    const handlePage = (num) => {
        setIndex(num);
    }

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

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

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

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

83
    async function handlebookmark(index, place) {
baesangjune's avatar
baesangjune committed
84
85
        const include = bookmark.findIndex(i => i.name === place.name) !== -1
        if (!include) {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
86
87
88
            try {
                const response = await axios.put(`/api/users/bookmark?ID=${user}&place=${pagePlace[index]._id}`)
                alert(response.data, '북마크가 저장되었습니다.')
89
                getBookmark()
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
90
91
92
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
93
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
94
95
96
            try {
                const response = await axios.delete(`/api/users/bookmark?ID=${user}&place=${pagePlace[index]._id}`)
                alert(response.data, '저장된 북마크가 삭제되었습니다.')
97
                getBookmark()
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
98
99
100
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
101
102
        }
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
103
    return (
104
        <Container >
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
105
            <Link to="/"><Image src={ohuh} /></Link>
Kim, Chaerin's avatar
last    
Kim, Chaerin committed
106
            <Row className="mb-2 d-flex justify-content-center">
107
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
108
109
110
111
112
113
114
115
116
                    <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
117
                            <Button type="submit" variant="info" >검색</Button>
118
119
                        </InputGroup.Append>
                    </InputGroup>
120
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
121
            </Row>
baesangjune's avatar
baesangjune committed
122
            <Row>
baesangjune's avatar
baesangjune committed
123

124
125
                {pagePlace.map((place, index) => {
                    return (
126
                        <Col key={index} md={6} >
baesangjune's avatar
baesangjune committed
127
128
129
                            <Card border="info" style={{ margin: "2%" }}>
                                <Row>
                                    <Card.Header style={{ margin: "0", marginLeft: "3%", marginRight: "3%", fontSize: '200%', fontWeight: 'bold', width: "100vw" }} >
baesangjune's avatar
baesangjune committed
130
131
                                        {user ?
                                            <Button
132
                                                style={{ marginRight: "3%" }}
133
                                                variant={bookmark.findIndex(i => i.name === place.name) !== -1 ? "info" : "light"}
baesangjune's avatar
baesangjune committed
134
135
                                                onClick={() => handlebookmark(index, place)}>
                                                <Icon.BookmarkStarFill size={35} />
136
                                            </Button> : null}
137
                                        {place.name}
baesangjune's avatar
baesangjune committed
138
139
                                    </Card.Header>
                                </Row>
140
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
Kim, Chaerin's avatar
Kim, Chaerin committed
141
                                <Card.Body>
baesangjune's avatar
baesangjune committed
142
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "65px" }} >
143
                                        {place.address} </Card.Text>
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
144
                                    <Link to={`/place?name=${place.name}&src=${place.img}&address=${place.address}`} >
baesangjune's avatar
.    
baesangjune committed
145
                                        <Button variant="info"> {place.name} 자세히 살펴보기</Button>
Kim, Chaerin's avatar
Kim, Chaerin committed
146
                                    </Link>
147
148
149
                                </Card.Body>
                            </Card>
                        </Col>
150
151
152
                    )
                })}
            </Row>
153
            <Row className="mt-2 d-flex justify-content-center">
154
                <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
155
            </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
156
        </Container >
Kim, Chaerin's avatar
Kim, Chaerin committed
157
158
159
    );
}

160
export default Search;