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

112

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

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

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

172
export default Search;