Skip to content

Commit

Permalink
refactor(resourceadm): Moving headercontext and gitea header
Browse files Browse the repository at this point in the history
  • Loading branch information
wrt95 committed Oct 4, 2024
1 parent c3f6f5e commit 321c38f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import classes from './ResourceAdmHeader.module.css';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import {
Expand All @@ -15,6 +16,7 @@ import { useLogoutMutation } from 'app-shared/hooks/mutations/useLogoutMutation'
import type { User } from 'app-shared/types/Repository';
import { useUrlParams } from '../../hooks/useUrlParams';
import { getAppName } from '../../utils/stringUtils';
import { GiteaHeader } from 'app-shared/components/GiteaHeader';

interface ResourceAdmHeaderProps {
organizations: Organization[];
Expand All @@ -34,6 +36,9 @@ export const ResourceAdmHeader = ({ organizations, user }: ResourceAdmHeaderProp
<DashboardHeaderMenu organizations={organizations} user={user} />
</StudioPageHeader.Right>
</StudioPageHeader.Main>
<StudioPageHeader.Sub>
<GiteaHeader menuOnlyHasRepository rightContentClassName={classes.extraPadding} />
</StudioPageHeader.Sub>
</StudioPageHeader>
);
};
Expand Down
19 changes: 19 additions & 0 deletions frontend/resourceadm/context/HeaderContext/HeaderContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { type Organization } from 'app-shared/types/Organization';
import { type User } from 'app-shared/types/Repository';

export enum SelectedContextType {
All = 'all',
Self = 'self',
None = 'none',
}

export type HeaderContextType = {
selectableOrgs?: Organization[];
user: User;
};

export const HeaderContext = React.createContext<HeaderContextType>({
selectableOrgs: undefined,
user: undefined,
});
1 change: 1 addition & 0 deletions frontend/resourceadm/context/HeaderContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HeaderContext, type HeaderContextType, SelectedContextType } from './HeaderContext';
3 changes: 0 additions & 3 deletions frontend/resourceadm/pages/PageLayout/PageLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { useEffect, useRef } from 'react';
import classes from './PageLayout.module.css';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import { userHasAccessToOrganization } from '../../utils/userUtils';
import { useOrganizationsQuery } from '../../hooks/queries';
import { useRepoStatusQuery, useUserQuery } from 'app-shared/hooks/queries';
import { GiteaHeader } from 'app-shared/components/GiteaHeader';
import { useUrlParams } from '../../hooks/useUrlParams';
import postMessages from 'app-shared/utils/postMessages';
import { MergeConflictModal } from '../../components/MergeConflictModal';
Expand Down Expand Up @@ -63,7 +61,6 @@ export const PageLayout = (): React.JSX.Element => {
<>
<MergeConflictModal ref={mergeConflictModalRef} org={org} repo={app} />
{organizations && user && <ResourceAdmHeader organizations={organizations} user={user} />}
<GiteaHeader menuOnlyHasRepository rightContentClassName={classes.extraPadding} />
<Outlet />
</>
);
Expand Down
144 changes: 95 additions & 49 deletions frontend/resourceadm/utils/userUtils/userUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,106 @@
import { SelectedContextType } from 'app-shared/navigation/main-header/Header';
import { userHasAccessToOrganization } from './index';
import { SelectedContextType } from 'resourceadm/context/HeaderContext';
import { getOrgNameByUsername, userHasAccessToOrganization } from './userUtils';
import { type Organization } from 'app-shared/types/Organization';

describe('userHasAccessToOrganization', () => {
it('should return true when context is self', () => {
const result = userHasAccessToOrganization({
org: SelectedContextType.Self,
orgs: [],
const mockOrg1: Organization = {
avatar_url: '',
id: 12,
username: 'ttd',
full_name: 'Test',
};
const mockOrg2: Organization = {
avatar_url: '',
id: 23,
username: 'unit-test-2',
full_name: 'unit-test-2',
};
const mockOrganizations: Organization[] = [mockOrg1, mockOrg2];

describe('userUtils', () => {
describe('userHasAccessToOrganization', () => {
it('should return true when context is self', () => {
const result = userHasAccessToOrganization({
org: SelectedContextType.Self,
orgs: [],
});

expect(result).toBe(true);
});

expect(result).toBe(true);
});
it('should return true when context is all', () => {
const result = userHasAccessToOrganization({
org: SelectedContextType.All,
orgs: [],
});

it('should return true when context is all', () => {
const result = userHasAccessToOrganization({
org: SelectedContextType.All,
orgs: [],
expect(result).toBe(true);
});

expect(result).toBe(true);
});
it('should return true when context id is present in orgs list', () => {
const result = userHasAccessToOrganization({
org: 'username1',
orgs: [
{
avatar_url: 'avatar_url',
description: '',
full_name: 'full_name',
id: 1,
location: '',
username: 'username1',
website: '',
},
],
});

it('should return true when context id is present in orgs list', () => {
const result = userHasAccessToOrganization({
org: 'username1',
orgs: [
{
avatar_url: 'avatar_url',
description: '',
full_name: 'full_name',
id: 1,
location: '',
username: 'username1',
website: '',
},
],
});

expect(result).toBe(true);
expect(result).toBe(true);
});

it('should return false when context id is not present in orgs list', () => {
const result = userHasAccessToOrganization({
org: 'username2',
orgs: [
{
avatar_url: 'avatar_url',
description: '',
full_name: 'full_name',
id: 1,
location: '',
username: 'username',
website: '',
},
],
});

expect(result).toBe(false);
});
});

it('should return false when context id is not present in orgs list', () => {
const result = userHasAccessToOrganization({
org: 'username2',
orgs: [
{
avatar_url: 'avatar_url',
description: '',
full_name: 'full_name',
id: 1,
location: '',
username: 'username',
website: '',
},
],
});

expect(result).toBe(false);
describe('getOrgNameByUsername', () => {
it('should return the full name of the organization when a matching username is found', () => {
const result = getOrgNameByUsername(mockOrg1.username, mockOrganizations);
expect(result).toBe(mockOrg1.full_name);
});

it('should return the username if full name is not available', () => {
const mockOrg3: Organization = {
username: 'org3',
full_name: '',
id: 12,
avatar_url: '',
};
const orgsWithoutFullName: Organization[] = [mockOrg3];
const result = getOrgNameByUsername(mockOrg3.username, orgsWithoutFullName);
expect(result).toBe(mockOrg3.username);
});

it('should return undefined if the organization is not found', () => {
const result = getOrgNameByUsername('nonexistent-org', mockOrganizations);
expect(result).toBeUndefined();
});

it('should return undefined if orgs array is undefined', () => {
const result = getOrgNameByUsername(mockOrg1.username, undefined);
expect(result).toBeUndefined();
});
});
});
2 changes: 1 addition & 1 deletion frontend/resourceadm/utils/userUtils/userUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectedContextType } from 'app-shared/navigation/main-header/Header';
import { SelectedContextType } from 'resourceadm/context/HeaderContext';
import type { Organization } from 'app-shared/types/Organization';

export const userHasAccessToOrganization = ({
Expand Down

0 comments on commit 321c38f

Please sign in to comment.