Skip to content

Commit

Permalink
chore(j-s): Redirect logged in user (#16978)
Browse files Browse the repository at this point in the history
* chore(j-s): Redirect logged in user

* Update Login.tsx

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
unakb and kodiakhq[bot] authored Nov 27, 2024
1 parent fdc3e12 commit 0226fb1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions apps/judicial-system/web/src/routes/Shared/Login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { useContext, useEffect } from 'react'
import { useIntl } from 'react-intl'
import { useRouter } from 'next/router'

import { AlertMessage, Box, Button, Text } from '@island.is/island-ui/core'
import {
CASES_ROUTE,
COURT_OF_APPEAL_CASES_ROUTE,
DEFENDER_CASES_ROUTE,
PRISON_CASES_ROUTE,
USERS_ROUTE,
} from '@island.is/judicial-system/consts'
import {
isCourtOfAppealsUser,
isDefenceUser,
isPrisonStaffUser,
} from '@island.is/judicial-system/types'
import { login, titles } from '@island.is/judicial-system-web/messages'
import {
PageHeader,
PageTitle,
UserContext,
} from '@island.is/judicial-system-web/src/components'
import { UserRole } from '@island.is/judicial-system-web/src/graphql/schema'
import { api } from '@island.is/judicial-system-web/src/services'
import { LoginErrorCodes } from '@island.is/judicial-system-web/src/types'

Expand All @@ -15,6 +30,7 @@ import * as styles from './Login.css'
const Login = () => {
const router = useRouter()
const { formatMessage } = useIntl()
const { user } = useContext(UserContext)

const getErrorAlert = (errorCode: LoginErrorCodes): JSX.Element | null => {
switch (errorCode) {
Expand Down Expand Up @@ -55,6 +71,22 @@ const Login = () => {
}
}

useEffect(() => {
if (user) {
const redirectRoute = isDefenceUser(user)
? DEFENDER_CASES_ROUTE
: isPrisonStaffUser(user)
? PRISON_CASES_ROUTE
: isCourtOfAppealsUser(user)
? COURT_OF_APPEAL_CASES_ROUTE
: user.role === UserRole.ADMIN
? USERS_ROUTE
: CASES_ROUTE

router.push(redirectRoute)
}
}, [router, user])

return (
<>
<PageHeader title={formatMessage(titles.shared.login)} />
Expand Down

0 comments on commit 0226fb1

Please sign in to comment.