2022-01-01 20:42:25 +01:00
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
|
|
|
import { useEffect, useState } from 'react'
|
2021-11-13 21:50:34 +01:00
|
|
|
import InfiniteScroll from 'react-infinite-scroll-component'
|
|
|
|
|
2022-01-07 21:21:38 +01:00
|
|
|
import { useAuthentication } from '../../../tools/authentication'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { GuildPublic as GuildPublicType } from '../../../models/Guild'
|
2022-01-07 21:21:38 +01:00
|
|
|
import { Loader } from '../../design/Loader'
|
2022-01-01 20:42:25 +01:00
|
|
|
import { GuildPublic } from './GuildPublic'
|
2022-01-07 21:21:38 +01:00
|
|
|
import { usePagination } from '../../../hooks/usePagination'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { SocketData } from '../../../tools/handleSocketData'
|
|
|
|
import { handleSocketData } from '../../../tools/handleSocketData'
|
2021-11-13 21:50:34 +01:00
|
|
|
|
|
|
|
export const JoinGuildsPublic: React.FC = () => {
|
2022-01-01 20:42:25 +01:00
|
|
|
const [search, setSearch] = useState('')
|
2022-02-09 23:27:54 +01:00
|
|
|
|
2021-11-13 21:50:34 +01:00
|
|
|
const { authentication } = useAuthentication()
|
2022-01-01 20:42:25 +01:00
|
|
|
const { t } = useTranslation()
|
2021-11-13 21:50:34 +01:00
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
const { items, hasMore, nextPage, resetPagination, setItems } =
|
2022-01-01 20:42:25 +01:00
|
|
|
usePagination<GuildPublicType>({
|
|
|
|
api: authentication.api,
|
|
|
|
url: '/guilds/public'
|
2021-11-13 21:50:34 +01:00
|
|
|
})
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
useEffect(() => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.on(
|
|
|
|
'guilds',
|
|
|
|
(data: SocketData<GuildPublicType>) => {
|
|
|
|
handleSocketData({ data, setItems })
|
|
|
|
}
|
|
|
|
)
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
return () => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.off('guilds')
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}, [authentication.socket, setItems])
|
|
|
|
|
2021-11-13 21:50:34 +01:00
|
|
|
useEffect(() => {
|
2022-01-01 20:42:25 +01:00
|
|
|
resetPagination()
|
|
|
|
nextPage({ search })
|
|
|
|
}, [resetPagination, nextPage, search])
|
2021-11-13 21:50:34 +01:00
|
|
|
|
|
|
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
|
2022-01-01 20:42:25 +01:00
|
|
|
setSearch(event.target.value)
|
2021-11-13 21:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-02-19 23:20:33 +01:00
|
|
|
<div className='flex h-full w-full flex-col transition-all'>
|
2021-11-13 21:50:34 +01:00
|
|
|
<input
|
|
|
|
data-cy='search-guild-input'
|
|
|
|
onChange={handleChange}
|
2022-02-19 23:20:33 +01:00
|
|
|
className='my-6 mx-auto mt-16 w-10/12 rounded-md border border-gray-500 bg-white p-3 dark:border-gray-700 dark:bg-[#3B3B3B] sm:w-8/12 md:w-6/12 lg:w-5/12'
|
2021-11-13 21:50:34 +01:00
|
|
|
type='search'
|
|
|
|
name='search-guild'
|
2022-01-01 20:42:25 +01:00
|
|
|
placeholder={`🔎 ${t('application:search')}...`}
|
2021-11-13 21:50:34 +01:00
|
|
|
/>
|
2022-02-09 23:27:54 +01:00
|
|
|
<div className='w-full p-12'>
|
2021-11-13 21:50:34 +01:00
|
|
|
<InfiniteScroll
|
2022-03-16 11:25:44 +01:00
|
|
|
className='guilds-public-list relative mx-auto grid max-w-[1400px] grid-cols-[repeat(auto-fill,_minmax(20em,_1fr))] gap-8 !overflow-visible'
|
2022-01-01 20:42:25 +01:00
|
|
|
dataLength={items.length}
|
|
|
|
next={nextPage}
|
2021-11-13 21:50:34 +01:00
|
|
|
scrollableTarget='application-page-content'
|
|
|
|
hasMore={hasMore}
|
2022-03-16 11:25:44 +01:00
|
|
|
loader={<Loader className='absolute left-1/2 -translate-x-1/2' />}
|
2021-11-13 21:50:34 +01:00
|
|
|
>
|
2022-01-01 20:42:25 +01:00
|
|
|
{items.map((guild) => {
|
|
|
|
return <GuildPublic guild={guild} key={guild.id} />
|
2021-11-13 21:50:34 +01:00
|
|
|
})}
|
|
|
|
</InfiniteScroll>
|
|
|
|
</div>
|
2022-02-09 23:27:54 +01:00
|
|
|
</div>
|
2021-11-13 21:50:34 +01:00
|
|
|
)
|
|
|
|
}
|