initial commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
'use client'
|
||||
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import Loading from '../components/layout/shared/Loading'
|
||||
|
||||
type AuthContextType = {
|
||||
isAuthenticated: boolean
|
||||
token: string | null
|
||||
currentUser: string | null
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType>({
|
||||
isAuthenticated: false,
|
||||
token: null,
|
||||
currentUser: null
|
||||
})
|
||||
|
||||
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const [token, setToken] = useState<string | null>(null)
|
||||
const [currentUser, setCurrentUser] = useState<string | null>(null)
|
||||
const [isInitialized, setIsInitialized] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const savedToken = localStorage.getItem('authToken')
|
||||
const savedUser = localStorage.getItem('user')
|
||||
if (savedToken) setToken(savedToken)
|
||||
if (savedUser) setCurrentUser(savedUser)
|
||||
setIsInitialized(true)
|
||||
}, [])
|
||||
|
||||
if (!isInitialized) return <Loading />
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
isAuthenticated: !!token,
|
||||
token,
|
||||
currentUser
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useAuth = () => useContext(AuthContext)
|
||||
@@ -0,0 +1,46 @@
|
||||
'use client'
|
||||
|
||||
// React Imports
|
||||
import { createContext, useState } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
export const initialIntersections: Record<string, boolean> = {
|
||||
features: false,
|
||||
team: false,
|
||||
faq: false,
|
||||
'contact-us': false
|
||||
}
|
||||
|
||||
type IntersectionContextProps = {
|
||||
intersections: Record<string, boolean>
|
||||
updateIntersections: (data: Record<string, boolean>) => void
|
||||
}
|
||||
|
||||
export const IntersectionContext = createContext<IntersectionContextProps | null>(null)
|
||||
|
||||
export const IntersectionProvider = ({ children }: { children: ReactNode }) => {
|
||||
// States
|
||||
const [intersections, setIntersections] = useState(initialIntersections)
|
||||
|
||||
const updateIntersections = (data: Record<string, boolean>) => {
|
||||
setIntersections(prev => {
|
||||
const isAnyActive = Object.values(intersections).some(value => value === true)
|
||||
|
||||
if (!Object.values(data)[0] && !isAnyActive) return prev
|
||||
|
||||
Object.keys(prev).forEach(key => {
|
||||
if (prev[key] === true && Object.keys(data).some(dataKey => data[dataKey] === true)) {
|
||||
prev[key] = false
|
||||
}
|
||||
})
|
||||
|
||||
return { ...prev, ...data }
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<IntersectionContext.Provider value={{ intersections, updateIntersections }}>
|
||||
{children}
|
||||
</IntersectionContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user