import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import ohuh from '../ohuh-sm.PNG'; import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Nav } from 'react-bootstrap'; import Paginations from '../Components/Paginations'; import axios from 'axios'; import queryString from 'query-string' import * as Icon from 'react-bootstrap-icons'; import { isAuthenticated } from '../utils/auth'; import catchErrors from '../utils/catchErrors'; import _ from 'lodash'; function Search(props) { const [state, setState] = useState(false); const [index, setIndex] = useState(1); const [search, setSearch] = useState(queryString.parse(props.location.search).keyword); const [bookmark, setBookmark] = useState([]) const user = isAuthenticated() const [association, setAssociation] = useState([{ name: " ", address: " ", img: " " }]) const [pagePlace, setPagePlace] = useState([{ name: " ", address: " ", img: " " }, { name: " ", address: " ", img: " " }]) const [endPage, setEndPage] = useState(1) const [error, setError] = useState('') async function getBookmark() { try { const response = await axios.get(`/api/users/bookmark?ID=${user}`) setBookmark(response.data.bookmark) } catch (error) { catchErrors(error, setError(error)) } } const getAssociation = () => { axios.get(`/api/search/association?keyword=${search}`) .then(res => { console.log("Associations = ", res.data) setAssociation(res.data) }) .catch(err => { console.log("search.associations 에러 발생", err) }) } useEffect(() => { setPagePlace(paginate(association, index, 4)) setEndPage(Math.ceil((association.length / 4))) }, [association, index]) useEffect(() => { getAssociation() getBookmark() if (state) { props.history.push('/search?keyword=' + search) setState(false) } }, [state]); const handlePage = (num) => { setIndex(num); console.log("pagenation num", num) console.log(index) } const handleChange = (e) => { setSearch(e.target.value); } const handleSubmit = (e) => { e.preventDefault() setState(true) setIndex(1) } function paginate(items, pageNumber, itemNumber) { const startIndex = (pageNumber - 1) * itemNumber return _(items) .slice(startIndex) .take(itemNumber) .value(); } async function handlebookmark(index, place) { if (!bookmark.includes(place.name)) { 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) } } } let time = new Date() return (
{console.log("#####################33", pagePlace)} {pagePlace.map((place, index) => { return ( {place.name} {console.log(bookmark.findIndex(i => i.name === place.name))} {user ? : null} {place.address} ) })}
); } export default Search;