How can I get the users by deparment from API using React and Redux.
I have got the list of users from API using the AXIOS :
import axios from "axios";
const DEPARTMENTS_GOT_FROM_REST_API = "http://localhost:8082/api/departments";
class AdminSetvice {
get_all_departments() {
return axios.get(DEPARTMENTS_GOT_FROM_REST_API)
}
get_users_by_depName(depName) {
return axios.get(DEPARTMENTS_GOT_FROM_REST_API + "/" + depName)
}
}
export default new AdminSetvice();
For Redux I created
UsersByDep.ActionTypes.js
export default {
GETTING_USERS_BY_DEPARTMENT: 'GETTING_USERS_BY_DEPARTMENT',
GET_USERS_BY_DEPARTMENT: 'GET_USERS_BY_DEPARTMENT',
GET_USERS_BY_DEPARTMENT_ERROR: 'GET_USERS_BY_DEPARTMENT_ERROR',
}
UsersByDep.actions.js
import UsersByDepActionTypes from "./UsersByDep.ActionTypes"
const GettingUsersByDepName = () => ({
type: UsersByDepActionTypes.GETTING_USERS_BY_DEPARTMENT,
})
const GetUsersByDepName = (users) => ({
type: UsersByDepActionTypes.GET_USERS_BY_DEPARTMENT,
payload: users
})
const GettingUsersByDepNameError = (errorMsg) => ({
type: UsersByDepActionTypes.GET_USERS_BY_DEPARTMENT_ERROR,
payload: errorMsg,
})
export default {
GettingUsersByDepName,
GetUsersByDepName,
GettingUsersByDepNameError,
}
UsersByDep.InitialState.js
export default {
usersIsLoading: false,
users: [],
usersErrorMessage: null,
}
UsersByDep.Reducer.js
import UsersByDepActionTypes from "./UsersByDep.ActionTypes";
import UsersByDepInitialState from "./UsersByDep.InitialState";
const UsersByDepReducer = (state = UsersByDepInitialState, { type, payload }) => {
switch (type) {
case UsersByDepActionTypes.GETTING_USERS_BY_DEPARTMENT:
return {
...state,
usersIsLoading: true,
}
case UsersByDepActionTypes.GET_USERS_BY_DEPARTMENT:
return {
...state,
usersIsLoading: false,
users: payload,
}
case UsersByDepActionTypes.GET_USERS_BY_DEPARTMENT_ERROR:
return {
...state,
usersIsLoading: false,
usersErrorMsg: payload,
}
default:
return state
}
};
export default UsersByDepReducer;
and UsersByDep.Thunk.js
import UsersByDepService from '../../../Services/Adminservice'
import UsersByDepActions from './UsersByDep.actions'
export const AsyncGetUsersByDepName = (department) => (dispatch) => {
dispatch(UsersByDepActions.GettingUsersByDepName)
UsersByDepService.get_users_by_depName(department.department)
.then(res => {
dispatch(UsersByDepActions.GetUsersByDepName(res.data))
console.log(`users : ${res.data}`)
})
.catch(err => {
dispatch(UsersByDepActions.GettingUsersByDepNameError(err.message))
})
}
I configured store, reducers and so on…other components work fine.
For the UI I created a small table for displaying the departments and users in that department:
import { AsyncDepartmentsLoad, AsyncDepartmentsAdd, AsyncDeleteDepartmet } from '../../Redux/Reducer/Departments/Departments.Thunk';
import { AsyncGetUsersByDepName } from '../../Redux/Reducer/Departments/UsersByDep.Thunk'
const AdminComponent = () => {
let countriesCount = 1;
let departmentsCount = 1;
let locationsCount = 1;
let plantsCount = 1;
let UsersCount = 0;
const DepDispatch = useDispatch();
const { depIsLoading, departments, depErrorMessage } = useSelector((state) => state.departments)
useEffect(() => {
DepDispatch(AsyncDepartmentsLoad())
}, [DepDispatch]);
const [depName, setDepName] = useState('');
const handleDepSubmit = (e) => {
e.preventDefault();
const dep = {
department: depName,
}
DepDispatch(AsyncDepartmentsAdd(dep))
setDepName('');
setDepartmentShow(false);
}
<div className='col-6 p-2'>
<h3 className='text-center'>
<span>
Departments
</span>
<span className='ms-3'>
<button type='button' className='btn btn-sm btn-secondary' data-toggle="modal" onClick={() => setDepartmentShow(true)} data-target="#addNewItemModal">
+
</button>
</span>
</h3>
<div className='p-2' style={{ minHeight: "400px" }}>
<MDBTable hover>
<MDBTableHead>
<tr>
<th scope='col'>#</th>
<th scope='col'>Department name</th>
<th scope='col'></th>
<th scope='col'></th>
</tr>
</MDBTableHead>
<MDBTableBody>
{isLoading && <p className='text-muted bold'>loading....</p>}
{errorMessage && <p className='p-3 mb-2 bg-danger text-white"'>{errorMessage}</p>}
{departments && departments.map(dep =>
<tr key={dep.id}>
<th scope='col'>{departmentsCount++}</th>
<th scope='col'>{dep.department}
</th>
<th><span>
{
}
</span></th>
<th scope='col'> <Button variant="danger btn-sm" onClick={() => { DepDispatch(AsyncDeleteDepartmet(dep)) }}>-</Button></th>
</tr>
)}
</MDBTableBody>
</MDBTable>
</div>
</div>
</div >
</div >
</div >
</>
)
}
export default AdminComponent
Itried to add
<tr key={dep.id}>
<th scope='col'>{departmentsCount++}</th>
<th scope='col'>{dep.department}</th>
<th><span>
{
() => { return DepDispatch(AsyncGetUsersByDepName(dep.department)) }
}
</span></th>
<th scope='col'> <Button variant="danger btn-sm" onClick={() => {DepDispatch(AsyncDeleteDepartmet(dep)) }}>-</Button></th>
</tr>
but No result.
If I access the link from Axios, for example :
http://localhost:8082/api/departments/IT
I can see the user json which in this Department :
[{
"firstname":"Artiom",
"lastname":"userIT",
"login":"userIT",
"email":"[email protected]",
"isActive":"ACTIVE",
"activity":true,
"password":"admin",
"secret":"admin",
"roles":"ROLE_ADMIN",
"permissions":"IT",
"creationDate":"2023-06-0210:03:33",
"departmentName":"IT",
"plant":"Plant1",
"id":1,
"permissionsList":["ROLE_ADMIN"],
"roleList":["ROLE_ADMIN"]
}]
How can I display at least names of users by departmetn? What I do wrong?