Skip to content

Commit ac4fe03

Browse files
committed
added redux toolkit
1 parent 5bbc107 commit ac4fe03

File tree

12 files changed

+1592
-2166
lines changed

12 files changed

+1592
-2166
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
"plugin:@typescript-eslint/recommended",
55
"prettier/@typescript-eslint",
66
"plugin:prettier/recommended",
7-
"plugin:react/recommended",
7+
// "plugin:react/recommended",
88
],
99
parserOptions: {
1010
ecmaVersion: 2018,
@@ -22,7 +22,7 @@ module.exports = {
2222
{
2323
endOfLine: "auto",
2424
},
25-
{ usePrettierrc: true },
25+
// { usePrettierrc: true },
2626
],
2727
},
2828
settings: {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@reduxjs/toolkit": "^1.5.1",
67
"eslint-plugin-react": "^7.19.0",
78
"husky": "^4.2.3",
89
"react": "^17.0.1",
@@ -37,7 +38,7 @@
3738
]
3839
},
3940
"devDependencies": {
40-
"@types/react": "^17.0.0",
41+
"@types/react": "^17.0.3",
4142
"@types/react-dom": "^17.0.0",
4243
"@types/react-redux": "^7.1.15",
4344
"@types/react-router-dom": "^5.1.7",

src/App.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import React from "react"
22
import { Provider } from "react-redux"
3-
import { createStore, combineReducers, applyMiddleware } from "redux"
4-
import thunk from "redux-thunk"
53

6-
import items from "./app/redux/Item/Item.reducers"
4+
import { store } from "./app/redux/store"
75
import RouteManager from "./app/RouteManager"
86

97
// Setup Redux store with Thunks
10-
const reducers = combineReducers({ items })
11-
const store = createStore(reducers, applyMiddleware(thunk))
8+
// const reducers = combineReducers({ items })
9+
// const store = createStore(reducers, applyMiddleware(thunk))
1210

1311
const App = () => {
1412
return (

src/app/pages/item/ItemList.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import React from "react"
2-
import { connect, useDispatch } from "react-redux"
3-
import { refreshList } from "../../redux/Item/Item.actions"
4-
import { ItemProps, Item } from "../../redux/Item/Item.types"
2+
import { useAppDispatch, useAppSelector } from "../../redux/hooks"
3+
import { fetchList } from "../../redux/Item/Item.slice"
54

6-
interface RootState {
7-
items: any
8-
}
9-
const ItemList = ({ items }: ItemProps) => {
10-
const dispatch = useDispatch()
5+
const ItemList = () => {
6+
const items = useAppSelector((state) => state.items.items)
7+
const loading = useAppSelector((state) => state.items.loading)
8+
const dispatch = useAppDispatch()
119
const handleClick = () => {
12-
dispatch(refreshList)
10+
dispatch(fetchList())
1311
}
1412
return (
1513
<div>
16-
<button onClick={handleClick}>Refresh</button>
14+
<button onClick={handleClick} disabled={loading}>
15+
Refresh
16+
</button>
1717
<ul>
18-
{items.map((item: Item) => (
18+
{items.map((item) => (
1919
<li key={item.id}>{item.name}</li>
2020
))}
2121
</ul>
2222
</div>
2323
)
2424
}
2525

26-
const mapStateToProps = (state: RootState) => {
27-
console.log(state.items)
28-
return {
29-
items: state.items.items,
30-
}
31-
}
32-
33-
export default connect(mapStateToProps)(ItemList)
26+
export default ItemList

src/app/redux/Item/Item.actions.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/app/redux/Item/Item.reducers.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/app/redux/Item/Item.slice.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"
2+
import type { RootState } from "../store"
3+
4+
import { ItemServiceImpl } from "../../../domain/usecases/ItemService"
5+
import { ItemRepositoryImpl } from "../../../data/repositories/ItemRepositoryImpl"
6+
import { Item } from "../../../domain/entities/Item"
7+
// Define a type for the slice state
8+
interface CounterState {
9+
loading: boolean
10+
items: Array<Item>
11+
}
12+
13+
// Define the initial state using that type
14+
const initialState: CounterState = {
15+
loading: false,
16+
items: [],
17+
}
18+
19+
export const fetchList = createAsyncThunk("itemList/fetchList", async () => {
20+
const itemrepo = new ItemRepositoryImpl()
21+
const itemservice = new ItemServiceImpl(itemrepo)
22+
const items = await itemservice.GetItems()
23+
console.log(items)
24+
return items
25+
})
26+
export const itemSlice = createSlice({
27+
name: "itemList",
28+
// `createSlice` will infer the state type from the `initialState` argument
29+
initialState,
30+
reducers: {},
31+
extraReducers: (builder) => {
32+
builder.addCase(fetchList.fulfilled, (state, action) => {
33+
return {
34+
...state,
35+
items: action.payload,
36+
loading: false,
37+
}
38+
})
39+
builder.addCase(fetchList.pending, (state) => {
40+
return {
41+
...state,
42+
loading: true,
43+
}
44+
})
45+
builder.addCase(fetchList.rejected, (state) => {
46+
return {
47+
...state,
48+
loading: false,
49+
}
50+
})
51+
},
52+
})
53+
54+
// Other code such as selectors can use the imported `RootState` type
55+
export const items = (state: RootState) => state.items.items
56+
57+
export default itemSlice.reducer

src/app/redux/Item/Item.types.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/app/redux/hooks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"
2+
import type { RootState, AppDispatch } from "./store"
3+
4+
// Use throughout your app instead of plain `useDispatch` and `useSelector`
5+
export const useAppDispatch = () => useDispatch<AppDispatch>()
6+
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector

src/app/redux/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { configureStore } from "@reduxjs/toolkit"
2+
import { itemSlice } from "./Item/Item.slice"
3+
4+
export const store = configureStore({
5+
reducer: {
6+
items: itemSlice.reducer,
7+
},
8+
})
9+
10+
// Infer the `RootState` and `AppDispatch` types from the store itself
11+
export type RootState = ReturnType<typeof store.getState>
12+
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
13+
export type AppDispatch = typeof store.dispatch

0 commit comments

Comments
 (0)