Skip to content

Commit 3ddc099

Browse files
committed
Completed the challenge jonasschmedtmann#1 : Bank Account with useReducer in section 16 of the course
1 parent dce6dd3 commit 3ddc099

File tree

12 files changed

+330
-0
lines changed

12 files changed

+330
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:react/recommended',
7+
'plugin:react/jsx-runtime',
8+
'plugin:react-hooks/recommended',
9+
],
10+
ignorePatterns: ['dist', '.eslintrc.cjs'],
11+
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
12+
settings: { react: { version: '18.2' } },
13+
plugins: ['react-refresh'],
14+
rules: {
15+
'react/jsx-no-target-blank': 'off',
16+
'react-refresh/only-export-components': [
17+
'warn',
18+
{ allowConstantExport: true },
19+
],
20+
},
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# React + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.jsx"></script>
12+
</body>
13+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "bank-account-usereducer",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"react": "^18.2.0",
14+
"react-dom": "^18.2.0"
15+
},
16+
"devDependencies": {
17+
"@types/react": "^18.2.66",
18+
"@types/react-dom": "^18.2.22",
19+
"@vitejs/plugin-react": "^4.2.1",
20+
"eslint": "^8.57.0",
21+
"eslint-plugin-react": "^7.34.1",
22+
"eslint-plugin-react-hooks": "^4.6.0",
23+
"eslint-plugin-react-refresh": "^0.4.6",
24+
"vite": "^5.2.0"
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { useReducer } from "react";
2+
import "./App.css";
3+
4+
const initialState = {
5+
balance: 0,
6+
loan: 0,
7+
isActive: false,
8+
};
9+
10+
const reducer = (state, action) => {
11+
if (!state.isActive && action.type !== "openAccount") return state;
12+
13+
switch (action.type) {
14+
case "openAccount":
15+
return { ...state, isActive: true, balance: 500 };
16+
case "deposit":
17+
return { ...state, balance: state.balance + action.payload };
18+
case "withdraw":
19+
return { ...state, balance: state.balance - action.payload };
20+
case "requestLoan": {
21+
const haveLoan = state.loan === action.payload;
22+
23+
return {
24+
...state,
25+
loan: haveLoan ? state.loan : action.payload,
26+
balance: haveLoan ? state.balance : state.balance + action.payload,
27+
};
28+
}
29+
case "payLoan": {
30+
const haveLoan = state.loan === action.payload;
31+
32+
return {
33+
...state,
34+
loan: haveLoan ? state.loan - action.payload : state.loan,
35+
balance: haveLoan ? state.balance - action.payload : state.balance,
36+
};
37+
}
38+
case "closeAccount":
39+
if (state.balance === 0 && state.loan === 0) return { ...initialState };
40+
return { ...state };
41+
42+
default:
43+
throw new Error("Unknown type error");
44+
}
45+
};
46+
47+
export default function App() {
48+
const [{ balance, loan, isActive }, dispatch] = useReducer(
49+
reducer,
50+
initialState
51+
);
52+
53+
return (
54+
<div className="App">
55+
<h1>useReducer Bank Account</h1>
56+
<p>Balance: {balance}</p>
57+
<p>Loan: {loan}</p>
58+
59+
<p>
60+
<button
61+
onClick={() => dispatch({ type: "openAccount" })}
62+
disabled={isActive}
63+
>
64+
Open account
65+
</button>
66+
</p>
67+
<p>
68+
<button
69+
onClick={() => dispatch({ type: "deposit", payload: 150 })}
70+
disabled={!isActive}
71+
>
72+
Deposit 150
73+
</button>
74+
</p>
75+
<p>
76+
<button
77+
onClick={() => dispatch({ type: "withdraw", payload: 50 })}
78+
disabled={!isActive}
79+
>
80+
Withdraw 50
81+
</button>
82+
</p>
83+
<p>
84+
<button
85+
onClick={() => dispatch({ type: "requestLoan", payload: 5000 })}
86+
disabled={!isActive}
87+
>
88+
Request a loan of 5000
89+
</button>
90+
</p>
91+
<p>
92+
<button
93+
onClick={() => dispatch({ type: "payLoan", payload: 5000 })}
94+
disabled={!isActive}
95+
>
96+
Pay loan
97+
</button>
98+
</p>
99+
<p>
100+
<button
101+
onClick={() => dispatch({ type: "closeAccount" })}
102+
disabled={!isActive}
103+
>
104+
Close account
105+
</button>
106+
</p>
107+
</div>
108+
);
109+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
:root {
2+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3+
line-height: 1.5;
4+
font-weight: 400;
5+
6+
color-scheme: light dark;
7+
color: rgba(255, 255, 255, 0.87);
8+
background-color: #242424;
9+
10+
font-synthesis: none;
11+
text-rendering: optimizeLegibility;
12+
-webkit-font-smoothing: antialiased;
13+
-moz-osx-font-smoothing: grayscale;
14+
}
15+
16+
a {
17+
font-weight: 500;
18+
color: #646cff;
19+
text-decoration: inherit;
20+
}
21+
a:hover {
22+
color: #535bf2;
23+
}
24+
25+
body {
26+
margin: 0;
27+
display: flex;
28+
place-items: center;
29+
min-width: 320px;
30+
min-height: 100vh;
31+
}
32+
33+
h1 {
34+
font-size: 3.2em;
35+
line-height: 1.1;
36+
}
37+
38+
button {
39+
border-radius: 8px;
40+
border: 1px solid transparent;
41+
padding: 0.6em 1.2em;
42+
font-size: 1em;
43+
font-weight: 500;
44+
font-family: inherit;
45+
background-color: #1a1a1a;
46+
cursor: pointer;
47+
transition: border-color 0.25s;
48+
}
49+
button:hover {
50+
border-color: #646cff;
51+
}
52+
button:focus,
53+
button:focus-visible {
54+
outline: 4px auto -webkit-focus-ring-color;
55+
}
56+
57+
@media (prefers-color-scheme: light) {
58+
:root {
59+
color: #213547;
60+
background-color: #ffffff;
61+
}
62+
a:hover {
63+
color: #747bff;
64+
}
65+
button {
66+
background-color: #f9f9f9;
67+
}
68+
}

0 commit comments

Comments
 (0)