-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from baehyunji/React-배현지-sprint7
[배현지] Sprint7
- Loading branch information
Showing
53 changed files
with
1,449 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
export async function getProducts() { | ||
const response = await fetch("https://panda-market-api.vercel.app/products/"); | ||
if (!response.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다"); | ||
} | ||
const body = await response.json(); | ||
return body; | ||
import axios from "axios"; | ||
|
||
const instance = axios.create({ | ||
baseURL: "https://panda-market-api.vercel.app", | ||
timeout: 3000, | ||
}); | ||
|
||
export async function getProducts(params = {}) { | ||
const query = new URLSearchParams(params).toString(); | ||
const res = await instance.get(`/products?${query}`); | ||
return res.data; | ||
} | ||
|
||
export async function getProduct(productId) { | ||
const res = await instance.get(`/products/${productId}`); | ||
return res.data; | ||
} | ||
|
||
export async function getComment({ productId, params }) { | ||
const query = new URLSearchParams(params).toString(); | ||
const res = await instance.get(`/products/${productId}/comments?${query}`); | ||
return res.data; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import ProductListContainer from "../ui/ProductListContainer"; | ||
import { useState, useEffect } from "react"; | ||
import { getProducts } from "../../api"; | ||
|
||
function ListContainer() { | ||
const [products, setProducts] = useState([]); | ||
const [order, setOrder] = useState("createdAt"); | ||
const [favoriteProducts, setFavoriteProducts] = useState([]); | ||
const [searchText, setSearchText] = useState(""); | ||
|
||
const sortedProducts = products.sort((a, b) => b[order] - a[order]); | ||
|
||
const loadList = async () => { | ||
try { | ||
const { list } = await getProducts(); | ||
setProducts(list); | ||
} catch (e) { | ||
if (e.response) { | ||
console.log(e.response.status); | ||
console.log(e.response.data); | ||
} else { | ||
console.log("리퀘스트가 실패했습니다."); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
loadList(); | ||
}, [order]); | ||
|
||
useEffect(() => { | ||
const sortedFavoriteProducts = [...products].sort( | ||
(a, b) => b.favoriteCount - a.favoriteCount | ||
); | ||
setFavoriteProducts(sortedFavoriteProducts); | ||
}, [products]); | ||
|
||
const handleToggleClick = (option) => { | ||
if (option === "newest") { | ||
setOrder("createdAt"); | ||
} else if (option === "likes") { | ||
setOrder("favoriteCount"); | ||
} | ||
}; | ||
|
||
return ( | ||
<ProductListContainer | ||
favoriteProducts={favoriteProducts} | ||
sortedProducts={sortedProducts} | ||
searchText={searchText} | ||
handleToggleClick={handleToggleClick} | ||
setSearchText={setSearchText} | ||
/> | ||
); | ||
} | ||
|
||
export default ListContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// import { getProducts } from "../../api"; | ||
// import PaginationBar from "../ui/PaginationBar"; | ||
|
||
function PaginationContainer() { | ||
return; | ||
} | ||
|
||
export default PaginationContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import ProductDetails from "../ui/ProductDetails"; | ||
import { useParams } from "react-router-dom"; | ||
import { useEffect, useState } from "react"; | ||
import { getProduct, getComment } from "../../api"; | ||
import CommentDetails from "../ui/CommentDetails"; | ||
|
||
function ProductDetailsContainer() { | ||
const { productId } = useParams(); | ||
const [product, setProduct] = useState(null); | ||
const [comments, setComments] = useState([]); | ||
|
||
useEffect(() => { | ||
const detailedProduct = async () => { | ||
try { | ||
const productData = await getProduct(productId); | ||
setProduct(productData); | ||
} catch (e) { | ||
if (e.response) { | ||
console.log(e.response.status); | ||
console.log(e.response.data); | ||
} else { | ||
console.log("리퀘스트가 실패했습니다."); | ||
} | ||
} | ||
}; | ||
|
||
const detailedComment = async () => { | ||
const params = { limit: 3 }; | ||
try { | ||
const commentData = await getComment({ productId, params }); | ||
setComments(commentData.list); | ||
} catch (e) { | ||
if (e.response) { | ||
console.log(e.response.status); | ||
console.log(e.response.data); | ||
} else { | ||
console.log("리퀘스트가 실패했습니다."); | ||
} | ||
} | ||
}; | ||
detailedComment(); | ||
detailedProduct(); | ||
}, [productId]); | ||
|
||
if (!product) { | ||
return <div>로딩중</div>; | ||
} | ||
|
||
return ( | ||
<> | ||
<ProductDetails product={product} /> | ||
<CommentDetails comments={comments} /> | ||
</> | ||
); | ||
} | ||
|
||
export default ProductDetailsContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import RagistrationForm from "../ui/RagistrationForm"; | ||
import { useState } from "react"; | ||
|
||
function RagistrationContainer() { | ||
const [values, setValues] = useState({ | ||
imageFile: null, | ||
productName: "", | ||
productDescription: "", | ||
productPrice: "", | ||
productTag: "", | ||
}); | ||
|
||
const handleChange = (name, value) => { | ||
setValues((prevValues) => ({ | ||
...prevValues, | ||
[name]: value, | ||
})); | ||
}; | ||
|
||
const handleInputChange = (e) => { | ||
const { name, value } = e.target; | ||
handleChange(name, value); | ||
}; | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
setValues({ | ||
imageFile: null, | ||
productName: "", | ||
productDescription: "", | ||
productPrice: "", | ||
productTag: "", | ||
}); | ||
}; | ||
return ( | ||
<RagistrationForm | ||
values={values} | ||
handleChange={handleChange} | ||
handleInputChange={handleInputChange} | ||
handleSubmit={handleSubmit} | ||
/> | ||
); | ||
} | ||
|
||
export default RagistrationContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import ToggleMenu from "../ui/ToggleMenu"; | ||
|
||
function ToggleMenuContainer({ onClick }) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const onToggleMenu = useCallback((e) => { | ||
e.stopPropagation(); | ||
setIsOpen((nextIsOpen) => !nextIsOpen); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!isOpen) return; | ||
|
||
const handleClickOutside = () => setIsOpen(false); | ||
window.addEventListener("click", handleClickOutside); | ||
|
||
return () => { | ||
window.removeEventListener("click", handleClickOutside); | ||
}; | ||
}, [isOpen]); | ||
return ( | ||
<ToggleMenu onToggleMenu={onToggleMenu} onClick={onClick} isOpen={isOpen} /> | ||
); | ||
} | ||
|
||
export default ToggleMenuContainer; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.