import { useCallback, useEffect, useState, useRef } from 'react' import InfiniteScroll from 'react-infinite-scroll-component' import { useAuthentication } from 'utils/authentication' import { GuildPublic } from 'models/Guild' import { Loader } from 'components/design/Loader' import { useFetchState } from 'hooks/useFetchState' import { Guild } from './Guild' export const JoinGuildsPublic: React.FC = () => { const [guilds, setGuilds] = useState([]) const [hasMore, setHasMore] = useState(true) const [inputSearch, setInputSearch] = useState('') const [fetchState, setFetchState] = useFetchState('idle') const afterId = useRef(null) const { authentication } = useAuthentication() const fetchGuilds = useCallback(async (): Promise => { if (fetchState !== 'idle') { return } setFetchState('loading') const { data } = await authentication.api.get( `/guilds/public?limit=20&search=${inputSearch}${ afterId.current != null ? `&after=${afterId.current}` : '' }` ) afterId.current = data.length > 0 ? data[data.length - 1].id : null setGuilds((oldGuilds) => { return [...oldGuilds, ...data] }) setHasMore(data.length > 0) setFetchState('idle') }, [authentication, fetchState, setFetchState, inputSearch]) useEffect(() => { afterId.current = null setGuilds([]) fetchGuilds().catch((error) => { console.error(error) }) }, [inputSearch]) // eslint-disable-line react-hooks/exhaustive-deps const handleChange = (event: React.ChangeEvent): void => { setInputSearch(event.target.value) } return ( <>
} > {guilds.map((guild) => { return })}
) }