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) {
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([])
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
20
    const user = isAuthenticated()
baesangjune's avatar
baesangjune committed
21
22
23
    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
24
25
26
27
28
    const [error, setError] = useState('')

    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

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

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

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

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

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

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

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

85
86
    async function handlebookmark(index, place) {
        if (!bookmark.includes(place.name)) {
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;