Place.js 2.12 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, Button, Col, ListGroup} from 'react-bootstrap';
Kim, Chaerin's avatar
Kim, Chaerin committed
4
5
6
7
8
9
10
import queryString from 'query-string'

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
11
  const place = queryString.parse(props.location).place
Kim, Chaerin's avatar
Kim, Chaerin committed
12
13
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

  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
56
      <Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
57
58
59
      {place}
      {Array.isArray(reviews) ? reviews.map((review, index) => {
        return (
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
60
61
          <ListGroup>
          <ListGroup.Item className="mt-4">
Kim, Chaerin's avatar
Kim, Chaerin committed
62
63
64
            <a href={review.link}>{review.title}</a>
            <div>{review.summary}</div>
            <div>{review.content}</div>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
65
66
          </ListGroup.Item>
          </ListGroup>
Kim, Chaerin's avatar
Kim, Chaerin committed
67
68
        )
      })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
69
70
      : "리뷰가 없습니다."}
      </Row>
Kim, Chaerin's avatar
Kim, Chaerin committed
71
72
73
74
75
    </Container>
  );
}

export default Place;