2022-01-01 20:42:25 +01:00
|
|
|
import { createContext, useContext, useEffect, useState } from 'react'
|
2022-03-05 18:22:30 +01:00
|
|
|
import { useRouter } from 'next/router'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { GuildWithDefaultChannelId } from '../models/Guild'
|
|
|
|
import type { Member } from '../models/Member'
|
2022-03-16 12:18:09 +01:00
|
|
|
import { useAuthentication } from '../tools/authentication'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { SocketData } from '../tools/handleSocketData'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
export interface GuildMember {
|
2022-03-05 18:22:30 +01:00
|
|
|
guild: GuildWithDefaultChannelId
|
2022-01-01 20:42:25 +01:00
|
|
|
member: Member
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:30:06 +02:00
|
|
|
export interface GuildMemberResult extends GuildMember {}
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
export interface GuildMemberProps {
|
|
|
|
guildMember: GuildMember
|
2022-03-05 18:22:30 +01:00
|
|
|
path: {
|
|
|
|
guildId: number
|
|
|
|
}
|
2022-01-01 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultGuildMemberContext = {} as any
|
2022-03-05 18:22:30 +01:00
|
|
|
const GuildMemberContext = createContext<GuildMemberResult>(
|
|
|
|
defaultGuildMemberContext
|
|
|
|
)
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2022-05-12 20:35:46 +02:00
|
|
|
export const GuildMemberProvider: React.FC<
|
|
|
|
React.PropsWithChildren<GuildMemberProps>
|
|
|
|
> = (props) => {
|
2022-03-05 18:22:30 +01:00
|
|
|
const { path, children } = props
|
|
|
|
const router = useRouter()
|
2022-01-01 20:42:25 +01:00
|
|
|
const [guildMember, setGuildMember] = useState(props.guildMember)
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchGuildMember = async (): Promise<void> => {
|
2022-03-05 18:22:30 +01:00
|
|
|
const { data } = await authentication.api.get(`/guilds/${path.guildId}`)
|
2022-01-01 20:42:25 +01:00
|
|
|
setGuildMember(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchGuildMember().catch((error) => {
|
|
|
|
console.error(error)
|
|
|
|
})
|
2022-03-05 18:22:30 +01:00
|
|
|
}, [path, authentication.api])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.on(
|
2022-03-05 18:22:30 +01:00
|
|
|
'guilds',
|
|
|
|
async (data: SocketData<GuildWithDefaultChannelId>) => {
|
|
|
|
if (data.item.id === path.guildId) {
|
|
|
|
switch (data.action) {
|
|
|
|
case 'delete':
|
|
|
|
await router.push('/application')
|
|
|
|
break
|
|
|
|
case 'update':
|
|
|
|
setGuildMember((oldGuildMember) => {
|
|
|
|
return {
|
|
|
|
...oldGuildMember,
|
|
|
|
guild: {
|
|
|
|
...oldGuildMember.guild,
|
|
|
|
...data.item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return () => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.off('guilds')
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}, [authentication.socket, path.guildId, router])
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
return (
|
2022-03-05 18:22:30 +01:00
|
|
|
<GuildMemberContext.Provider
|
|
|
|
value={{
|
2022-08-30 21:30:06 +02:00
|
|
|
...guildMember
|
2022-03-05 18:22:30 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
2022-01-01 20:42:25 +01:00
|
|
|
</GuildMemberContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
export const useGuildMember = (): GuildMemberResult => {
|
2022-01-01 20:42:25 +01:00
|
|
|
const guildMember = useContext(GuildMemberContext)
|
|
|
|
if (guildMember === defaultGuildMemberContext) {
|
2022-08-30 21:30:06 +02:00
|
|
|
throw new Error(
|
|
|
|
'`useGuildMember` must be used within `GuildMemberProvider`'
|
|
|
|
)
|
2022-01-01 20:42:25 +01:00
|
|
|
}
|
|
|
|
return guildMember
|
|
|
|
}
|