Commit cdfec30f authored by kusang96's avatar kusang96
Browse files

Merge remote-tracking branch 'origin/sangho' into ourMaster

parents 74f2c338 97417b55
...@@ -24,6 +24,7 @@ function App() { ...@@ -24,6 +24,7 @@ function App() {
<Route path="/login" component={Login} /> <Route path="/login" component={Login} />
<Route path="/signup" component={Signup} /> <Route path="/signup" component={Signup} />
<Route path="/product/:productId" component={Product} /> <Route path="/product/:productId" component={Product} />
<Route path="/categories/:main/:sub" component={ProductsList} />
<Route path="/categories/:main" component={ProductsList} /> <Route path="/categories/:main" component={ProductsList} />
<Route path="/admin" component={Admin} /> <Route path="/admin" component={Admin} />
<Route path="/regist" component={ProductRegist} /> <Route path="/regist" component={ProductRegist} />
......
...@@ -16,6 +16,7 @@ function SubNav() { ...@@ -16,6 +16,7 @@ function SubNav() {
const url = ele.toLowerCase() const url = ele.toLowerCase()
list.push( list.push(
<Nav.Link as={Link} to={`/categories/${url}`}>{ele}</Nav.Link> <Nav.Link as={Link} to={`/categories/${url}`}>{ele}</Nav.Link>
//categories/${SubNav.url}/&{url}
) )
}) })
setCategoriesDiv(list) setCategoriesDiv(list)
......
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import ListCard from '../Components/ListCard'; import ListCard from '../Components/ListCard';
import Pagination from '../Components/Pagination';
import axios from 'axios'; import axios from 'axios';
import catchError from '../utils/catchErrors'; import catchError from '../utils/catchErrors';
import { isAuthenticated } from '../utils/auth'; import { Container, Row, Col, Form, FormControl, Button, Dropdown, ButtonGroup } from 'react-bootstrap';
import { Container, Row, Col, Form, FormControl, Button, Dropdown } from 'react-bootstrap';
function ProductsList({ match }) { function ProductsList({ match }) {
const [mainCategory, setMainCategory] = useState(match.params.main.toUpperCase()) const [mainCategory, setMainCategory] = useState(match.params.main.toUpperCase())
const [subcategory, setSubcategory] = useState([]) const [subcategory, setSubcategory] = useState([])
const [productlist, setProductlist] = useState([]) const [productlist, setProductlist] = useState([])
const [sub, setSub] = useState([])
const [error, setError] = useState('') const [error, setError] = useState('')
// const user=isAuthenticated() useEffect(() => {
setMainCategory(match.params.main.toUpperCase())
}, [match.params.main])
useEffect(() => { useEffect(() => {
getSubsCategories() getSubsCategories([])
getProductlist() getProductlist()
}, [mainCategory]) }, [mainCategory])
useEffect(() => {
setMainCategory(match.params.main.toUpperCase())
}, [match.params.main])
function handleSearch() { function handleSearch() {
} }
// async function handleClick(subCategory) { async function getSubsCategories([]) {
// try {
// const response = await axios.get(`/api/product/getproduct/${subCategory}`)
// console.log("response.data=", response.data)
// setProductlist(response.data)
// } catch (error) {
// catchErrors(error, setError)
// }
// }
// function handleSubmit(e) {
// e.preventDefault()
// }
async function getSubsCategories() {
try { try {
const response = await axios.get(`/api/categories/sub/${mainCategory}`) const response = await axios.get(`/api/categories/sub/${mainCategory}`)
console.log("sub", response.data) setSubcategory(Object.values(response.data)[0])
setSubcategory(response.data)
console.log("response data=", response.data)
console.log("object value=", Object.values(response.data));
} catch (error) { } catch (error) {
catchError(error, setError) catchError(error, setError)
} }
...@@ -58,21 +43,7 @@ function ProductsList({ match }) { ...@@ -58,21 +43,7 @@ function ProductsList({ match }) {
const response = await axios.get(`/api/product/getproduct/${mainCategory}`) const response = await axios.get(`/api/product/getproduct/${mainCategory}`)
console.log("response.data=", response.data) console.log("response.data=", response.data)
setProductlist(response.data) setProductlist(response.data)
} catch (error) {
catchError(error, setError)
}
}
function handleClick(e) {
e.preventDefault()
return getsubproductlist()
}
async function getsubproductlist() {
try {
const response = await axios.get(`/api/product/getproduct/${subcategory}`)
console.log("response.data sub=", response.data)
setProductlist(response.data)
} catch (error) { } catch (error) {
catchError(error, setError) catchError(error, setError)
} }
...@@ -80,7 +51,6 @@ function ProductsList({ match }) { ...@@ -80,7 +51,6 @@ function ProductsList({ match }) {
return ( return (
<div> <div>
{console.log("main=", mainCategory)}
<style type="text/css"> <style type="text/css">
{` {`
a, a:hover, a:active { a, a:hover, a:active {
...@@ -100,19 +70,17 @@ function ProductsList({ match }) { ...@@ -100,19 +70,17 @@ function ProductsList({ match }) {
`} `}
</style> </style>
<Container> <Container>
<Row className="justify-content-center" > <Row className="justify-content-center">
<Col sm={10} xs={12} > <Col sm={10} xs={12} >
<h1 style={{ fontSize: "3rem" }} className="text-center">{mainCategory}</h1> <h1 style={{ fontSize: "3rem" }} className="text-center">{mainCategory}</h1>
<div className="text-center"> <div className="text-center">
<Button className="m-1" onClick={getProductlist}>ALL</Button> <ButtonGroup className="d-flex flex-wrap" variant="outline-light secondary">
{subcategory.map((ele) => ( {subcategory.map(el => <Button className="m-1" variant="secondary">{el}</Button>)}
<Button className="m-1" onClick={(ele) => handleClick(ele)}>{ele}</Button> </ButtonGroup>
))}
</div> </div>
</Col> </Col>
</Row> </Row>
<Row className="justify-content-end mx-0 my-5"> <Row className="justify-content-end mx-0 my-5">
{/* <Form as={Row} onSubmit={handleSubmit} className="justify-content-end mx-0"> */}
<Dropdown> <Dropdown>
<Dropdown.Toggle className="mx-2">정렬</Dropdown.Toggle> <Dropdown.Toggle className="mx-2">정렬</Dropdown.Toggle>
<Dropdown.Menu> <Dropdown.Menu>
...@@ -128,7 +96,6 @@ function ProductsList({ match }) { ...@@ -128,7 +96,6 @@ function ProductsList({ match }) {
<img src="/icon/search.svg" width="20" height="20" /> <img src="/icon/search.svg" width="20" height="20" />
</Button> </Button>
</Form> </Form>
{/* </Form> */}
</Row> </Row>
<Row md={8} sm={12} className="justify-content-center m-2"> <Row md={8} sm={12} className="justify-content-center m-2">
{productlist.map(pro => ( {productlist.map(pro => (
...@@ -148,7 +115,6 @@ function ProductsList({ match }) { ...@@ -148,7 +115,6 @@ function ProductsList({ match }) {
))} ))}
</Row> </Row>
</Container> </Container>
{/* <Pagination postsPerPage={postsPerPage} totalPosts={posts.length} paginate={paginate} /> */}
</div> </div>
) )
} }
......
import Category from "../schemas/Category.js"; import Category from "../schemas/Category.js";
const getCategory = async (req, res) => { const getCategory = async (req, res) => {
console.log("dsadd=")
try { try {
const category = await Category.find({}, {_id: 0}) const category = await Category.find({}, { _id: 0 })
console.log("main= ", category);
res.json(category) res.json(category)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
...@@ -11,16 +11,60 @@ const getCategory = async (req, res) => { ...@@ -11,16 +11,60 @@ const getCategory = async (req, res) => {
} }
} }
const getSubCategory=(req,res)=>{ const getSubCategory = async (req, res) => {
console.log("req.params=", req.params);
const { sub } = req.params
try {
const subcategory = await Category.findOne({}, { _id: 0}).select(`${sub}`)
console.log("sub= ",subcategory);
res.json(subcategory);
} catch (error) {
res.status(500).send('카테고리를 불러오지 못했습니다.')
}
} }
const getsubId=(req,res,next,sub)=>{ const getToHome = async (res, req) => {
// const subcategory = await category.find({}) try {
console.log('sub=',sub) const bestProduct = await Product.find({}).sort({ purchase: 1 }).limit(6)
const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
console.log("best=", bestProduct)
console.log("new=", newProduct)
res.json(bestProduct, newProduct)
} catch {
res.status(500).send('상품을 불러오지 못했습니다.')
}
}
const getsubId = async (req, res, next, ele) => {
try {
const sub = await Category.find({ele})
if (!sub) {
res.status(404).send('카테고리가 존재하지 않습니다.')
} req.category = sub
req.subcategory = sub
next()
}
catch (error) {
console.log(error);
res.status(500).send('카테고리를 불러오지 못했습니다.')
}
next() next()
} }
// const userById = async (req, res, next, id) => {
// try {
// const user = await User.findById(id)
// if (!user) {
// res.status(404).send('사용자를 찾을 수 없습니다')
// }
// req.account = user
// next()
// } catch (error) {
// console.log(error);
// res.status(500).send('사용자 아이디 검색 실패')
// }
// }
export default { getCategory , getsubId, getSubCategory} export default { getCategory, getsubId, getSubCategory, getToHome }
\ No newline at end of file \ No newline at end of file
...@@ -92,4 +92,4 @@ const subcategoryId = async (req, res, next, subcategory) => { ...@@ -92,4 +92,4 @@ const subcategoryId = async (req, res, next, subcategory) => {
} }
} }
export default { imageUpload, regist, getToHome, getAll, categoryId, getlist, subcategoryId, subgetlist } export default { imageUpload, regist, getToHome, getAll, categoryId, getlist, subcategoryId, subgetlist }
\ No newline at end of file
...@@ -9,6 +9,6 @@ router.route('/main') ...@@ -9,6 +9,6 @@ router.route('/main')
router.route('/sub/:sub') router.route('/sub/:sub')
.get(categoryCtrl.getSubCategory) .get(categoryCtrl.getSubCategory)
router.param('sub',categoryCtrl.getsubId) // router.param('sub',categoryCtrl.getsubId)
export default router export default router
\ 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