mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
36 lines
924 B
TypeScript
36 lines
924 B
TypeScript
'use client'
|
|
import { getAPIUrl } from '@services/config/config'
|
|
import { swrFetcher } from '@services/utils/ts/requests'
|
|
import React, { useContext, useEffect } from 'react'
|
|
import useSWR from 'swr'
|
|
import { createContext } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
export const OrgContext = createContext({}) as any
|
|
|
|
export function OrgProvider({
|
|
children,
|
|
orgslug,
|
|
}: {
|
|
children: React.ReactNode
|
|
orgslug: string
|
|
}) {
|
|
const { data: org } = useSWR(`${getAPIUrl()}orgs/slug/${orgslug}`, swrFetcher)
|
|
const router = useRouter()
|
|
// Check if Org is Active
|
|
const verifyIfOrgIsActive = () => {
|
|
if (org && org?.config.config.GeneralConfig.active === false) {
|
|
router.push('/404')
|
|
}
|
|
|
|
}
|
|
useEffect(() => {
|
|
verifyIfOrgIsActive()
|
|
}, [org])
|
|
|
|
return <OrgContext.Provider value={org}>{children}</OrgContext.Provider>
|
|
}
|
|
|
|
export function useOrg() {
|
|
return useContext(OrgContext)
|
|
}
|