Search.js 6.82 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('')
baesangjune's avatar
수정    
baesangjune committed
24
25
26
    
    const user = isAuthenticated()
    
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
44
            .then(res => {
                console.log("Associations = ", res.data)
                setAssociation(res.data)
            })
            .catch(err => {
                console.log("search.associations 에러 발생", err)
            })
baesangjune's avatar
.    
baesangjune committed
45
46
    }

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

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

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

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

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

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

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
81
82
83
84
        return _(items)
            .slice(startIndex)
            .take(itemNumber)
            .value();
85
86
    }

87
88
    async function handlebookmark(index, place) {
        if (!bookmark.includes(place.name)) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
89
            console.log(pagePlace[index])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
90
91
92
93
94
95
            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)
96
                console.log("bookmark=", bookmark)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
97
98
99
            } catch (error) {
                catchErrors(error, setError)
            }
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
100
        } else {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
101
102
103
104
105
106
107
108
109
110
            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
111
112
        }
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
113
    return (
114
        <Container >
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
115
            <Link to="/"><Image src={ohuh} /></Link>
116
117
            <Row className="mb-2" className="d-flex justify-content-center">
                <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
118
119
120
121
122
123
124
125
126
                    <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
127
                            <Button type="submit" variant="info" >검색</Button>
128
129
                        </InputGroup.Append>
                    </InputGroup>
130
                </Form>
Kim, Chaerin's avatar
Kim, Chaerin committed
131
            </Row>
baesangjune's avatar
baesangjune committed
132
            <Row>
baesangjune's avatar
baesangjune committed
133
134

                {console.log("#####################33", pagePlace)}
135
136
                {pagePlace.map((place, index) => {
                    return (
137
                        <Col key={index} md={6} >
baesangjune's avatar
baesangjune committed
138
139
140
                            <Card border="info" style={{ margin: "2%" }}>
                                <Row>
                                    <Card.Header style={{ margin: "0", marginLeft: "3%", marginRight: "3%", fontSize: '200%', fontWeight: 'bold', width: "100vw" }} >
141
                                        {console.log(bookmark.findIndex(i => i.name === place.name))}
baesangjune's avatar
baesangjune committed
142
143
                                        {user ?
                                            <Button
144
                                                variant={bookmark.findIndex(i => i.name === place.name) !== -1 ? "info" : "light"}
baesangjune's avatar
baesangjune committed
145
146
                                                onClick={() => handlebookmark(index, place)}>
                                                <Icon.BookmarkStarFill size={35} />
147
                                            </Button> : null}
baesangjune's avatar
baesangjune committed
148
                                            {place.name}
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}`} >
baesangjune's avatar
.    
baesangjune 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;