-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug in protected routes components
routing is now relative and zustand is used to share state between components
- Loading branch information
Showing
5 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { useEffect, useState } from 'react' | ||
import NotFound from '../pages/NotFound/NotFound.tsx' | ||
import { User } from '../services/userProfile' | ||
import { isAuthorizedToCreateTeam } from '../services/createTeam' | ||
import { Effect } from 'effect' | ||
import { Effect, Option as O } from 'effect' | ||
import { isDaplaAdmin } from '../utils/services' | ||
import { option } from '../utils/utils' | ||
import { Skeleton } from '@mui/material' | ||
import PageLayout from './PageLayout/PageLayout.tsx' | ||
import { Outlet } from 'react-router-dom' | ||
import { useUserProfileStore } from '../services/store.ts' | ||
|
||
export interface Props { | ||
component: React.ReactElement | ||
} | ||
const ProtectedAuthorizedUserRoute = () => { | ||
// O.none() here represents the loading state | ||
const [oIsAuthorized, setIsAuthorized] = useState<O.Option<boolean>>(O.none()) | ||
const maybeUser: O.Option<User> = useUserProfileStore((state) => state.loggedInUser) | ||
|
||
const ProtectedAuthorizedUserRoute = ({ component }: Props) => { | ||
const [isAuthorized, setIsAuthorized] = useState(false) | ||
useEffect(() => { | ||
const userProfileItem = localStorage.getItem('userProfile') | ||
if (!userProfileItem) return | ||
Effect.gen(function* () { | ||
const user: User = yield* O.match(maybeUser, { | ||
onNone: () => Effect.fail(new Error('User not logged in!')), | ||
onSome: (user) => Effect.succeed(user), | ||
}) | ||
|
||
const user = JSON.parse(userProfileItem) as User | ||
if (!user) return | ||
const daplaAdmin: boolean = yield* Effect.promise(() => isDaplaAdmin(user.principal_name)) | ||
|
||
Effect.promise(() => isDaplaAdmin(user.principal_name)) | ||
.pipe(Effect.runPromise) | ||
.then((isDaplaAdmin: boolean) => setIsAuthorized(isAuthorizedToCreateTeam(isDaplaAdmin, user.job_title))) | ||
yield* Effect.sync(() => setIsAuthorized(O.some(isAuthorizedToCreateTeam(daplaAdmin, user.job_title)))) | ||
}).pipe(Effect.runPromise) | ||
}, []) | ||
|
||
return isAuthorized ? component : <NotFound /> | ||
return option( | ||
oIsAuthorized, | ||
() => ( | ||
<PageLayout | ||
title='Opprett Team' | ||
content={<Skeleton variant='rectangular' animation='wave' width={800} height={600} />} | ||
/> | ||
), | ||
(isAuthorized) => (isAuthorized ? <Outlet /> : <NotFound />) | ||
) | ||
} | ||
|
||
export default ProtectedAuthorizedUserRoute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters