Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leftDrawerOrg: Fixed the org not found error on viewing admin profile. #2386

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/components/LeftDrawerOrg/LeftDrawerOrg.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import 'jest-localstorage-mock';
import { I18nextProvider } from 'react-i18next';
import { BrowserRouter } from 'react-router-dom';
import { BrowserRouter, MemoryRouter } from 'react-router-dom';

import i18nForTest from 'utils/i18nForTest';
import type { InterfaceLeftDrawerProps } from './LeftDrawerOrg';
Expand All @@ -21,6 +21,10 @@ const { setItem } = useLocalStorage();
const props: InterfaceLeftDrawerProps = {
orgId: '123',
targets: [
{
name: 'Admin Profile',
url: '/member/123',
},
{
name: 'Dashboard',
url: '/orgdash/123',
Expand Down Expand Up @@ -215,6 +219,20 @@ const MOCKS_EMPTY = [
},
];

const MOCKS_EMPTY_ORGID = [
{
request: {
query: ORGANIZATIONS_LIST,
variables: { id: '' },
},
result: {
data: {
organizations: [],
},
},
},
];

const defaultScreens = [
'Dashboard',
'People',
Expand Down Expand Up @@ -264,6 +282,7 @@ afterEach(() => {
const link = new StaticMockLink(MOCKS, true);
const linkImage = new StaticMockLink(MOCKS_WITH_IMAGE, true);
const linkEmpty = new StaticMockLink(MOCKS_EMPTY, true);
const linkEmptyOrgId = new StaticMockLink(MOCKS_EMPTY_ORGID, true);

describe('Testing LeftDrawerOrg component for SUPERADMIN', () => {
test('Component should be rendered properly', async () => {
Expand Down Expand Up @@ -308,6 +327,29 @@ describe('Testing LeftDrawerOrg component for SUPERADMIN', () => {
expect(screen.getByTestId(/orgBtn/i)).toBeInTheDocument();
});

test('Should not show org not found error when viewing admin profile', async () => {
setItem('UserImage', '');
setItem('SuperAdmin', true);
setItem('FirstName', 'John');
setItem('LastName', 'Doe');
setItem('id', '123');
render(
<MockedProvider addTypename={false} link={linkEmptyOrgId}>
<MemoryRouter initialEntries={['/member/123']}>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LeftDrawerOrg {...props} hideDrawer={null} />
</I18nextProvider>
</Provider>
</MemoryRouter>
</MockedProvider>,
);
await wait();
expect(
screen.queryByText(/Error occured while loading Organization data/i),
).not.toBeInTheDocument();
});

test('Testing Menu Buttons', async () => {
setItem('UserImage', '');
setItem('SuperAdmin', true);
Expand Down
29 changes: 24 additions & 5 deletions src/components/LeftDrawerOrg/LeftDrawerOrg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import IconComponent from 'components/IconComponent/IconComponent';
import React, { useEffect, useState } from 'react';
import Button from 'react-bootstrap/Button';
import { useTranslation } from 'react-i18next';
import { NavLink } from 'react-router-dom';
import { NavLink, useLocation } from 'react-router-dom';
import type { TargetsType } from 'state/reducers/routesReducer';
import type { InterfaceQueryOrganizationsListObject } from 'utils/interfaces';
import AngleRightIcon from 'assets/svgs/angleRight.svg?react';
import TalawaLogo from 'assets/svgs/talawa.svg?react';
import styles from './LeftDrawerOrg.module.css';
import Avatar from 'components/Avatar/Avatar';
import useLocalStorage from 'utils/useLocalstorage';

export interface InterfaceLeftDrawerProps {
orgId: string;
Expand All @@ -38,8 +39,16 @@ const leftDrawerOrg = ({
}: InterfaceLeftDrawerProps): JSX.Element => {
const { t: tCommon } = useTranslation('common');
const { t: tErrors } = useTranslation('errors');
const location = useLocation();
const { getItem } = useLocalStorage();
const userId = getItem('id');
const getIdFromPath = (pathname: string): string => {
const segments = pathname.split('/');
// Index 2 represents the ID in paths like /member/{userId}, /orgdash/{orgId}
return segments[2];
};
sethdivyansh marked this conversation as resolved.
Show resolved Hide resolved
const [isProfilePage, setIsProfilePage] = useState(false);
const [showDropdown, setShowDropdown] = useState(false);

const [organization, setOrganization] =
useState<InterfaceQueryOrganizationsListObject>();
const {
Expand All @@ -54,16 +63,26 @@ const leftDrawerOrg = ({
variables: { id: orgId },
});

// Check if the current page is admin profile page
useEffect(() => {
// id could be userId or orgId
const id = getIdFromPath(location.pathname);
// if param id is equal to userId, then it is a profile page
setIsProfilePage(id === userId);
}, [location, userId]);
sethdivyansh marked this conversation as resolved.
Show resolved Hide resolved

// Set organization data when query data is available
useEffect(() => {
let isMounted = true;
if (data && isMounted) {
setOrganization(data?.organizations[0]);
} else {
setOrganization(undefined);
sethdivyansh marked this conversation as resolved.
Show resolved Hide resolved
}
return () => {
isMounted = false;
};
}, [data]);
}, [data, location]);
sethdivyansh marked this conversation as resolved.
Show resolved Hide resolved

/**
* Handles link click to hide the drawer on smaller screens.
Expand Down Expand Up @@ -104,7 +123,7 @@ const leftDrawerOrg = ({
/>
</>
) : organization == undefined ? (
<>
!isProfilePage && (
<button
className={`${styles.profileContainer} bg-danger text-start text-white`}
disabled
Expand All @@ -114,7 +133,7 @@ const leftDrawerOrg = ({
</div>
{tErrors('errorLoading', { entity: 'Organization' })}
</button>
</>
)
) : (
<button className={styles.profileContainer} data-testid="OrgBtn">
<div className={styles.imageContainer}>
Expand Down
Loading