-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,10 +1,89 @@ | ||
import React, { useState } from 'react' | ||
import React, { useState,useEffect, useCallback } from 'react'; | ||
import SearchIcon from '../../../assets/images/searchicon.png'; | ||
import { Link } from 'react-router-dom'; | ||
import ItemCard from './ItemCard'; | ||
import { getProducts } from '../../../api/itemApi'; | ||
|
||
const getPageSize = () => { | ||
const width = window.innerWidth; | ||
if(width < 767) { | ||
return 4; | ||
} else if (width < 1280) { | ||
return 6; | ||
} else { | ||
return 10; | ||
} | ||
}; | ||
|
||
function AllItem() { | ||
const [itemList, setItemList] = useState([]); | ||
const [pageSize, setPageSize] = useState(getPageSize()); | ||
const [order, setOrder] = useState('recent'); | ||
const [selected, setSelected] = useState('최신순'); | ||
const [dropdownOpen, setDropdownOpen] = useState(false); | ||
|
||
const handleNewestClick = () => { | ||
setOrder('recent'); | ||
setSelected('최신순'); | ||
setDropdownOpen(false); | ||
}; | ||
|
||
const handleFavoriteClick = () => { | ||
setOrder('favorite'); | ||
setSelected('좋아요순'); | ||
setDropdownOpen(false); | ||
}; | ||
|
||
const fetchSortedData = useCallback(async () => { | ||
const products = await getProducts({ orderBy:order, pageSize}); | ||
setItemList(products.list); | ||
},[order, pageSize]); | ||
useEffect(() => { | ||
const handleResize = () => { | ||
setPageSize(getPageSize()); | ||
}; | ||
window.addEventListener("resize",handleResize); | ||
fetchSortedData(); | ||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
}; | ||
},[fetchSortedData]) | ||
|
||
const toggleDropdown = () => { | ||
setDropdownOpen(!dropdownOpen); | ||
}; | ||
|
||
return ( | ||
<div>AllItem</div> | ||
<div className="allitem"> | ||
<div className="allitem-container"> | ||
<div className="allitem-header"> | ||
<h3 className='list-title'>전체 상품</h3> | ||
<div className="allitem-header-right"> | ||
<div className="search-container"> | ||
<img className="search-icon" src={SearchIcon} alt="검색"></img> | ||
<input className='search-input' placeholder='검색할 상품을 입력해주세요'></input> | ||
</div> | ||
<Link className="additem-button" to="/additem">상품 등록하기</Link> | ||
<div className="dropdown-container"> | ||
<button className="dropdown-button" onClick={toggleDropdown}> | ||
{selected} <span className="dropdown-arrow">▼</span> | ||
</button> | ||
<ul className={`dropdown-menu ${dropdownOpen ? 'show' : ''}`}> | ||
<li className="dropdown-option" onClick={handleNewestClick}>최신순</li> | ||
<li className="dropdown-option" onClick={handleFavoriteClick}>좋아요순</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="card-list"> | ||
{itemList?.map((item) => ( | ||
<ItemCard key={item.id} item={item}/> | ||
))} | ||
</div> | ||
|
||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default AllItem | ||
export default AllItem; |