-
Notifications
You must be signed in to change notification settings - Fork 35
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
[DOTORI] 2주차 첫번째 PR #82
Changes from all commits
e7746bc
39401cb
c7fc0db
1931cae
d7e799f
f063227
c5ba58b
daa9354
f2df099
a249a85
7081dc3
5160b7c
3dabc6c
f2c0cc6
321dce2
c58c3ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
import { useContext, memo } from "react"; | ||
import { InsertCoinContext, CoinContext } from "context"; | ||
import { SetInsertCoinContext, SetCoinContext, HistoryContext } from "context"; | ||
import { Button } from "components"; | ||
|
||
function Coin({ unit, count }) { | ||
const { selectCoin } = useContext(CoinContext); | ||
const { insertCoin, setInsertCoin } = useContext(InsertCoinContext); | ||
const selectCoin = useContext(SetCoinContext); | ||
const setInsertCoin = useContext(SetInsertCoinContext); | ||
const { addHistory } = useContext(HistoryContext); | ||
|
||
const clickCoin = () => { | ||
const handleCoinClick = () => { | ||
if (!count) return; | ||
selectCoin(unit, count); | ||
setInsertCoin(insertCoin + unit); | ||
selectCoin(unit); | ||
setInsertCoin((prevCoin) => prevCoin + unit); | ||
addHistory("INSERT_COIN", { | ||
coin: unit, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button color="yellow" size="medium" onClick={clickCoin}> | ||
<Button color="yellow" size="medium" onClick={handleCoinClick}> | ||
<strong>{unit}</strong> | ||
</Button> | ||
<span>{count}</span> | ||
</> | ||
); | ||
} | ||
|
||
const areEqual = (prevProps, nextProps) => prevProps.count === nextProps.count; | ||
const areEqual = (prevProps, nextProps) => { | ||
return prevProps.count === nextProps.count; | ||
}; | ||
|
||
const MemoizedCoin = memo(Coin, areEqual); | ||
|
||
export { MemoizedCoin }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { VendProduct } from "./VendProduct"; | |
|
||
const StyledVendProductContainer = styled.ul` | ||
display: grid; | ||
grid-template-columns: 25% 25% 25% 25%; | ||
grid-template-columns: repeat(4, 1fr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
li { | ||
background-color: #f2f2f2; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useCallback } from "react"; | ||
|
||
const { createContext, useReducer } = require("react"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일관성을 위해 import로 변경하면 어떨까요?? ✋ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자동완성 기능을 사용해서 몰랐는데 확인을 꼭 해야겠네요..! 감사합니다 🙏 |
||
|
||
function historyReducer(histories, { type, history }) { | ||
switch (type) { | ||
case "INSERT_COIN": | ||
histories.history = histories.history.concat({ | ||
...history, | ||
id: histories.history.length + 1, | ||
comment: `${history.coin}원이 투입되었습니다.`, | ||
}); | ||
return histories; | ||
case "CHANGE_COIN": | ||
histories.history = histories.concat({ | ||
...history, | ||
commet: `${history.change}이 반환되었습니다.`, | ||
}); | ||
return histories; | ||
case "PURCHASE_PRODUCT": | ||
histories.history = histories.concat({ ...history, commet: `${history.product}를 구매했습니다.` }); | ||
Comment on lines
+8
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공통되는 부분을 함수로 분리해도 괜찮을 것 같습니다! |
||
return histories; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
const initialHistory = { | ||
history: [], | ||
}; | ||
|
||
const HistoryContext = createContext(); | ||
|
||
function HistoryProvider({ children }) { | ||
const [histories, dispatch] = useReducer(historyReducer, initialHistory); | ||
|
||
const addHistory = useCallback((type, history) => { | ||
dispatch({ | ||
type, | ||
history, | ||
}); | ||
}, []); | ||
|
||
return <HistoryContext.Provider value={{ histories, addHistory }}>{children}</HistoryContext.Provider>; | ||
} | ||
|
||
export { HistoryContext, HistoryProvider }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import React, { useState, createContext } from "react"; | ||
|
||
const InsertCoinContext = createContext(); | ||
const SetInsertCoinContext = createContext(); | ||
|
||
function InsertCoinProvider({ children }) { | ||
const [insertCoin, setInsertCoin] = useState(0); | ||
|
||
return ( | ||
<InsertCoinContext.Provider value={{ insertCoin, setInsertCoin }}>{children}</InsertCoinContext.Provider> | ||
<InsertCoinContext.Provider value={insertCoin}> | ||
<SetInsertCoinContext.Provider value={setInsertCoin}>{children}</SetInsertCoinContext.Provider> | ||
</InsertCoinContext.Provider> | ||
); | ||
} | ||
|
||
export { InsertCoinContext, InsertCoinProvider }; | ||
export { InsertCoinContext, SetInsertCoinContext, InsertCoinProvider }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./DisplayProvider"; | ||
export * from "./InsertCoinProvider"; | ||
export * from "./CoinProvider"; | ||
export * from "./HistoryProvider"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./money"; | ||
export * from "./product"; | ||
export * from "./money.js"; | ||
export * from "./product.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,16 @@ import { useState, useCallback } from "react"; | |
function useCoin(init) { | ||
const [coin, setCoin] = useState(init); | ||
|
||
const selectCoin = useCallback( | ||
(unit, count) => { | ||
const copyArray = [...coin]; | ||
const findIndex = coin.findIndex((e) => e.unit === unit); | ||
copyArray[findIndex] = { ...copyArray[findIndex], count: count - 1 }; | ||
setCoin(copyArray); | ||
}, | ||
[coin] | ||
); | ||
const selectCoin = useCallback((unit) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
setCoin((prevCoin) => | ||
prevCoin.map((current) => { | ||
if (current.unit === unit) { | ||
return { ...current, count: current.count - 1 }; | ||
} | ||
return current; | ||
}) | ||
); | ||
}, []); | ||
|
||
return { coin, selectCoin }; | ||
} | ||
|
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.
중괄호와 리턴을 추가한 이유가 궁금합니다!
컨벤션을 바꾸신 걸까요?!
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.
앗 아무 생각없이 썼네요..! 수정했습니다 🥲