import React, { useState, useEffect } from 'react' import { Col, Card, Container, Row, Button, Nav, Navbar, Image } from "react-bootstrap" import axios from "axios" import ohuh from '../ohuh-sm.PNG'; import catchErrors from '../utils/catchErrors.js' import { isAuthenticated } from '../utils/auth' import * as Icon from 'react-bootstrap-icons'; import { Link } from 'react-router-dom'; function Bookmark() { const [error, setError] = useState('') const [bookmark, setBookmark] = useState([]) const [pagePlace, setPagePlace] = useState([]) const user = isAuthenticated() async function getBookmark() { try { console.log("bookmarkArr 까지는 온다.") const bookmarkArr = []; const response = await axios.get(`/api/users/bookmark?ID=${user}`) setPagePlace(response.data.bookmark) 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) } } 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 { 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) } } } useEffect(() => { getBookmark() }, [bookmark]) return ( 북마크 {console.log("#####################33", pagePlace)} {pagePlace.map((place, index) => { return ( {place.name} {user ? : null} {place.address} ) })} ) } export default Bookmark // async function handleSubmit(e){ // setState(true); //버튼이 눌려서 handlesubmit이될때 setState값이 true로 바뀐다 // 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])