feat(contexts): add Channels and Guilds
This commit is contained in:
54
contexts/Channels.tsx
Normal file
54
contexts/Channels.tsx
Normal file
@ -0,0 +1,54 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
|
||||
import { NextPage, usePagination } from 'hooks/usePagination'
|
||||
import { useAuthentication } from 'utils/authentication'
|
||||
import { Channel } from 'models/Channel'
|
||||
import { GuildsChannelsPath } from 'components/Application'
|
||||
|
||||
export interface Channels {
|
||||
channels: Channel[]
|
||||
hasMore: boolean
|
||||
nextPage: NextPage
|
||||
}
|
||||
|
||||
const defaultChannelsContext = {} as any
|
||||
const ChannelsContext = createContext<Channels>(defaultChannelsContext)
|
||||
|
||||
export interface ChannelsProviderProps {
|
||||
path: GuildsChannelsPath
|
||||
}
|
||||
|
||||
export const ChannelsProvider: React.FC<ChannelsProviderProps> = (props) => {
|
||||
const { path, children } = props
|
||||
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const {
|
||||
items: channels,
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination
|
||||
} = usePagination<Channel>({
|
||||
api: authentication.api,
|
||||
url: `/guilds/${path.guildId}/channels`
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
resetPagination()
|
||||
nextPage()
|
||||
}, [nextPage, resetPagination])
|
||||
|
||||
return (
|
||||
<ChannelsContext.Provider value={{ channels, hasMore, nextPage }}>
|
||||
{children}
|
||||
</ChannelsContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useChannels = (): Channels => {
|
||||
const channels = useContext(ChannelsContext)
|
||||
if (channels === defaultChannelsContext) {
|
||||
throw new Error('useChannels must be used within ChannelsProvider')
|
||||
}
|
||||
return channels
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { createContext, useContext } from 'react'
|
||||
import { createContext, useContext, useState } from 'react'
|
||||
|
||||
import { Guild } from 'models/Guild'
|
||||
import { Member } from 'models/Member'
|
||||
@ -16,8 +16,10 @@ const defaultGuildMemberContext = {} as any
|
||||
const GuildMemberContext = createContext<GuildMember>(defaultGuildMemberContext)
|
||||
|
||||
export const GuildMemberProvider: React.FC<GuildMemberProps> = (props) => {
|
||||
const [guildMember] = useState(props.guildMember)
|
||||
|
||||
return (
|
||||
<GuildMemberContext.Provider value={props.guildMember}>
|
||||
<GuildMemberContext.Provider value={guildMember}>
|
||||
{props.children}
|
||||
</GuildMemberContext.Provider>
|
||||
)
|
||||
|
49
contexts/Guilds.tsx
Normal file
49
contexts/Guilds.tsx
Normal file
@ -0,0 +1,49 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
|
||||
import { NextPage, usePagination } from 'hooks/usePagination'
|
||||
import { useAuthentication } from 'utils/authentication'
|
||||
import { GuildWithDefaultChannelId } from 'models/Guild'
|
||||
|
||||
export interface Guilds {
|
||||
guilds: GuildWithDefaultChannelId[]
|
||||
hasMore: boolean
|
||||
nextPage: NextPage
|
||||
}
|
||||
|
||||
const defaultGuildsContext = {} as any
|
||||
const GuildsContext = createContext<Guilds>(defaultGuildsContext)
|
||||
|
||||
export const GuildsProvider: React.FC = (props) => {
|
||||
const { children } = props
|
||||
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const {
|
||||
items: guilds,
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination
|
||||
} = usePagination<GuildWithDefaultChannelId>({
|
||||
api: authentication.api,
|
||||
url: '/guilds'
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
resetPagination()
|
||||
nextPage()
|
||||
}, [nextPage, resetPagination])
|
||||
|
||||
return (
|
||||
<GuildsContext.Provider value={{ guilds, hasMore, nextPage }}>
|
||||
{children}
|
||||
</GuildsContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useGuilds = (): Guilds => {
|
||||
const guilds = useContext(GuildsContext)
|
||||
if (guilds === defaultGuildsContext) {
|
||||
throw new Error('useGuilds must be used within GuildsProvider')
|
||||
}
|
||||
return guilds
|
||||
}
|
Reference in New Issue
Block a user