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

Redux #11

Open
wants to merge 2 commits into
base: redux
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Chapter1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
1 change: 1 addition & 0 deletions Chapter2-end/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
69 changes: 41 additions & 28 deletions Chapter2-start/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Chapter2-start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.3.4",
"axios": "^1.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Expand Down
11 changes: 9 additions & 2 deletions Chapter2-start/src/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useSelector } from 'react-redux';
import './App.css';
import Account from './components/Account';
import Bonus from './components/Bonus';
import Reward from './components/Reward';

function App() {
const amount = useSelector(state => state.account.amount);
const bonus = useSelector(state => state.bonus.point)
const pending = useSelector(state => state.account.pending);
return (
<div className="App">
<h4>App</h4>
<h3>Current Amount : </h3>
<h3>Total Bonus : </h3>
<h3>Current Amount : {pending ? 'Loadiiing...' : amount} </h3>
<h3>Total Bonus : {pending ? 'Loading....' : bonus}</h3>

<Account></Account>
<Bonus></Bonus>
<Reward></Reward>
</div>
);
}
Expand Down
35 changes: 13 additions & 22 deletions Chapter2-start/src/components/Account.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import { useState } from 'react';

function Account() {
const [account, setAccount] = useState({ amount: 0 });
const [value, setValue] = useState(0);

const increment = () => {
setAccount({ amount: account.amount + 1 });
};

const decrement = () => {
setAccount({ amount: account.amount - 1 });
};
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement, incrementByAmount, getUser } from '../slices/accountSlice';

const incrementByAmount = (value) => {
setAccount({ amount: account.amount + value });
};
function Account() {
const amount = useSelector(state => state.account.amount);
const dispatch = useDispatch();

return (
<div className="card">
<div className="container">
<h4>
<b>Account Component</b>
</h4>
<h3>Amount:${account.amount}</h3>
<button onClick={increment}>Increment +</button>
<button onClick={decrement}>Decrement -</button>
<input type="text" onChange={(e) => setValue(+e.target.value)}></input>
<button onClick={() => incrementByAmount(value)}>
Increment By {value} +
<h3>Amount:${amount}</h3>
<button onClick={() => dispatch(increment())}>Increment +</button>
<button onClick={() => dispatch(decrement())}>Decrement -</button>
<button onClick={() => dispatch(incrementByAmount(1011))}>
Increment By +
</button>
<button onClick={() => dispatch(getUser(1))}>
get user
</button>
</div>
</div>
Expand Down
14 changes: 6 additions & 8 deletions Chapter2-start/src/components/Bonus.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { useState } from 'react';
import { useSelector, useDispatch } from "react-redux";
import { increment } from "../slices/bonusSlice";

function Bonus() {
const [points, setPoints] = useState({ value: 0 });
const point = useSelector(state => state.bonus.point);
const dispatch = useDispatch();

const increment = () => {
setPoints({ value: points.value + 1 });
};
return (
<div className="card">
<div className="container">
<h4>
<b>Bonus Component</b>
</h4>
<h3>Total Point : ${points.value}</h3>

<button onClick={increment}>Increment +</button>
<h3>Total Point : ${point}</h3>
<button onClick={() => dispatch(increment())}>Increment +</button>
</div>
</div>
);
Expand Down
21 changes: 21 additions & 0 deletions Chapter2-start/src/components/Reward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useSelector, useDispatch } from "react-redux";
import { increment } from "../reducers/reducer";

function Reward() {
const point = useSelector(state => state.reward.rewardPoints);
const dispatch = useDispatch();

return (
<div className="card">
<div className="container">
<h4>
<b>Reward Component</b>
</h4>
<h3>Total Point : ${point}</h3>
<button onClick={() => dispatch(increment())}>Increment +</button>
</div>
</div>
);
}

export default Reward;
15 changes: 15 additions & 0 deletions Chapter2-start/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { configureStore } from '@reduxjs/toolkit';
import accountReducer from './slices/accountSlice';
import bonusReducer from './slices/bonusSlice';
import { rewardReducer } from './reducers/reducer';
import { Provider } from 'react-redux';

const store = configureStore({
reducer: {
account: accountReducer,
bonus: bonusReducer,
reward: rewardReducer
}
})


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);

Expand Down
13 changes: 13 additions & 0 deletions Chapter2-start/src/reducers/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createAction, createReducer } from '@reduxjs/toolkit'

export const increment = createAction('reward/increment')

const initialState = { rewardPoints: 0 }

export const rewardReducer = createReducer(initialState, (builder) => {
builder
.addCase(increment, (state, action) => {
state.rewardPoints++
})
})

51 changes: 51 additions & 0 deletions Chapter2-start/src/slices/accountSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import axios from 'axios'

export const getUser = createAsyncThunk(
'account/getUser',
async (userId, thunkAPI) => {
await new Promise((resolve, rejected) => {
setTimeout(() => {
resolve()
}, 5000);
})
const { data } = await axios(`http://localhost:8080/accounts/${userId}`);

return data;
}
)

const initialState = {
amount: 1,
pending: false
}

export const accountSlice = createSlice({
name: 'account',
initialState,
reducers: {
increment: (state) => {
state.amount += 1
},
decrement: (state) => {
state.amount -= 1
},
incrementByAmount: (state, action) => {
state.amount += action.payload
}
},
extraReducers: (builder) => {
builder.addCase(getUser.pending, (state, action) => {
state.pending = true;
}).addCase(getUser.fulfilled, (state, action) => {
state.pending = false;
}).addCase(getUser.rejected, (state, action) => {
state.pending = false;
})
}
})

// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = accountSlice.actions

export default accountSlice.reducer
Loading