feat: nextjs 14 (#20)
* feat: eslint update * feat: start over and layout * feat: nextjs13 boilerplate * feat: static homepage * feat: static pages * feat: static unirse * chore: remove old mui types * chore: moving from yarn to bun * chore: update dependencies * feat: static equipo unirse * feat: move appwrite to entgamers-database package * feat: improve ui components * feat: update dependencies * feat: static login & register pages * fix: remove unused logs * feat: state redux toolkit & feedback slice * fix: equipo div inside p * feat: session * feat: metadataBase * feat: basic apply form * feat: http verbs * feat: recover password flow * chore: updated dependencies * fix: fix image config * fix: api team-applications route * fix: remove not longer used fonts * feat: session with current user * fix: login form recuperar contraseña * feat: equipo pages now uses data from database package * feat: useManageErrors hook * feat: updated cuenta page * chore: updated old formik forms to use hooks * feat: updated dependencies &package name * fix: session related bugs * fix: missing helper texts * feat: static applications dashboard * chore: update dependencies * refactor: team applications * fix: session api update
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
'use client'
|
||||
import { useAppDispatch } from '@/hooks/useAppDispatch'
|
||||
import { useAppSelector } from '@/hooks/useAppSelector'
|
||||
import { setClanes, setCurrentUser, setSession, setStatus } from '@/state/sessionSlice'
|
||||
import { AppwriteException } from 'appwrite'
|
||||
import { getClanes } from 'entgamers-database/frontend/clanes'
|
||||
import { getCurrentUser, getSession } from 'entgamers-database/frontend/session'
|
||||
import { useCallback, useEffect, type FC } from 'react'
|
||||
|
||||
const SessionConsumer: FC = () => {
|
||||
const { status, session, user, clanes } = useAppSelector((state) => state.session)
|
||||
const dispatch = useAppDispatch()
|
||||
|
||||
const ensureSession = useCallback(async () => {
|
||||
try {
|
||||
if (status !== 'initializing' || session !== undefined) return
|
||||
dispatch(setStatus('loading'))
|
||||
const currentSession = await getSession('current')
|
||||
const currentUser = await getCurrentUser()
|
||||
dispatch(setSession(currentSession))
|
||||
dispatch(setCurrentUser(currentUser))
|
||||
} catch (error) {
|
||||
dispatch(setSession())
|
||||
dispatch(setCurrentUser())
|
||||
throw error
|
||||
} finally {
|
||||
dispatch(setStatus('idle'))
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
ensureSession()
|
||||
.catch((error) => {
|
||||
if (error instanceof AppwriteException) {
|
||||
console.error(error)
|
||||
}
|
||||
})
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (user !== undefined && clanes === undefined) {
|
||||
getClanes()
|
||||
.then((clanes) => {
|
||||
dispatch(setClanes(clanes))
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof AppwriteException) {
|
||||
console.error(error)
|
||||
}
|
||||
})
|
||||
} else if (user === undefined && clanes !== undefined) {
|
||||
dispatch(setClanes())
|
||||
}
|
||||
}, [user])
|
||||
|
||||
return (
|
||||
<>
|
||||
</>
|
||||
)
|
||||
}
|
||||
export default SessionConsumer
|
||||
Reference in New Issue
Block a user