Place.js 2.32 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
2
import axios from 'axios';
import React, { useEffect, useState } from 'react';
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
3
import { Container, Row, ListGroup, Image} from 'react-bootstrap';
Kim, Chaerin's avatar
Kim, Chaerin committed
4
import queryString from 'query-string'
baesangjune's avatar
baesangjune committed
5
6
import { Link } from 'react-router-dom';
import ohuh from '../ohuh-sm.PNG';
Kim, Chaerin's avatar
Kim, Chaerin committed
7
8
9
10
11
12

function Place(props) {
  console.log(props)
  const [db, setDb] = useState(false)
  const [index, setIndex] = useState(0)
  const [reviews, setReviews] = useState([])
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
13
  const place = queryString.parse(props.location).place
Kim, Chaerin's avatar
Kim, Chaerin committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

  const getReview = (index) => {
    console.log(index, "dlseprtm")
    axios({ url: `/api/review?keyword=${place}&index=${index}`, method: 'post', data: { db: db } })
      .then(res => {
        console.log("place res.data", res.data)
        setReviews([...reviews, ...res.data.review])
        setDb(res.data.db)
        setIndex(res.data.index)
      })
      .then(() => {
        console.log(index, "인텍스", db)
      })
      .catch(err => {
        console.log(err)
      })
  }

  useEffect(() => {
    getReview();
    window.addEventListener("scroll", infiniteScroll);
    return () => { window.removeEventListener("scroll", infiniteScroll); }
  }, []);

  // useEffect(() => {
  //   getReview();

  // }, [index])

  const infiniteScroll = () => {
    const { documentElement, body } = document;
    const scrollHeight = Math.max(documentElement.scrollHeight, body.scrollHeight);
    const scrollTop = Math.max(documentElement.scrollTop, body.scrollTop);
    const clientHeight = documentElement.clientHeight;
    if (scrollTop + clientHeight >= scrollHeight) {
      // setIndex(index + 1)
      getReview(index + 1);
      console.log("더불러", index + 1)
    }
    console.log(scrollHeight, scrollTop, clientHeight)
  }

  return (
    <Container {...props}>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
58
      <Row>
baesangjune's avatar
baesangjune committed
59
60
61
      <Link to="/" className="d-flex justify-content-center">
        <Image style={{ margin: "1%" }} src={ohuh} />
      </Link>
Kim, Chaerin's avatar
Kim, Chaerin committed
62
63
64
      {place}
      {Array.isArray(reviews) ? reviews.map((review, index) => {
        return (
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
65
66
          <ListGroup>
          <ListGroup.Item className="mt-4">
Kim, Chaerin's avatar
Kim, Chaerin committed
67
68
69
            <a href={review.link}>{review.title}</a>
            <div>{review.summary}</div>
            <div>{review.content}</div>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
70
71
          </ListGroup.Item>
          </ListGroup>
Kim, Chaerin's avatar
Kim, Chaerin committed
72
73
        )
      })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
74
75
      : "리뷰가 없습니다."}
      </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
76
77
78
79
80
    </Container>
  );
}

export default Place;