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] 1주차 두번째 PR #57

Merged
merged 13 commits into from
May 14, 2022
40 changes: 14 additions & 26 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import styled, { ThemeProvider } from "styled-components";
import { createGlobalStyle } from "styled-components";
import reset from "styled-reset";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { Home, VendingMachine, Wallet, NotFound } from "pages";
import { ThemeProvider, createGlobalStyle } from "styled-components";
import { theme } from "styles";

const StyledApp = styled.div`
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
`;
import reset from "styled-reset";
import { DisplayProvider } from "context";
import { Routers } from "components";

const GlobalStyles = createGlobalStyle`
${reset}
* {
box-sizing : border-box;
}

body{
width:100%;
margin: 0 auto;
font-family: sans-serif;
}

div {
border-radius: 10px;
}
Expand All @@ -26,26 +24,16 @@ a:hover {
cursor: pointer;
font-size: 1.2em;
}

`;

function App() {
return (
<StyledApp>
<GlobalStyles />
<DisplayProvider>
<ThemeProvider theme={theme}>
<BrowserRouter basename={process.env.PUBLIC_URL}>
<Routes>
<Route path="/" element={<Home />}>
<Route index element={<VendingMachine />} />
<Route path="/vendingmachine" element={<VendingMachine />} />
<Route path="/wallet/*" element={<Wallet />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
<GlobalStyles />
<Routers />
</ThemeProvider>
</StyledApp>
</DisplayProvider>
);
}

Expand Down
17 changes: 17 additions & 0 deletions src/components/AppLayout/AppLayout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import styled from "styled-components";

const ToggleWrapper = styled.div`
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
width: 100vw;
border-radius: 0px;
background-color: ${({ display }) => display.backgroundColor};
`;

function AppLayout({ display, children }) {
return <ToggleWrapper display={display}>{children}</ToggleWrapper>;
}

export { AppLayout };
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useContext } from "react";
import styled from "styled-components";
import { CoinContext } from "context";

const StyledBalance = styled.div`
width: 100%;
Expand All @@ -11,7 +13,10 @@ const StyledBalance = styled.div`
margin-top: 10px;
`;

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

return <StyledBalance>{balance + "원"}</StyledBalance>;
}

Expand Down
38 changes: 0 additions & 38 deletions src/components/Coin.jsx

This file was deleted.

28 changes: 28 additions & 0 deletions src/components/Coin/Coin.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useContext, memo } from "react";
import { InsertCoinContext, CoinContext } from "context";
import { Button } from "components";

function Coin({ unit, count }) {
const { selectCoin } = useContext(CoinContext);
const { insertCoin, setInsertCoin } = useContext(InsertCoinContext);

const clickCoin = () => {
if (!count) return;

Choose a reason for hiding this comment

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

early return 👍

selectCoin(unit, count);
setInsertCoin(insertCoin + unit);
};

return (
<>
<Button color="yellow" size="medium" onClick={clickCoin}>
<strong>{unit}</strong>
</Button>
<span>{count}</span>
</>
);
}

const areEqual = (prevProps, nextProps) => prevProps.count === nextProps.count;
const MemoizedCoin = memo(Coin, areEqual);

export { MemoizedCoin };
40 changes: 40 additions & 0 deletions src/components/Coin/CoinContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useContext } from "react";
import styled from "styled-components";
import { CoinContext } from "context";
import { MemoizedCoin } from "components";

const StyledCoinContainer = styled.ul`
li {
display: grid;
justify-items: center;
align-items: center;
grid-template-columns: 50% 50%;
margin: 10px;

span {
width: 100%;
background-color: #fff7bc;
display: grid;
place-items: center;
height: 50px;
color: #000;
border-radius: 10px;
}
}
`;

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

return (
<StyledCoinContainer>
{coin.map(({ id, unit, count }) => (
<li key={id}>
<MemoizedCoin unit={unit} count={count} />
</li>
))}
</StyledCoinContainer>
);
}

export { CoinContainer };
54 changes: 54 additions & 0 deletions src/components/MainNav/MainNav.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import styled from "styled-components";
import { Nav } from "components";

const StyledNav = styled.nav`
position: absolute;
top: 0;
padding-top: 20px;
margin-bottom: 10px;

ul {
display: flex;
margin-bottom: 20px;
}
`;

const Li = styled.li`
& + & {
margin-left: 1rem;
}

a {
display: grid;
place-items: center;
width: 200px;
height: 50px;
border-radius: 10px;
text-decoration: none;
color: #fff;
font-weight: 500;
}
`;

function MainNav() {
const activeStyle = ({ isActive }) => ({ backgroundColor: isActive ? "#512d6d" : "#9772fb" });

return (
<StyledNav>
<ul>
<Li>
<Nav link="/vendingmachine" activeStyle={activeStyle}>
자판기
</Nav>
</Li>
<Li>
<Nav to="/wallet" activeStyle={activeStyle}>
지갑
</Nav>
</Li>
</ul>
</StyledNav>
);
}

export { MainNav };
19 changes: 19 additions & 0 deletions src/components/Routers/Routers.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { Home, VendingMachine, Wallet, NotFound } from "pages";

function Routers() {
return (
<BrowserRouter basename={process.env.PUBLIC_URL}>
<Routes>
<Route path="/" element={<Home />}>
<Route index element={<VendingMachine />} />
<Route path="/vendingmachine" element={<VendingMachine />} />
<Route path="/wallet/*" element={<Wallet />} />
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}

export { Routers };
29 changes: 29 additions & 0 deletions src/components/ToggleDisplay/ToggleDisplay.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import styled from "styled-components";
import { Button } from "components";
import { useDisplay } from "hooks";

const StyledDisplay = styled.div`
position: absolute;
top: 30px;
right: 100px;

button {
box-shadow: 0px 0px 7px;
}
`;

function ToggleDisplay() {
const { displayMode, toggleDisplay } = useDisplay();
const color = displayMode === "light" ? "white" : "black";
const icon = displayMode === "light" ? "🌚" : "🌝";
Comment on lines +17 to +18

Choose a reason for hiding this comment

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

다크모드를 구현하셨군요!
밤에 확인을 해서 눈이 침침했었는데.. 감사합니다! 😄


return (
<StyledDisplay>
<Button color={color} size="small" onClick={toggleDisplay}>
{icon}
</Button>
</StyledDisplay>
);
}

export { ToggleDisplay };
42 changes: 0 additions & 42 deletions src/components/VendProduct.jsx

This file was deleted.

File renamed without changes.
12 changes: 12 additions & 0 deletions src/components/common/Nav.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";
import { NavLink } from "react-router-dom";

function Nav({ children, link, activeStyle, ...rest }) {
return (
<NavLink to={link} style={activeStyle} {...rest}>
{children}
</NavLink>
);
}

export { Nav };
23 changes: 15 additions & 8 deletions src/components/index.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export * from "./Coin";
export * from "./Balance";
export * from "./VendProduct";
export * from "./VendController";
export * from "./InsertCoin";
export * from "./ChangeOutlet";
export * from "./HistoryBox";
export * from "./Button";
export * from "./common/Button";
export * from "./common/Nav";
export * from "./Routers/Routers";
export * from "./AppLayout/AppLayout";
export * from "./MainNav/MainNav";
export * from "./ToggleDisplay/ToggleDisplay";
export * from "./coin/CoinContainer";
export * from "./coin/Coin";
export * from "./Balance/Balance";
export * from "./vend-product/VendProductContainer";
export * from "./vend-product/VendProduct";
export * from "./vend-controller/VendController";
export * from "./vend-controller/InsertCoin";
export * from "./vend-controller/ChangeOutlet";
export * from "./vend-controller/HistoryBox";
Loading