import style from "./Users.module.css"
import React, { useEffect } from "react"
import User from "./SingleUser/User"
import Paginator from "../common/Paginator/Paginator"
import UserSearchForm, { FilterType } from "./UserSearchForm"
import { useDispatch, useSelector } from "react-redux"
import {
getCurrentPageSelector,
getFilterSelector,
getFollowingInProgressSelector,
getPageSizeSelector,
getTotalUsersCountSelector,
getUsersSelector,
} from "../../redux/selectors/userSelectors"
import { followTC, getUsersTC, unFollowTC } from "../../redux/usersReducer"
const Users: React.FC = (props) => {
useEffect(() => {
dispatch(getUsersTC(pageSize, currentPage, filter))
// eslint-disable-next-line react-hooks/exhaustive-deps
} , [])
const currentPage = useSelector(getCurrentPageSelector)
const pageSize = useSelector(getPageSizeSelector)
const filter = useSelector(getFilterSelector)
const totalUsersCount = useSelector(getTotalUsersCountSelector)
const users = useSelector(getUsersSelector)
const followingInProgress = useSelector(getFollowingInProgressSelector)
const dispatch = useDispatch()
const updateUsers = (pageNumber: number) => {
dispatch(getUsersTC(pageSize, pageNumber, filter))
}
const updateFilter = (filter: FilterType) => {
dispatch(getUsersTC(pageSize, 1, filter))
}
const followThunk = (userId: number) => {
dispatch(followTC(userId))
}
const unFollowThunk = (userId: number) => {
dispatch(unFollowTC(userId))
}
return (
<div className={style.usersContainer}>
<div>
<h2>Users</h2>
</div>
<UserSearchForm updateFilter={updateFilter} />
<Paginator
totalUsersCount={totalUsersCount}
pageSize={pageSize}
currentPage={currentPage}
updateUsers={updateUsers}
/>
<div className={style.usersInfo}>
{users.map((u) => (
<User
followThunk={followThunk}
unFollowThunk={unFollowThunk}
followingInProgress={followingInProgress}
user={u}
key={u.id}
/>
))}
</div>
</div>
)
}
export default Users