Place.js 2.23 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
2
import axios from 'axios';
import React, { useEffect, useState } from 'react';
baesangjune's avatar
baesangjune committed
3
import { Container, Row, 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
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
56
57

function Place(props) {
  console.log(props)
  const [db, setDb] = useState(false)
  const [index, setIndex] = useState(0)
  const [reviews, setReviews] = useState([])
  const place = queryString.parse(props.location.search).place

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

export default Place;