-
Notifications
You must be signed in to change notification settings - Fork 27
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
Task #211821 Redux Tool-kit in project #1010
base: release-2.8.1
Are you sure you want to change the base?
Changes from 5 commits
ede35e5
989580b
d8815e4
5357152
add71ee
1a9ea8c
e96d4a8
f112608
3655797
ecb3de1
35390e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; | ||
|
||
export const fetchUsers = createAsyncThunk('users/fetchUsers', async () => { | ||
const response = await fetch('https://dummyjson.com/users'); | ||
const data = await response.json(); | ||
return data; | ||
}); | ||
|
||
|
||
const dataTableSlice = createSlice({ | ||
|
||
name :"dataTable", | ||
initialState:{data:null,status: 'idle', | ||
error: null,}, | ||
reducers:{}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(fetchUsers.pending, (state) => { | ||
state.status = 'loading'; | ||
}) | ||
.addCase(fetchUsers.fulfilled, (state, action) => { | ||
state.status = 'succeeded'; | ||
state.data = action.payload; | ||
}) | ||
.addCase(fetchUsers.rejected, (state, action) => { | ||
state.status = 'failed'; | ||
state.error = action.error.message; | ||
}); | ||
}, | ||
}) | ||
|
||
export const {getData} = dataTableSlice.actions | ||
export const selectdataTable = (state) => state?.dataTable; | ||
|
||
export default dataTableSlice.reducer |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,45 @@ | ||||||||||||||||||||||||||||||||||||||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; | ||||||||||||||||||||||||||||||||||||||
import { facilitatorRegistryService } from "@shiksha/common-lib"; | ||||||||||||||||||||||||||||||||||||||
import { get, set } from "idb-keyval"; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export const fetchIpUserData = createAsyncThunk( | ||||||||||||||||||||||||||||||||||||||
"ipData/fetchIpUserData", | ||||||||||||||||||||||||||||||||||||||
async () => { | ||||||||||||||||||||||||||||||||||||||
const result = facilitatorRegistryService.getInfo(); | ||||||||||||||||||||||||||||||||||||||
const data = await result; | ||||||||||||||||||||||||||||||||||||||
return data; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+5
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The async () => {
+ try {
const result = facilitatorRegistryService.getInfo();
const data = await result;
return data;
+ } catch (error) {
+ // Handle or throw the error appropriately
+ }
} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const ip_ListSlice = createSlice({ | ||||||||||||||||||||||||||||||||||||||
name: "ipData", | ||||||||||||||||||||||||||||||||||||||
initialState: { | ||||||||||||||||||||||||||||||||||||||
data: (await get("ipData")) || null, | ||||||||||||||||||||||||||||||||||||||
status: "idle", | ||||||||||||||||||||||||||||||||||||||
error: null, | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+14
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using initialState: {
- data: (await get("ipData")) || null,
+ data: null, // Set initial state to null or a synchronous value
status: "idle",
error: null,
}, You will need to handle the asynchronous retrieval of Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
reducers: {}, | ||||||||||||||||||||||||||||||||||||||
extraReducers: (builder) => { | ||||||||||||||||||||||||||||||||||||||
builder | ||||||||||||||||||||||||||||||||||||||
.addCase(fetchIpUserData.pending, (state) => { | ||||||||||||||||||||||||||||||||||||||
state.status = "loading"; | ||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
.addCase(fetchIpUserData.fulfilled, (state, action) => { | ||||||||||||||||||||||||||||||||||||||
state.status = "succeeded"; | ||||||||||||||||||||||||||||||||||||||
state.data = action.payload; | ||||||||||||||||||||||||||||||||||||||
set("ipData", action.payload).catch((error) => { | ||||||||||||||||||||||||||||||||||||||
console.error("Error setting data:", error); | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging errors to the console is not a robust error handling strategy for production code. Consider dispatching an action to store the error in the state or implementing a more user-friendly error reporting mechanism. - console.error("Error setting data:", error);
+ // Dispatch an action to store the error in the state or handle it appropriately Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
.addCase(fetchIpUserData.rejected, (state, action) => { | ||||||||||||||||||||||||||||||||||||||
state.status = "failed"; | ||||||||||||||||||||||||||||||||||||||
state.error = action.error.message; | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export const getData = ip_ListSlice.actions; | ||||||||||||||||||||||||||||||||||||||
export const selectedIpData = (state) => state?.ipData; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export default ip_ListSlice.reducer; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||
// userSlice.js | ||||||||||||||||||||||||||||
import { createSlice } from '@reduxjs/toolkit'; | ||||||||||||||||||||||||||||
import { get, set } from 'idb-keyval'; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const userSlice = createSlice({ | ||||||||||||||||||||||||||||
name: 'user', | ||||||||||||||||||||||||||||
initialState: {data: await get("data") || {} }, | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using - initialState: {data: await get("data") || {} },
+ initialState: {data: {} }, You will need to dispatch an action to populate this state after the store has been created. Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
reducers: { | ||||||||||||||||||||||||||||
setUser: (state, action) => { | ||||||||||||||||||||||||||||
state.data = action.payload; | ||||||||||||||||||||||||||||
set('data', action.payload) | ||||||||||||||||||||||||||||
.then(() => { | ||||||||||||||||||||||||||||
console.log('Data set successfully'); | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
.catch((error) => { | ||||||||||||||||||||||||||||
console.error('Error setting data:', error); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
Comment on lines
+9
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reducer - set('data', action.payload)
- .then(() => {
- console.log('Data set successfully');
- })
- .catch((error) => {
- console.error('Error setting data:', error);
- });
+ // Move IndexedDB operations to a middleware or an async thunk. Consider using an async thunk for IndexedDB operations. Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export const { setUser } = userSlice.actions; | ||||||||||||||||||||||||||||
export const selectUser = (state) => state?.user?.data; | ||||||||||||||||||||||||||||
export default userSlice.reducer; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||
// mainSlice.js | ||||||
import { combineReducers } from "@reduxjs/toolkit"; | ||||||
import userReducer from "./Slices/userSlice"; | ||||||
import dataTableReducer from "./Slices/dataTableSlice"; | ||||||
import IpUserInfoSlice from "./Slices/ipUserInfoSlice"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import - import IpUserInfoSlice from "./Slices/ipUserInfoSlice";
+ import ipUserInfoSlice from "./Slices/ipUserInfoSlice"; Committable suggestion
Suggested change
|
||||||
|
||||||
const rootReducer = combineReducers({ | ||||||
user: userReducer, | ||||||
dataTable: dataTableReducer, | ||||||
ipUserInfo: IpUserInfoSlice, | ||||||
// Add more child slices as needed | ||||||
}); | ||||||
|
||||||
export default rootReducer; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import for - import IpUserInfoSlice from "./Slices/ipUserInfoSlice";
+ import ipUserInfoReducer from "./Slices/ipUserInfoSlice"; Then, update the ipUserInfo: IpUserInfoSlice,
+ ipUserInfo: ipUserInfoReducer, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// store.js | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
// import rootReducer from './rootReducer'; | ||
import rootReducer from "./rootReducer"; | ||
|
||
const store = configureStore({ | ||
reducer: rootReducer, | ||
// Add middleware or enhancers as needed | ||
}); | ||
|
||
export default store; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second
useEffect
hook is fetching user data from IndexedDB. Ensure that error handling is robust and consider the implications of setting state directly from IndexedDB without going through Redux if the state is meant to be globally accessible.Consider refactoring to use Redux for all state management, including asynchronous operations with IndexedDB.