Commit 2f7efa72 authored by Lee SeoYeon's avatar Lee SeoYeon
Browse files

Merge remote-tracking branch 'origin/cherry' into lsy

parents c368c2c6 e08aede8
browser=none
\ No newline at end of file
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { Modal, Container, Row, Button, } from 'react-bootstrap';
function Place(props) {
const [reviews, setReviews] = useState()
const [db, setDb] = useState(false)
const getReview = () => {
axios({ url: `/api/review?keyword=${props.place.name}`, method: 'post', data: { db: db } })
.then(res => {
console.log("place res.data", res.data)
setReviews(res.data.review)
setDb(res.data.db)
console.log(db)
})
.catch(err => {
console.log(err)
})
}
useEffect(() => {
getReview();
}, [])
return (
<Modal {...props}
size="xl"
keyboard="true"
variant="backdrop.in"
aria-labelledby="example-modal-sizes-title-lg">
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter" style={{ fontSize: '40px' }}>
{props.place.name}
</Modal.Title>
</Modal.Header>
<Modal.Body className="show-grid">
<Container style={{ fontSize: '40px' }}>
{Array.isArray(reviews) ? reviews.map((review) => {
return (
<Row className="mt-4">
<a href={review.link}>{review.title}</a>
<div>{review.summary}</div>
<div>{review.content}</div>
</Row>
)
})
: "리뷰가 없습니다."}
</Container>
</Modal.Body>
<Modal.Footer>
<Button block onClick={props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
export default Place;
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { Container, Row, Button, } from 'react-bootstrap';
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([])
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}>
{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;
...@@ -58,6 +58,7 @@ function Search(props) { ...@@ -58,6 +58,7 @@ function Search(props) {
setPagePlace(paginate(association, index, 4)) setPagePlace(paginate(association, index, 4))
} }
setEndPage(Math.floor((association.length / 4))) setEndPage(Math.floor((association.length / 4)))
console.log("7489309484839", endPage)
}, [association, index]) }, [association, index])
...@@ -161,7 +162,7 @@ function Search(props) { ...@@ -161,7 +162,7 @@ function Search(props) {
return ( return (
<Container > <Container >
<Link to="/" className="d-flex justify-content-center"><Image src={ohuh} /></Link> <Link to="/path" className="d-flex justify-content-center"><Image src={ohuh} /></Link>
<Row className="mb-2" className="d-flex justify-content-center"> <Row className="mb-2" className="d-flex justify-content-center">
<Form style={{ width: "90vw" }} onSubmit={handleSubmit}> <Form style={{ width: "90vw" }} onSubmit={handleSubmit}>
<InputGroup size="lg"> <InputGroup size="lg">
...@@ -196,22 +197,17 @@ function Search(props) { ...@@ -196,22 +197,17 @@ function Search(props) {
</Button> : null} </Button> : null}
</Card.Title> </Card.Title>
<Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} /> <Card.Img variant="top" style={{ padding: "5%", width: "100%", height: "340px" }} src={place.img} />
<Card.Body > <Card.Body>
<Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} > <Card.Text style={{ overflow: 'auto', fontSize: '25px', width: '100%', height: "80px" }} >
{place.address} </Card.Text> {place.address} </Card.Text>
<Button variant="info" onClick={() => { <Link to={`/place?id=${index}&place=${place.name}`} >
const showArr = [false, false, false, false] <Button variant="primary"> {place.name} 자세히 살펴보기</Button>
showArr[index] = true </Link>
setShowSet(showArr)
}}>{place.name} 자세히 살펴보기</Button>
<Place place={place} index={index} show={showSet[index]} onHide={() => setShowSet([false, false, false, false])} />
</Card.Body> </Card.Body>
</Card> </Card>
</Col> </Col>
) )
})} })}
</Row> </Row>
<Row className="mt-2 d-flex justify-content-center"> <Row className="mt-2 d-flex justify-content-center">
<Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations> <Paginations index={index} endPage={endPage} handlePage={handlePage}></Paginations>
......
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
Route, Route,
Redirect, Redirect,
} from "react-router-dom"; } from "react-router-dom";
import Place from './Pages/Place';
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
...@@ -22,6 +23,7 @@ ReactDOM.render( ...@@ -22,6 +23,7 @@ ReactDOM.render(
<Switch> <Switch>
<Route exact path="/" component={App} /> <Route exact path="/" component={App} />
<Route path="/search" component={Search} /> <Route path="/search" component={Search} />
<Route path="/place" component={Place} />
<Route path='/signup' component={Signup}/> <Route path='/signup' component={Signup}/>
<Route path='/login' component={Login} /> <Route path='/login' component={Login} />
<PrivateRoute path='/bookmark'><Bookmark/></PrivateRoute> <PrivateRoute path='/bookmark'><Bookmark/></PrivateRoute>
......
...@@ -2,7 +2,6 @@ import Places from '../models/Place.js' ...@@ -2,7 +2,6 @@ import Places from '../models/Place.js'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import fs from 'fs' import fs from 'fs'
import axios from 'axios'; import axios from 'axios';
import { nextTick } from 'process';
const searchPlace = async (req, res, next,) => { const searchPlace = async (req, res, next,) => {
......
...@@ -2,50 +2,44 @@ import Review from '../models/Review.js' ...@@ -2,50 +2,44 @@ import Review from '../models/Review.js'
import cheerio from "cheerio"; import cheerio from "cheerio";
import axios from 'axios'; import axios from 'axios';
const search = async (req, res, next) => { const search = async (req, res) => {
console.log(req.reviews, "1")
try { try {
for (let j = 1; j < 2; j++) { let reviews = []
let reviews = [] console.log(req.reviews, "제발")
// console.log(req.reviews, "제발")/
let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + j
let response1 = await axios.get(url)
let $1 = cheerio.load(response1.data);
$1('.kCrYT').each(async function (i) {
console.log("3")
let title = $1(this).find('h3').text()
let searchParams = new URLSearchParams($1(this).find('a').attr('href'));
let link = searchParams.get("/url?q")
let summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
if (title) {
reviews[i] = { title: title, link: link }
} else if (summary) {
reviews[i - 1] = { ...reviews[i - 1], summary: summary }
reviews = reviews.filter(e => e)
}
})
reviews.forEach((review, i) => {
Review.findOne({ link: review.link }, async function (err, reviewss) {
console.log("4")
if (err) { return res.status(404).send({ error: err }) }
if (reviewss) {
reviewss.keyword = [...reviewss.keyword, req.query.keyword]
reviewss.save()
}
if (!reviewss) {
reviews["keyword"] = [req.query.keyword]
let Reviews = await new Review(review).save()
// console.log(Reviews,"1")
req.reviews.push(Reviews)
}
})
}) let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + `+site%3Atistory.com&start=${Number(req.query.index) * 10}`
} console.log(url)
const a = await Promise.all(req.reviews) let response1 = await axios.get(url)
console.log(a, "2") let $1 = cheerio.load(response1.data);
res.status(200).send({ review: a, db: true }) $1('.kCrYT').each(async function (i) {
console.log("3")
let title = $1(this).find('h3').text()
let searchParams = new URLSearchParams($1(this).find('a').attr('href'));
let link = searchParams.get("/url?q")
let summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
if (title) {
reviews[i] = { title: title, link: link }
} else if (summary) {
reviews[i - 1] = { ...reviews[i - 1], summary: summary }
reviews = reviews.filter(e => e)
}
})
// reviews.forEach(async (review, i) => {
// await Review.updateOne({ link: review.link }, { $push: { bookmark: req.query.keyword } })
// const reviewss = await Review.findOne({ link: review.link })
// if (!reviewss) {
// // reviews["keyword"] = [req.query.keyword]
let newReviews = []
newReviews = [...newReviews, ...reviews]
// await new Review(review).save()
// }
// })
// console.log(req.reviews, "걸러낸 리뷰즈입니다")
// const a = await Promise.all(newReviews)
// console.log(a, "2")
console.log(req.query.index, "인덱스ㅡㅡ")
res.status(200).send({ review: newReviews, db: true, index: Number(req.query.index) + 1 })
} catch (error) { } catch (error) {
console.log("6") console.log("6")
console.log(error) console.log(error)
...@@ -53,22 +47,21 @@ const search = async (req, res, next) => { ...@@ -53,22 +47,21 @@ const search = async (req, res, next) => {
} }
const find = (req, res, next) => { const find = async(req, res, next) => {
console.log("7", req.query.keyword) console.log("7", req.query)
const keyword = req.query.keyword const keyword = req.query.keyword
res.send("dkssud") if (req.body.db) {
// if (req.body.db) { next()
// next() }
// } await Review.find({ keyword: keyword }, function (err, reviews) {
// Review.find({ keyword: keyword }, function (err, reviews) { req.reviews = []
// req.reviews = [] if (reviews) {
// if (reviews) { if (reviews.length > 5) {
// if (reviews.length > 10) { res.status(200).send({ review: reviews, db: true, message: "제발용" })
// res.status(200).send({ review: reviews, db: true }) }
// } req.reviews = reviews
// req.reviews = reviews }
// } next()
// next() })
// })
} }
export default { search, find } export default { search, find }
...@@ -27,4 +27,4 @@ const ReviewSchema = new mongoose.Schema({ ...@@ -27,4 +27,4 @@ const ReviewSchema = new mongoose.Schema({
timestamps: true timestamps: true
}) })
export default mongoose.models.Review || mongoose.model('Review', ReviewSchema) export default mongoose.models.Review || mongoose.model('Review', ReviewSchema)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment