Skip to content

Commit 3f19c64

Browse files
committed
Refactor for fetching a single user
1 parent 43167c9 commit 3f19c64

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/pages/User/index.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import { useFormatMessage } from 'hooks';
1111
const User = () => {
1212
const { id } = useParams();
1313

14-
const { success, usersList, userData, error } = useSelector(
14+
const { success, userData, error } = useSelector(
1515
(state) => ({
1616
success: state.users.success,
17-
usersList: state.users.list,
1817
userData: state.users.user,
1918
error: state.users.error,
2019
}),
@@ -27,12 +26,7 @@ const User = () => {
2726

2827
useEffect(() => {
2928
if (id) {
30-
const userFetched = usersList.find(
31-
(fetchedUser) => fetchedUser.id === id
32-
);
33-
if (userFetched) {
34-
setUser(userFetched);
35-
} else if (userData.id === id) {
29+
if (userData.id === id) {
3630
setUser(userData);
3731
} else {
3832
dispatch(fetchUsers(id));

src/state/actions/users.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export const fetchUsers = (userId = '') => {
4141

4242
dispatch(USERS_FETCH_DATA_INIT());
4343
if (userId) {
44+
const userFetched = getState().users.list.find(
45+
(fetchedUser) => fetchedUser.id === userId
46+
);
47+
48+
if (userId) {
49+
return dispatch(
50+
USERS_FETCH_DATA_SUCCESS({
51+
list: getState().users.list,
52+
user: userFetched,
53+
})
54+
);
55+
}
56+
4457
let userData;
4558
try {
4659
userData = (

0 commit comments

Comments
 (0)