Commit 488f656f authored by kusang96's avatar kusang96
Browse files

검색 기능 구현

parent e776b57a
...@@ -7,6 +7,7 @@ import catchError from '../utils/catchErrors'; ...@@ -7,6 +7,7 @@ import catchError from '../utils/catchErrors';
import { Row, Form, FormControl, Button, Container } from 'react-bootstrap'; import { Row, Form, FormControl, Button, Container } from 'react-bootstrap';
function Admin() { function Admin() {
const [search, setSearch] = useState({ word: '' })
const [productlist, setProductlist] = useState([]) const [productlist, setProductlist] = useState([])
const [error, setError] = useState('') const [error, setError] = useState('')
...@@ -24,12 +25,21 @@ function Admin() { ...@@ -24,12 +25,21 @@ function Admin() {
} }
} }
function handleSearch() { function handleChange(event) {
setSearch({ word: event.target.value })
} }
function handleSubmit(e) { async function handleSubmit(e) {
e.preventDefault() e.preventDefault()
try {
setError('')
const response = await axios.get(`/api/product/getproduct/all?product=${search.word}`)
console.log("response.data=", response.data)
setProductlist(response.data)
e.target.childNodes[0].value = ''
} catch (error) {
catchError(error, setError)
}
} }
return ( return (
...@@ -48,7 +58,7 @@ function Admin() { ...@@ -48,7 +58,7 @@ function Admin() {
`} `}
</style> </style>
<Row as={Form} onSubmit={handleSubmit} className="justify-content-end mx-0 my-5"> <Row as={Form} onSubmit={handleSubmit} className="justify-content-end mx-0 my-5">
<FormControl type="text" placeholder="Search" style={{ width: "13rem" }} /> <FormControl type="text" onChange={handleChange} placeholder="Search" style={{ width: "13rem" }} />
<Button type="submit" className="px-2"> <Button type="submit" className="px-2">
<img src="icon/search.svg" width="20" height="20" /> <img src="icon/search.svg" width="20" height="20" />
</Button> </Button>
......
...@@ -9,7 +9,7 @@ import catchErrors from '../utils/catchErrors'; ...@@ -9,7 +9,7 @@ import catchErrors from '../utils/catchErrors';
function Payment({ match, location }) { function Payment({ match, location }) {
const [cart, setCart] = useState([]) const [cart, setCart] = useState([])
const [order, setOrder] = useState({products: []}) const [order, setOrder] = useState({ products: [] })
const [userData, setUserData] = useState({}) const [userData, setUserData] = useState({})
const [error, setError] = useState() const [error, setError] = useState()
const [paymentWay, setPaymentWay] = useState([]) const [paymentWay, setPaymentWay] = useState([])
...@@ -201,11 +201,7 @@ function Payment({ match, location }) { ...@@ -201,11 +201,7 @@ function Payment({ match, location }) {
return <Redirect to={'/kakao'} /> return <Redirect to={'/kakao'} />
} }
return ( return (
<div>
{/* {console.log(order)} */}
<Container> <Container>
<h3 className="my-5 font-weight-bold text-center">주문/결제</h3> <h3 className="my-5 font-weight-bold text-center">주문/결제</h3>
<div> <div>
...@@ -300,7 +296,6 @@ function Payment({ match, location }) { ...@@ -300,7 +296,6 @@ function Payment({ match, location }) {
<Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} onClick={paymentCompleted} block>결제완료</Button> <Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} onClick={paymentCompleted} block>결제완료</Button>
</div> </div>
</Container> </Container>
</div>
) )
} }
......
...@@ -34,17 +34,17 @@ function ProductsList({ match }) { ...@@ -34,17 +34,17 @@ function ProductsList({ match }) {
}, [mainCategory]) }, [mainCategory])
function handleChange(event) { function handleChange(event) {
console.log('handle change', event.target.value)
setSearch({ word: event.target.value }) setSearch({ word: event.target.value })
} }
async function handleSearch(event) { async function handleSearch(e) {
event.preventDefault() e.preventDefault()
try { try {
setError('') setError('')
const response = await axios.post(`/api/product/getproduct/main/${mainCategory}`, search) const response = await axios.get(`/api/product/getproduct/main/${mainCategory}?product=${search.word}`)
console.log("response.data=", response.data) console.log("response.data=", response.data)
setProductlist(response.data) setProductlist(response.data)
e.target.childNodes[0].value = ''
} catch (error) { } catch (error) {
catchError(error, setError) catchError(error, setError)
} }
......
...@@ -43,8 +43,17 @@ const getToHome = async (req, res) => { ...@@ -43,8 +43,17 @@ const getToHome = async (req, res) => {
const getAll = async (req, res) => { const getAll = async (req, res) => {
try { try {
if (req.query.product) {
const productslist = await Product.find({ pro_name: { $regex: new RegExp(req.query.product) } }).sort({ createdAt: -1 })
if (productslist.length == 0) {
res.status(404).send('상품을 찾을 수 없습니다.')
} else {
res.json(productslist)
}
} else {
const productslist = await Product.find({}).sort({ createdAt: -1 }) const productslist = await Product.find({}).sort({ createdAt: -1 })
res.json(productslist) res.json(productslist)
}
} catch (error) { } catch (error) {
res.status(500).send('상품을 불러오지 못했습니다.') res.status(500).send('상품을 불러오지 못했습니다.')
} }
...@@ -68,16 +77,20 @@ const subname = async (req, res) => { ...@@ -68,16 +77,20 @@ const subname = async (req, res) => {
} }
const categoryId = async (req, res, next, category) => { const categoryId = async (req, res, next, category) => {
console.log("req=",req.body) console.log("req=", req.query.product)
// const { search } = req.body
// console.log("다시 실행 server search=", search)
try { try {
if (req.query.product) {
const productslist = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } })
if (productslist.length == 0) {
console.log('ds')
res.status(404).send('상품을 찾을 수 없습니다.')
} else {
req.productslist = productslist
}
} else {
const productslist = await Product.find({ main_category: category }) const productslist = await Product.find({ main_category: category })
// if (!productslist) {
// res.status(404).send('상품을 찾을 수 없습니다.')
// }
req.productslist = productslist req.productslist = productslist
console.log("nononono", req.productslist) }
next() next()
} catch (error) { } catch (error) {
res.status(500).send('상품을 불러오지 못했습니다.') res.status(500).send('상품을 불러오지 못했습니다.')
...@@ -95,7 +108,7 @@ const subcategoryId = async (req, res, next, subname) => { ...@@ -95,7 +108,7 @@ const subcategoryId = async (req, res, next, subname) => {
_id: 'nothing', _id: 'nothing',
pro_name: '상품준비중', pro_name: '상품준비중',
price: 0, price: 0,
main_imgUrl:'' main_imgUrl: ''
} }
console.log("findSubname2222=", findSubname) console.log("findSubname2222=", findSubname)
res.send(findSubname) res.send(findSubname)
......
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