2021-09-27 12:17:33 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2022-05-17 11:52:08 +02:00
|
|
|
import {
|
|
|
|
getUsers,
|
|
|
|
fetchUsers,
|
|
|
|
fetchUserById,
|
|
|
|
updateUserById,
|
|
|
|
createUser,
|
|
|
|
deleteUser,
|
|
|
|
clearCurrentUser,
|
|
|
|
} from '../redux';
|
2022-02-11 12:45:22 +01:00
|
|
|
import { getUserById, getUserModalLoading, getUserslLoading } from '../redux/selectors';
|
2021-09-27 12:17:33 +02:00
|
|
|
|
|
|
|
export function useUsers() {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const users = useSelector(getUsers);
|
2021-12-21 14:55:46 +00:00
|
|
|
const user = useSelector(getUserById);
|
|
|
|
const userModalLoading = useSelector(getUserModalLoading);
|
2022-02-11 12:45:22 +01:00
|
|
|
const userTableLoading = useSelector(getUserslLoading);
|
2021-09-27 12:17:33 +02:00
|
|
|
|
|
|
|
function loadUsers() {
|
|
|
|
return dispatch(fetchUsers());
|
|
|
|
}
|
|
|
|
|
2021-12-21 14:55:46 +00:00
|
|
|
function loadUser(id: string) {
|
|
|
|
return dispatch(fetchUserById(id));
|
|
|
|
}
|
|
|
|
|
2022-05-17 11:52:08 +02:00
|
|
|
function clearSelectedUser() {
|
|
|
|
return dispatch(clearCurrentUser());
|
|
|
|
}
|
|
|
|
|
2021-12-21 14:55:46 +00:00
|
|
|
function editUserById(data: any) {
|
|
|
|
return dispatch(updateUserById(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNewUser(data: any) {
|
|
|
|
return dispatch(createUser(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteUserById(id: string) {
|
|
|
|
return dispatch(deleteUser(id));
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:17:33 +02:00
|
|
|
return {
|
|
|
|
users,
|
2021-12-21 14:55:46 +00:00
|
|
|
user,
|
|
|
|
loadUser,
|
2021-09-27 12:17:33 +02:00
|
|
|
loadUsers,
|
2021-12-21 14:55:46 +00:00
|
|
|
editUserById,
|
|
|
|
userModalLoading,
|
2022-02-11 12:45:22 +01:00
|
|
|
userTableLoading,
|
2021-12-21 14:55:46 +00:00
|
|
|
createNewUser,
|
|
|
|
deleteUserById,
|
2022-05-17 11:52:08 +02:00
|
|
|
clearSelectedUser,
|
2021-09-27 12:17:33 +02:00
|
|
|
};
|
|
|
|
}
|