2020-08-03 12:04:07 +02:00
|
|
|
import { useState, useEffect, useRef, useCallback } from 'react'
|
|
|
|
import HeadTag from '../../components/HeadTag'
|
|
|
|
import Loader from '../../components/Loader'
|
2020-08-03 13:18:58 +02:00
|
|
|
import UserCard from '../../components/UserCard/UserCard'
|
2020-08-03 12:04:07 +02:00
|
|
|
import api from '../../utils/api'
|
2020-05-04 12:58:36 +02:00
|
|
|
|
|
|
|
const Users = () => {
|
2020-08-03 12:04:07 +02:00
|
|
|
let pageUsers = 1
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const [inputSearch, setInputSearch] = useState('')
|
2020-08-03 13:18:58 +02:00
|
|
|
const [usersData, setUsersData] = useState({
|
|
|
|
totalItems: 0,
|
|
|
|
hasMore: true,
|
|
|
|
rows: []
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
const [isLoadingUsers, setLoadingUsers] = useState(true)
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Récupère les users si la recherche change
|
|
|
|
useEffect(() => {
|
|
|
|
pageUsers = 1
|
2020-08-03 13:18:58 +02:00
|
|
|
getUsersData().then(data => setUsersData(data))
|
2020-08-03 12:04:07 +02:00
|
|
|
}, [inputSearch])
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const getUsersData = async () => {
|
|
|
|
setLoadingUsers(true)
|
2020-08-03 13:18:58 +02:00
|
|
|
const { data } = await api.get(
|
|
|
|
`/users?page=${pageUsers}&limit=15&search=${inputSearch}`
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
setLoadingUsers(false)
|
|
|
|
return data
|
|
|
|
}
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 13:18:58 +02:00
|
|
|
const handleSearchChange = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
setInputSearch(event.target.value)
|
|
|
|
}
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Permet la pagination au scroll
|
|
|
|
const observer = useRef()
|
2020-08-03 13:18:58 +02:00
|
|
|
const lastUserCardRef = useCallback(
|
|
|
|
node => {
|
|
|
|
if (isLoadingUsers) return
|
|
|
|
if (observer.current) observer.current.disconnect()
|
|
|
|
observer.current = new window.IntersectionObserver(
|
|
|
|
entries => {
|
|
|
|
if (entries[0].isIntersecting && usersData.hasMore) {
|
|
|
|
pageUsers += 1
|
|
|
|
getUsersData().then(data => {
|
|
|
|
setUsersData(oldData => {
|
|
|
|
return {
|
|
|
|
totalItems: data.totalItems,
|
|
|
|
hasMore: data.hasMore,
|
|
|
|
rows: [...oldData.rows, ...data.rows]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ threshold: 1 }
|
|
|
|
)
|
|
|
|
if (node) observer.current.observe(node)
|
|
|
|
},
|
|
|
|
[isLoadingUsers, usersData.hasMore]
|
|
|
|
)
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
|
|
|
<>
|
2020-08-03 13:18:58 +02:00
|
|
|
<HeadTag title='Utilisateurs' description='Liste des utilisateurs.' />
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='container text-center'>
|
|
|
|
<div className='row justify-content-center'>
|
|
|
|
<div className='col-24'>
|
2020-08-03 13:18:58 +02:00
|
|
|
<h1 style={{ marginBottom: 0, paddingTop: '20px' }}>
|
|
|
|
Utilisateurs
|
|
|
|
</h1>
|
|
|
|
<p style={{ marginTop: '5px' }}>
|
|
|
|
La liste des utilisateurs - Total de {usersData.totalItems}{' '}
|
|
|
|
utilisateurs :
|
|
|
|
</p>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='Users__search-container row justify-content-center'>
|
2020-08-03 13:18:58 +02:00
|
|
|
<input
|
|
|
|
value={inputSearch}
|
|
|
|
onChange={handleSearchChange}
|
|
|
|
type='search'
|
|
|
|
className='Users__form-control Users__search-input'
|
|
|
|
name='search'
|
|
|
|
id='search'
|
|
|
|
placeholder='🔎 Rechercher...'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='row justify-content-center'>
|
|
|
|
{usersData.rows.map((user, index) => {
|
|
|
|
// Si c'est le dernier élément
|
|
|
|
if (usersData.rows.length === index + 1) {
|
|
|
|
return (
|
2020-08-03 13:18:58 +02:00
|
|
|
<UserCard key={index} {...user} ref={lastUserCardRef} />
|
2020-08-03 12:04:07 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return (
|
2020-08-03 13:18:58 +02:00
|
|
|
<UserCard key={index} {...user} />
|
2020-08-03 12:04:07 +02:00
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
2020-05-04 12:58:36 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
{isLoadingUsers && <Loader />}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
2020-05-04 12:58:36 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default Users
|