-
Notifications
You must be signed in to change notification settings - Fork 39
[최창환] sprint6 #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
addiescode-sj
merged 5 commits into
codeit-bootcamp-frontend:React-최창환
from
3rdflr:React-최창환-sprint6
Jun 19, 2025
The head ref may contain hidden characters: "React-\uCD5C\uCC3D\uD658-sprint6"
Merged
[최창환] sprint6 #181
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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,3 @@ | ||
| :root { | ||
| background-color: var(--content-bg); | ||
| } |
This file contains hidden or 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,106 +1,18 @@ | ||
| import Nav from "./components/Nav"; | ||
| import { BrowserRouter, Routes, Route } from 'react-router-dom'; | ||
| import Items from './Pages/Items/Items.jsx'; | ||
| import AddItem from './Pages/AddItem/AddItem.jsx'; | ||
|
|
||
| import styles from "./css/App.module.css"; | ||
| import Header from "./components/Header"; | ||
| import DropDown from "./components/DropDown"; | ||
| import ProductList from "./components/ProductList"; | ||
| import Button from "./components/Button"; | ||
| import SearchItem from "./components/SearchItem"; | ||
| import Pagination from "./components/Pagination"; | ||
|
|
||
| import { getProducts } from "./api/ProductApi"; | ||
| import { useScreenSize } from "./utils/useScreenSize"; | ||
| import { useState, useEffect } from "react"; | ||
| import './App.css' | ||
|
|
||
| function App() { | ||
| const [search, setSearch] = useState(""); | ||
| const [order, setOrder] = useState("recent"); | ||
| const [currPage, setCurrPage] = useState(1); | ||
| const [totalProducts, setTotalProducts] = useState(0); | ||
| const [bestProduct, setBestProduct] = useState(4); | ||
| const [allProduct, setAllProduct] = useState(10); | ||
| const [isBestProduct, setIsBestProduct] = useState(true); | ||
|
|
||
| const screenSize = useScreenSize(); | ||
|
|
||
| const handleSearch = (e) => { | ||
| setSearch(e.target.value); | ||
| }; | ||
|
|
||
| const handleOrder = (selectedOrder) => { | ||
| setOrder(selectedOrder); | ||
| }; | ||
|
|
||
| const handlePage = (newPage) => { | ||
| setCurrPage(newPage); | ||
| }; | ||
|
|
||
| const getTotalProducts = async () => { | ||
| try { | ||
| const { totalCount } = await getProducts({ | ||
| orderBy: order, | ||
| keyword: search, | ||
| }); | ||
| setTotalProducts(totalCount); | ||
| } catch (error) { | ||
| console.error("전체 상품 수를 불러오는 중 오류:", error); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| let newPageSize; | ||
| if (screenSize === "lg") { | ||
| setBestProduct(4); | ||
| newPageSize = 10; | ||
| } else if (screenSize === "md") { | ||
| setBestProduct(2); | ||
| newPageSize = 6; | ||
| } else { | ||
| setBestProduct(1); | ||
| newPageSize = 4; | ||
| } | ||
| if (newPageSize !== allProduct) { | ||
| const firstItemOfCurrentPage = (currPage - 1) * allProduct; | ||
| const newPage = Math.floor(firstItemOfCurrentPage / newPageSize) + 1; | ||
| setCurrPage(newPage); | ||
| setAllProduct(newPageSize); | ||
| } | ||
| }, [screenSize, currPage, allProduct]); | ||
|
|
||
| useEffect(() => { | ||
| getTotalProducts(); | ||
| }, []); | ||
| return ( | ||
| <> | ||
| <Nav /> | ||
| <div className={styles.content}> | ||
| <Header text={"베스트 상품"} /> | ||
| <ProductList | ||
| orderBy={"favorite"} | ||
| pageSize={bestProduct} | ||
| isBestProduct={isBestProduct} | ||
| /> | ||
| <div className={styles.headers}> | ||
| <Header text={"전체 상품"} /> | ||
| <SearchItem value={search} onChange={handleSearch} /> | ||
| <Button href={"#"} buttonText={"상품등록하기"} /> | ||
| <DropDown onChangeOrder={handleOrder} /> | ||
| </div> | ||
| <ProductList | ||
| orderBy={order} | ||
| pageSize={allProduct} | ||
| keyword={search} | ||
| page={currPage} | ||
| /> | ||
| <Pagination | ||
| currPage={currPage} | ||
| totalProducts={totalProducts} | ||
| pageSize={allProduct} | ||
| handlePage={handlePage} | ||
| /> | ||
| </div> | ||
| </> | ||
| <BrowserRouter> | ||
| <Routes> | ||
| <Route path="/" element={<Items />} /> | ||
| <Route path='additem' element={<AddItem />}/> | ||
| </Routes> | ||
| </BrowserRouter> | ||
| ); | ||
| } | ||
|
|
||
| export default App; | ||
| export default App |
This file contains hidden or 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,144 @@ | ||
| import DeleteImg from '../../assets/input/delete.svg' | ||
|
|
||
| import { useState } from "react"; | ||
|
|
||
| import Nav from "../../components/Nav/Nav"; | ||
| import Content from "../../components/Content/Content"; | ||
| import Header from '../../components/Header/Header'; | ||
| import Button from '../../components/Button/Button'; | ||
| import FormImg from "../../components/Form/FormImg"; | ||
| import FormInput from "../../components/Form/FormInput"; | ||
|
|
||
| import styles from './AddItem.module.css'; | ||
|
|
||
| const INITIAL_VALUES = { | ||
| name: '', | ||
| description: '', | ||
| price: '', | ||
| tags:[], | ||
| tagInput:'', | ||
| images: null, | ||
| }; | ||
|
|
||
|
|
||
| function AddItem() { | ||
| const [values, setValues] = useState(INITIAL_VALUES) | ||
| const [isComposing, setIsComposing] = useState(false); | ||
|
|
||
| const handleChange = (name, value) => { | ||
| setValues((prevValues) => ({ | ||
| ...prevValues, | ||
| [name]: value, | ||
| })); | ||
| }; | ||
|
|
||
|
|
||
| const handleTagInputKeyDown = (e) => { | ||
| if (e.key === 'Enter' && !isComposing) { | ||
| e.preventDefault(); | ||
|
|
||
| const newTag = values.tagInput.trim(); | ||
|
|
||
| if (newTag !== '') { | ||
|
|
||
| if (!values.tags.includes(`#${newTag}`)) { | ||
| setValues((prev) => ({ | ||
| ...prev, | ||
| tags: [...prev.tags, `#${newTag}`], | ||
| tagInput:'', | ||
| })) | ||
| } else { | ||
| setValues((prev) => ({ | ||
| ...prev, | ||
| tagInput:'', | ||
| })) | ||
| } | ||
| }} | ||
| }; | ||
|
|
||
|
|
||
| const handleRemoveTag = (tagToRemove) => { | ||
| setValues((prevValues) => ({ | ||
| ...prevValues, | ||
| tags: prevValues.tags.filter((tag) => tag !== tagToRemove), | ||
| })); | ||
| }; | ||
|
|
||
| const handleCompositionStart = () => { | ||
| setIsComposing(true); | ||
| }; | ||
|
|
||
|
|
||
| const handleCompositionEnd = () => { | ||
| setIsComposing(false); | ||
| }; | ||
|
|
||
| const isSubmitDisabled = !values.name || !values.description || values.price <= 0 || !values.images; | ||
|
|
||
| return( | ||
| <> | ||
| <Nav /> | ||
| <Content> | ||
| <form className={styles.form}> | ||
| <div className={styles.headers}> | ||
| <Header type={'h1'} text={'상품 등록하기'}/> | ||
| <Button type={'submit'} href={'#'} buttonText={'등록'} disabled={isSubmitDisabled}/> | ||
| </div> | ||
| <FormImg label={'상품 이미지'} name="images" value={values.images} | ||
| initialPreview={''} onChange={handleChange}/> | ||
| <FormInput | ||
| label={'상품명'} | ||
| name="name" | ||
| type={'text'} | ||
| placeholder={'상품명을 입력해주세요'} | ||
| value={values.name} | ||
| onChange={handleChange} /> | ||
| <FormInput | ||
| label={'상품 소개'} | ||
| name="description" | ||
| type={'textarea'} | ||
| placeholder={'상품 소개를 입력해주세요'} | ||
| value={values.description} | ||
| onChange={handleChange} /> | ||
| <FormInput | ||
| label={'판매가격'} | ||
| name="price" | ||
| type={'number'} | ||
| placeholder={'판매 가격을 입력해주세요'} | ||
| value={values.price} | ||
| onChange={handleChange} /> | ||
| <FormInput | ||
| label={'태그'} | ||
| name="tagInput" | ||
| type={'text'} | ||
| placeholder={'태그를 입력해주세요'} | ||
| value={values.tagInput} | ||
| onChange={handleChange} | ||
| onKeyDown={handleTagInputKeyDown} | ||
| onCompositionStart={handleCompositionStart} | ||
| onCompositionEnd={handleCompositionEnd} | ||
|
|
||
| /> | ||
| {values.tags.length > 0 && ( | ||
| <div className={styles.taglistContainer}> | ||
| {values.tags.map((tag, index) => ( | ||
| <div key={index} className={styles.tagItem}> | ||
| <span>{tag}</span> | ||
| <button | ||
| type="button" | ||
| onClick={() => handleRemoveTag(tag)} | ||
| className={styles.removeTagButton} | ||
| > | ||
| <img src={DeleteImg} alt="선택해제" /> | ||
| </button> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </form> | ||
| </Content> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default AddItem | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
상태를 객체로 관리하게되면 값 하나만 바뀌어도 전체 객체가 새로운 참조로 변경되기때문에 불필요한 리렌더링이 발생해요. 개별적으로 state를 만들어서 관리하시는게 좋습니다 :)