Search.js 6.34 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) {
baesangjune's avatar
baesangjune committed
15

Kim, Chaerin's avatar
Kim, Chaerin committed
16
    const [state, setState] = useState(false);
17
    const [index, setIndex] = useState(1);
Kim, Chaerin's avatar
Kim, Chaerin committed
18
    const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
19
    const [bookmark, setBookmark] = useState([])
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
    const [error, setError] = useState('')
24

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

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

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

baesangjune's avatar
baesangjune committed
46
    useEffect(() => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
47
48
        setPagePlace(paginate(association, index, 4))
        setEndPage(Math.ceil((association.length / 4)))
baesangjune's avatar
baesangjune committed
49
    }, [association, index])
baesangjune's avatar
baesangjune committed
50
51
52

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

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

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

Kim, Chaerin's avatar
Kim, Chaerin committed
68
    const handleSubmit = (e) => {
baesangjune's avatar
baesangjune committed
69
        e.preventDefault()
baesangjune's avatar
baesangjune committed
70
71
        setState(true)
        setIndex(1)
baesangjune's avatar
.    
baesangjune committed
72
        setBookmark([false, false, false, false])
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
83
    }

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

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

161
export default Search;