2023-10-23 23:33:39 +02:00
|
|
|
import useTranslation from "next-translate/useTranslation"
|
|
|
|
import { useEffect, useState } from "react"
|
|
|
|
import InfiniteScroll from "react-infinite-scroll-component"
|
2021-11-13 21:50:34 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { useAuthentication } from "../../../tools/authentication"
|
|
|
|
import type { GuildPublic as GuildPublicType } from "../../../models/Guild"
|
|
|
|
import { Loader } from "../../design/Loader"
|
|
|
|
import { GuildPublic } from "./GuildPublic"
|
|
|
|
import { usePagination } from "../../../hooks/usePagination"
|
|
|
|
import type { SocketData } from "../../../tools/handleSocketData"
|
|
|
|
import { handleSocketData } from "../../../tools/handleSocketData"
|
2021-11-13 21:50:34 +01:00
|
|
|
|
|
|
|
export const JoinGuildsPublic: React.FC = () => {
|
2023-10-23 23:33:39 +02: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,
|
2023-10-23 23:33:39 +02:00
|
|
|
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(
|
2023-10-23 23:33:39 +02:00
|
|
|
"guilds",
|
2022-08-24 17:22:55 +02:00
|
|
|
(data: SocketData<GuildPublicType>) => {
|
|
|
|
handleSocketData({ data, setItems })
|
2023-10-23 23:33:39 +02:00
|
|
|
},
|
2022-08-24 17:22:55 +02:00
|
|
|
)
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
return () => {
|
2023-10-23 23:33:39 +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 (
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex h-full w-full flex-col transition-all">
|
2021-11-13 21:50:34 +01:00
|
|
|
<input
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="search-guild-input"
|
2021-11-13 21:50:34 +01:00
|
|
|
onChange={handleChange}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="mx-auto my-6 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"
|
|
|
|
type="search"
|
|
|
|
name="search-guild"
|
|
|
|
placeholder={`🔎 ${t("application:search")}...`}
|
2021-11-13 21:50:34 +01:00
|
|
|
/>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="w-full p-12">
|
2021-11-13 21:50:34 +01:00
|
|
|
<InfiniteScroll
|
2023-10-23 23:33:39 +02: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}
|
2023-10-23 23:33:39 +02:00
|
|
|
scrollableTarget="application-page-content"
|
2021-11-13 21:50:34 +01:00
|
|
|
hasMore={hasMore}
|
2023-10-23 23:33:39 +02: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
|
|
|
)
|
|
|
|
}
|