Bookmark.js 5.5 KB
Newer Older
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
1
import React, { useState, useEffect } from 'react'
baesangjune's avatar
수정    
baesangjune committed
2
import { Col, Card, Container, Row, Button, Nav, Navbar, Image } from "react-bootstrap"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
3
import axios from "axios"
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
4
5
6
import ohuh from '../ohuh-sm.PNG';
import catchErrors from '../utils/catchErrors.js'
import { isAuthenticated } from '../utils/auth'
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
7
import * as Icon from 'react-bootstrap-icons';
8
import { Link } from 'react-router-dom'
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
9

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
10

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
11

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
12

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
13
function Bookmark() {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
14
    const [error, setError] = useState('')
baesangjune's avatar
수정    
baesangjune committed
15
    const [bookmark, setBookmark] = useState([])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
16
    const [pagePlace, setPagePlace] = useState([])
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
17

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
18
19
    const user = isAuthenticated()

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
20
    async function getBookmark() {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
21
        try {
baesangjune's avatar
수정    
baesangjune committed
22
23
            console.log("bookmarkArr 까지는 온다.")
            const bookmarkArr = [];
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
24
25
            const response = await axios.get(`/api/users/bookmark?ID=${user}`)
            setPagePlace(response.data.bookmark)
baesangjune's avatar
수정    
baesangjune committed
26
27
28
29
30
31
32
33
            for (let a = 0; a < pagePlace.length; a++) {
                bookmarkArr.push(true)
            }
            setBookmark(bookmarkArr)
            console.log("bookmarkArr=",bookmarkArr)
        } catch (err) {
            catchErrors(err, setError(err))
            console.log("북마크에러", error)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
34
        }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
35
36
    }

Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

    async function handleBookmark(index) {
        if (!bookmark[index]) {
            console.log(pagePlace[index])
            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)
                console.log("bookmark=", bookmark)
            } catch (error) {
                catchErrors(error, setError)
            }
        } else {
            try {
53
                const response = await axios.delete(`/api/users/bookmark?ID=${user}&place=${pagePlace[index]._id}`)
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
54
55
56
57
58
59
60
61
62
63
64
65
                alert(response.data, '저장된 북마크가 삭제되었습니다.')
                const showArr = bookmark
                showArr[index] = false
                setBookmark(showArr)
                console.log("bookmark=", bookmark)
            } catch (error) {
                catchErrors(error, setError)
            }
        }
    }
    useEffect(() => {
        getBookmark()
baesangjune's avatar
수정    
baesangjune committed
66
    }, [bookmark])
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
67
68


Lee SeoYeon's avatar
.    
Lee SeoYeon committed
69
70
    return (
        <Container>
71
            {/* <Link to="/" >
72
73
74
75
76
77
78
                <Image style={{ margin: "1%" }} src={ohuh} />
            </Link>
            <Navbar bg="info" variant="dark">
                <Navbar.Brand href="/">북마크</Navbar.Brand>
                <Nav className="mr-auto">
                    <Nav.Link href="/">Home</Nav.Link>
                </Nav>
79
80
            </Navbar> */}
            <Link to="/" className="d-flex justify-content-center"><Image style={{ margin: "1%" }} src={ohuh} /></Link>
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
81
82
83
84
85
            <div class="d-flex align-items-center p-3 text-white bg-info rounded shadow-sm">
                <div >
                    <h1 class="h6 mb-0 text-white">북마크</h1>
                </div>
            </div>
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
86
87
88
89
90
91
92
            <Row className="d-flex flex-wrap">
                {console.log("#####################33", pagePlace)}
                {pagePlace.map((place, index) => {
                    return (
                        <Col key={index} md={6} >
                            <Card align="center" border="info" style={{ margin: "3%" }}>

93
                                <Card.Header className="d-flex justify-content-center" style={{ margin: "0", fontSize: '200%', fontWeight: 'bold' }} >{place.name}
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
94
95
                                    {user ?
                                        <Button
96
                                            className="float-right"
baesangjune's avatar
수정    
baesangjune committed
97
                                            variant={bookmark[index] ? "info" : "light"}
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
98
99
100
101
                                            onClick={() => handleBookmark(index, place)}>
                                            <Icon.BookmarkStarFill size={35} />
                                            {console.log("bookmark", bookmark)}
                                            {console.log("bookmark[index]", bookmark[index])}</Button> : null}
102
                                </Card.Header>
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
103
104
105
106
                                <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
                                <Card.Body >
                                    <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
                                        {place.address} </Card.Text>
107
                                    <Link to={`/place?&place=${place.name}`} >
Kim, Chaerin's avatar
z    
Kim, Chaerin committed
108
109
                                        <Button variant="info"> {place.name} 자세히 살펴보기</Button>
                                    </Link>
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
110
111
112
113
114
115
116
117
                                </Card.Body>
                            </Card>
                        </Col>
                    )
                })}


            </Row>
118
        </Container >
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
119
120
121
    )
}

Lee SeoYeon's avatar
..    
Lee SeoYeon committed
122
123
124
125
export default Bookmark


    // async function handleSubmit(e){
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
126
        //     setState(true);  //버튼이 눌려서 handlesubmit이될때 setState값이 true로 바뀐다
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
127
128
129
130
131
132
133
134
135
136
137
138
    //     try { //respons 서버에 post로 요청하여 데이터를 받아온다
    //         const response = await axios.post('/api/users/bookmark', page)
    //         setSuccess(true)
    //     } catch (error) {
    //         console.log(error)
    //         catchErrors(error, setError)
    //     }
    // }

    // useEffect(() => {
    //     getBookmark(user)
    // }, [user])