diff --git a/src/common/AuthWrapper.test.tsx b/src/common/AuthWrapper.test.tsx index fc5e281e12..f8015befc5 100644 --- a/src/common/AuthWrapper.test.tsx +++ b/src/common/AuthWrapper.test.tsx @@ -6,14 +6,26 @@ import { describe, expect, it, vi } from 'vitest'; import { AuthWrapper } from './AuthWrapper'; -vi.mock('src/stores/Profile/profile.store', () => ({ - useProfileStore: () => ({ - profile: FactoryUser({ - roles: [UserRole.BETA_TESTER], - }), - }), - ProfileStoreProvider: ({ children }: { children: React.ReactNode }) => children, -})); +import type { Profile } from 'oa-shared'; + +const { ProfileStore } = await vi.hoisted(async () => { + const actual = await import('src/stores/Profile/profile.store'); + return { ProfileStore: actual.ProfileStore }; +}); + +const mockStore = new ProfileStore(); +mockStore.profile = FactoryUser({ + roles: [UserRole.BETA_TESTER], +}) as Profile; + +vi.mock('src/stores/Profile/profile.store', async (importOriginal) => { + const actual: any = await importOriginal(); + return { + ...actual, + useProfileStore: () => mockStore, + ProfileStoreProvider: ({ children }: { children: React.ReactNode }) => children, + }; +}); describe('AuthWrapper', () => { it('renders fallback when user is not authorized', () => { diff --git a/src/common/AuthWrapper.tsx b/src/common/AuthWrapper.tsx index 348d43df9d..bdafca4461 100644 --- a/src/common/AuthWrapper.tsx +++ b/src/common/AuthWrapper.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { observer } from 'mobx-react'; import { useProfileStore } from 'src/stores/Profile/profile.store'; -import type { Profile, UserRole } from 'oa-shared'; +import type { UserRole } from 'oa-shared'; /* Simple wrapper to only render a component if the user is logged in (plus optional user role required) @@ -17,9 +17,8 @@ interface IProps { export const AuthWrapper = observer((props: IProps) => { const { borderLess, children, roleRequired } = props; - const { profile } = useProfileStore(); - - const isAuthorized = isUserAuthorized(profile, roleRequired); + const { isUserAuthorized } = useProfileStore(); + const isAuthorized = isUserAuthorized(roleRequired); const childElements = roleRequired === 'beta-tester' && !borderLess ? ( @@ -30,21 +29,3 @@ export const AuthWrapper = observer((props: IProps) => { return <>{isAuthorized ? childElements : props.fallback || <>}; }); - -export const isUserAuthorized = (user?: Profile | null, roleRequired?: UserRole | UserRole[]) => { - const userRoles = user?.roles || []; - - // If no role required just check if user is logged in - if (!roleRequired || roleRequired.length === 0) { - return user ? true : false; - } - - const rolesRequired = Array.isArray(roleRequired) ? roleRequired : [roleRequired]; - - // otherwise use logged in user profile values - if (user && roleRequired) { - return userRoles.some((role) => rolesRequired.includes(role as UserRole)); - } - - return false; -}; diff --git a/src/pages/Library/Content/List/LibraryListHeader.tsx b/src/pages/Library/Content/List/LibraryListHeader.tsx index 5a6bbbcb02..225acf4ca3 100644 --- a/src/pages/Library/Content/List/LibraryListHeader.tsx +++ b/src/pages/Library/Content/List/LibraryListHeader.tsx @@ -166,7 +166,7 @@ export const LibraryListHeader = (props: IProps) => { return ( { const { draftCount, handleShowDrafts, showDrafts } = props; - - // const [categories, setCategories] = useState([]) - // const [searchString, setSearchString] = useState('') - - // const [searchParams, setSearchParams] = useSearchParams() - // const categoryParam = searchParams.get(NewsSearchParams.category) - // const category = - // (categoryParam && categories?.find((x) => x.id === +categoryParam)) ?? null - // const q = searchParams.get(NewsSearchParams.q) - // const sort = searchParams.get(NewsSearchParams.sort) as NewsSortOption - - // useEffect(() => { - // const initCategories = async () => { - // const categories = (await newsContentService.getCategories()) || [] - // setCategories(categories) - // } - - // initCategories() - // }, []) - - // useEffect(() => { - // setSearchString(q || '') - // }, [q]) - - // const updateFilter = useCallback( - // (key: NewsSearchParams, value: string) => { - // const params = new URLSearchParams(searchParams.toString()) - // if (value) { - // params.set(key, value) - // } else { - // params.delete(key) - // } - // setSearchParams(params) - // }, - // [searchParams], - // ) - - // const onSearchInputChange = useCallback( - // debounce((value: string) => { - // searchValue(value) - // }, 500), - // [searchParams], - // ) - - // const searchValue = (value: string) => { - // const params = new URLSearchParams(searchParams.toString()) - // params.set('q', value) - - // if (value.length > 0 && sort !== 'MostRelevant') { - // params.set('sort', 'MostRelevant') - // } - - // if (value.length === 0 || !value) { - // params.set('sort', 'Newest') - // } - - // setSearchParams(params) - // } - - const actionComponents = ( - - - - - - - } - loggedIn={ - - - - - - - } - loggedOut={<>} - /> - ); - - // const categoryComponent = ( - // - // updateFilter( - // NewsSearchParams.category, - // updatedCategory ? (updatedCategory as Category).id.toString() : '', - // ) - // } - // /> - // ) - - // const filteringComponents = ( - // - // - // - //