import React, { useState, useEffect } from 'react' import { Col, Card, Container, Row, Button, 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' import Paginations from '../Components/Paginations'; import _ from 'lodash'; function Bookmark() { const [error, setError] = useState('') const [bookmark, setBookmark] = useState([true, true, true, true]) const [pagePlace, setpagePlace] = useState([]) const [index, setIndex] = useState(1); const [endPage, setEndPage] = useState(1) const [bookmarkPlace, setBookmarkPlace] = useState([{ name: " ", address: " ", img: " " }]) const user = isAuthenticated() async function getBookmark() { try { const bookmarkArr = []; const response = await axios.get(`/api/users/bookmark?ID=${user}`) setBookmarkPlace(response.data.bookmark) } catch (err) { catchErrors(err, setError(err)) console.log("북마크에러", error) } } async function handleBookmark(index) { if (!bookmark[index]) { console.log(bookmarkPlace[index]) try { const response = await axios.put(`/api/users/bookmark?ID=${user}&place=${bookmarkPlace[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=${bookmarkPlace[index]._id}`) alert(response.data, '저장된 북마크가 삭제되었습니다.') getBookmark() console.log("bookmark=", bookmark) } catch (error) { catchErrors(error, setError) } } } const handlePage = (num) => { setIndex(num); } function paginate(items, pageNumber, itemNumber) { const startIndex = (pageNumber - 1) * itemNumber return _(items) .slice(startIndex) .take(itemNumber) .value(); } useEffect(() => { getBookmark() }, []) useEffect(() => { setpagePlace(paginate(bookmarkPlace, index, 4)) setEndPage(Math.ceil((bookmarkPlace.length / 4))) }, [bookmarkPlace, index]) return (

북마크

{console.log("#####################33", bookmarkPlace)} {pagePlace.map((place, index) => { return ( {user ? : null} {place.name} {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])