fix teacher, parent and admin pages
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { useState } from 'react'
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { fetchDashboardRoute } from '../api/session'
|
||||
import { useAuth } from '../auth/AuthProvider'
|
||||
import { spaPathFromCiUrl } from '../lib/ciSpaPaths'
|
||||
|
||||
/** Shared navigation after JWT login when the account has multiple roles (CI `welcome_back` / `select_role`). */
|
||||
export function useContinueWithRole() {
|
||||
const { setSelectedRole } = useAuth()
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [busyRole, setBusyRole] = useState<string | null>(null)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const from = (location.state as { from?: string } | null)?.from ?? '/app/home'
|
||||
|
||||
async function continueWithRole(nextRole: string) {
|
||||
setBusy(true)
|
||||
setBusyRole(nextRole)
|
||||
setError(null)
|
||||
try {
|
||||
setSelectedRole(nextRole)
|
||||
const roleLower = nextRole.toLowerCase()
|
||||
const fromApp = from.startsWith('/app') ? from : ''
|
||||
const hasDeepLink = Boolean(fromApp && fromApp !== '/app/home')
|
||||
|
||||
let target: string
|
||||
if (hasDeepLink) {
|
||||
target = fromApp
|
||||
} else if (roleLower === 'parent') {
|
||||
target = '/app/parent/home'
|
||||
} else if (roleLower === 'teacher' || roleLower === 'teacher_assistant') {
|
||||
target = '/app/teacher_dashboard'
|
||||
} else {
|
||||
target = '/app/home'
|
||||
try {
|
||||
const dash = await fetchDashboardRoute()
|
||||
const route = dash.data?.dashboard?.route
|
||||
const mapped = spaPathFromCiUrl(route)
|
||||
if (mapped) target = mapped
|
||||
} catch {
|
||||
/* keep default */
|
||||
}
|
||||
}
|
||||
|
||||
navigate(target, { replace: true })
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : 'Unable to continue.')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
setBusyRole(null)
|
||||
}
|
||||
}
|
||||
|
||||
return { continueWithRole, busy, busyRole, error, setError, from }
|
||||
}
|
||||
Reference in New Issue
Block a user