Skip to content
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

Merged
merged 16 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Balance/Balance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const StyledBalance = styled.div`
`;

function Balance() {
const { coin } = useContext(CoinContext);
const coin = useContext(CoinContext);
const balance = coin.reduce((acc, cur) => acc + cur.unit * cur.count, 0);

return <StyledBalance>{balance + "원"}</StyledBalance>;
Expand Down
23 changes: 15 additions & 8 deletions src/components/Coin/Coin.jsx
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;
};
Comment on lines +29 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중괄호와 리턴을 추가한 이유가 궁금합니다!
컨벤션을 바꾸신 걸까요?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 아무 생각없이 썼네요..! 수정했습니다 🥲


const MemoizedCoin = memo(Coin, areEqual);

export { MemoizedCoin };
2 changes: 1 addition & 1 deletion src/components/Coin/CoinContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const StyledCoinContainer = styled.ul`
`;

function CoinContainer() {
const { coin } = useContext(CoinContext);
const coin = useContext(CoinContext);

return (
<StyledCoinContainer>
Expand Down
14 changes: 10 additions & 4 deletions src/components/vend-controller/HistoryBox.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HistoryContext } from "context";
import { useContext } from "react";
import styled from "styled-components";

const StyledHistoryBox = styled.ul`
Expand All @@ -7,14 +9,18 @@ const StyledHistoryBox = styled.ul`
margin-top: 10px;
`;

const History = styled.li``;
function History({ comment }) {
return comment;
}

function HistoryBox() {
const history = [{ id: 1, type: "input", content: "500" }];
const { histories } = useContext(HistoryContext);
return (
<StyledHistoryBox>
{history.map(({ id, type, content }) => (
<History key={id}>{content + type}</History>
{histories.history.map(({ id, comment }) => (
<li key={id}>
<History comment={comment} />
</li>
))}
</StyledHistoryBox>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/vend-controller/InsertCoin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const StyledInsertCoin = styled.input`
`;

function InsertCoin() {
const { insertCoin } = useContext(InsertCoinContext);
const insertCoin = useContext(InsertCoinContext);

return (
<StyledInsertCoin
defaultValue={insertCoin}
Expand Down
2 changes: 1 addition & 1 deletion src/components/vend-product/VendProductContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


li {
background-color: #f2f2f2;
Expand Down
10 changes: 8 additions & 2 deletions src/context/CoinProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { useCoin } from "hooks";
import { money } from "data";

const CoinContext = createContext();
const SetCoinContext = createContext();

function CoinProvider({ children }) {
const { coin, selectCoin } = useCoin(money);
return <CoinContext.Provider value={{ coin, selectCoin }}>{children}</CoinContext.Provider>;

return (
<CoinContext.Provider value={coin}>
<SetCoinContext.Provider value={selectCoin}>{children}</SetCoinContext.Provider>
</CoinContext.Provider>
);
}

export { CoinContext, CoinProvider };
export { CoinContext, SetCoinContext, CoinProvider };
47 changes: 47 additions & 0 deletions src/context/HistoryProvider.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useCallback } from "react";

const { createContext, useReducer } = require("react");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일관성을 위해 import로 변경하면 어떨까요?? ✋

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 };
8 changes: 6 additions & 2 deletions src/context/InsertCoinProvider.jsx
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 };
1 change: 1 addition & 0 deletions src/context/index.jsx
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";
4 changes: 2 additions & 2 deletions src/data/index.js
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";
19 changes: 10 additions & 9 deletions src/hooks/useCoin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Choose a reason for hiding this comment

The 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 };
}
Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ import ReactDOM from "react-dom/client";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
root.render(<App />);
14 changes: 8 additions & 6 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from "react";
import { Outlet } from "react-router-dom";
import { DisplayContext, CoinProvider, InsertCoinProvider } from "context";
import { DisplayContext, CoinProvider, InsertCoinProvider, HistoryProvider } from "context";
import { AppLayout, MainNav, ToggleDisplay } from "components";
import { theme } from "styles";

Expand All @@ -12,11 +12,13 @@ function Home() {
<AppLayout display={displayObject}>
<ToggleDisplay />
<MainNav />
<InsertCoinProvider>
<CoinProvider>
<Outlet />
</CoinProvider>
</InsertCoinProvider>
<HistoryProvider>
<InsertCoinProvider>
<CoinProvider>
<Outlet />
</CoinProvider>
</InsertCoinProvider>
</HistoryProvider>
</AppLayout>
);
}
Expand Down