Skip to content

Commit

Permalink
Pull projectId from query params and redirect to login (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
jadmsaadaot authored Sep 15, 2023
1 parent 04523ec commit fb43469
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 64 deletions.
15 changes: 11 additions & 4 deletions met-web/src/components/engagement/form/ActionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getEngagementMetadata,
patchEngagementMetadata,
} from '../../../services/engagementMetadataService';
import { useNavigate, useParams } from 'react-router-dom';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { EngagementContext, EngagementForm, EngagementFormUpdate, EngagementParams } from './types';
import {
createDefaultEngagement,
Expand Down Expand Up @@ -55,6 +55,10 @@ export const ActionContext = createContext<EngagementContext>({

export const ActionProvider = ({ children }: { children: JSX.Element }) => {
const { engagementId } = useParams<EngagementParams>();
// get projectId from query params
const { search } = useLocation();
const searchParams = new URLSearchParams(search);
const projectId = searchParams.get('projectId');
const navigate = useNavigate();
const dispatch = useAppDispatch();

Expand All @@ -65,7 +69,10 @@ export const ActionProvider = ({ children }: { children: JSX.Element }) => {
const [loadingAuthorization, setLoadingAuthorization] = useState(true);

const [savedEngagement, setSavedEngagement] = useState<Engagement>(createDefaultEngagement());
const [engagementMetadata, setEngagementMetadata] = useState<EngagementMetadata>(createDefaultEngagementMetadata());
const [engagementMetadata, setEngagementMetadata] = useState<EngagementMetadata>({
...createDefaultEngagementMetadata(),
project_id: projectId ?? '',
});
const [bannerImage, setBannerImage] = useState<File | null>();
const [savedBannerImageFileName, setSavedBannerImageFileName] = useState('');
const isCreate = window.location.pathname.includes(CREATE);
Expand Down Expand Up @@ -105,8 +112,8 @@ export const ActionProvider = ({ children }: { children: JSX.Element }) => {
}

try {
const engagement = await getEngagementMetadata(Number(engagementId));
setEngagementMetadata(engagement);
const engagementMetaData = await getEngagementMetadata(Number(engagementId));
setEngagementMetadata(engagementMetaData);
} catch (err) {
console.log(err);
dispatch(openNotification({ severity: 'error', text: 'Error Fetching Engagement Metadata' }));
Expand Down
12 changes: 12 additions & 0 deletions met-web/src/routes/RedirectLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { useEffect } from 'react';
import { MidScreenLoader } from 'components/common';
import UserService from 'services/userService';

export const RedirectLogin = () => {
const fullUrl = window.location.href;

useEffect(() => {
UserService.doLogin(fullUrl);
}, []);
return <MidScreenLoader />;
};
2 changes: 2 additions & 0 deletions met-web/src/routes/UnauthenticatedRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Landing from 'components/landing';
import ManageSubscription from '../components/engagement/view/widgets/Subscribe/ManageSubscription';
import { FormCAC } from 'components/FormCAC';
import ScrollToTop from 'components/scrollToTop';
import { RedirectLogin } from './RedirectLogin';
const UnauthenticatedRoutes = () => {
return (
<>
Expand All @@ -30,6 +31,7 @@ const UnauthenticatedRoutes = () => {
<Route path="/:slug/edit/:token" element={<EditSurvey />} />
<Route path="/surveys/submit/:surveyId/:token" element={<SurveySubmit />} />
<Route path="/engagements/:engagementId/cacform/:widgetId" element={<FormCAC />} />
<Route path="/engagements/create/form" element={<RedirectLogin />} />
<Route path="*" element={<NotFound />} />
<Route path="/not-found" element={<NotFound />} />
</Routes>
Expand Down
2 changes: 1 addition & 1 deletion met-web/src/services/userService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const userLogout = () => {
doLogout();
};

const doLogin = () => KeycloakData.login({ redirectUri: getBaseUrl() });
const doLogin = (redirectUri?: string) => KeycloakData.login({ redirectUri: redirectUri ?? getBaseUrl() });

const doLogout = async () => KeycloakData.logout({ redirectUri: getBaseUrl() });
const getToken = () => KeycloakData.token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jest.mock('@mui/material', () => ({
},
}));

jest.mock('axios')
jest.mock('axios');

jest.mock('components/common', () => ({
...jest.requireActual('components/common'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ jest.mock('apiManager/apiSlices/widgets', () => ({
useSortWidgetsMutation: () => [jest.fn(() => Promise.resolve())],
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

describe('Engagement form page tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
jest.spyOn(teamMemberService, 'getTeamMembers').mockReturnValue(Promise.resolve([mockTeamMember1]));
const useParamsMock = jest.spyOn(reactRouter, 'useParams');
jest.spyOn(engagementService, 'getEngagement').mockReturnValue(Promise.resolve(draftEngagement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ jest.mock('components/map', () => () => {
return <Box></Box>;
});

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: 'test project id' };
}),
useNavigate: () => jest.fn(),
}));

jest.mock('apiManager/apiSlices/widgets', () => ({
...jest.requireActual('apiManager/apiSlices/widgets'),
useCreateWidgetMutation: () => [jest.fn(() => Promise.resolve())],
Expand All @@ -61,7 +70,6 @@ Object.defineProperty(window, 'location', {

describe('Engagement form page tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
const openNotificationMock = jest.spyOn(notificationSlice, 'openNotification').mockImplementation(jest.fn());
const useParamsMock = jest.spyOn(reactRouter, 'useParams');
const getEngagementMetadataMock = jest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ jest.mock('apiManager/apiSlices/widgets', () => ({
useSortWidgetsMutation: () => [jest.fn(() => Promise.resolve())],
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

// Mocking window.location.pathname in Jest
Object.defineProperty(window, 'location', {
value: {
Expand All @@ -76,7 +85,6 @@ Object.defineProperty(window, 'location', {

describe('Engagement form page tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
const openNotificationModalMock = jest
.spyOn(notificationModalSlice, 'openNotificationModal')
.mockImplementation(jest.fn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ jest.mock('apiManager/apiSlices/widgets', () => ({
useSortWidgetsMutation: () => [jest.fn(() => Promise.resolve())],
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

// Mocking window.location.pathname in Jest
Object.defineProperty(window, 'location', {
value: {
Expand All @@ -73,7 +82,6 @@ Object.defineProperty(window, 'location', {

describe('Engagement form page tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
const openNotificationModalMock = jest
.spyOn(notificationModalSlice, 'openNotificationModal')
.mockImplementation(jest.fn());
Expand Down
35 changes: 23 additions & 12 deletions met-web/tests/unit/components/widgets/DocumentWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import * as notificationSlice from 'services/notificationService/notificationSli
import * as engagementMetadataService from 'services/engagementMetadataService';
import * as membershipService from 'services/membershipService';
import * as engagementSettingService from 'services/engagementSettingService';
import { createDefaultEngagement, createDefaultEngagementSettings, Engagement, EngagementSettings } from 'models/engagement';
import {
createDefaultEngagement,
createDefaultEngagementSettings,
Engagement,
EngagementSettings,
} from 'models/engagement';
import { EngagementStatus } from 'constants/engagementStatus';
import { Widget, WidgetType } from 'models/widget';
import { DocumentItem } from 'models/document';
Expand All @@ -39,9 +44,8 @@ const engagement: Engagement = {
};

const mockEngagementSettings: EngagementSettings = {
...createDefaultEngagementSettings()
}

...createDefaultEngagementSettings(),
};

const mockFile: DocumentItem = {
id: 1,
Expand All @@ -65,7 +69,16 @@ const documentWidget: Widget = {
items: [],
};

jest.mock('axios')
jest.mock('axios');

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
Expand Down Expand Up @@ -97,16 +110,14 @@ jest.mock('components/map', () => () => {

describe('Document widget in engagement page tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
jest.spyOn(notificationSlice, 'openNotification').mockImplementation(jest.fn());
jest.spyOn(engagementService, 'getEngagement').mockReturnValue(Promise.resolve(engagement));
jest.spyOn(documentService, 'fetchDocuments').mockReturnValue(Promise.resolve([mockFolder]));
jest.spyOn(engagementMetadataService, 'getEngagementMetadata')
.mockReturnValue(Promise.resolve(engagementMetadata));
jest.spyOn(membershipService, 'getTeamMembers')
.mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings')
.mockReturnValue(Promise.resolve(mockEngagementSettings));
jest.spyOn(engagementMetadataService, 'getEngagementMetadata').mockReturnValue(Promise.resolve(engagementMetadata));
jest.spyOn(membershipService, 'getTeamMembers').mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings').mockReturnValue(
Promise.resolve(mockEngagementSettings),
);
const getWidgetsMock = jest.spyOn(widgetService, 'getWidgets').mockReturnValue(Promise.resolve([]));
const useParamsMock = jest.spyOn(reactRouter, 'useParams');

Expand Down
27 changes: 17 additions & 10 deletions met-web/tests/unit/components/widgets/EventsWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jest.mock('components/map', () => () => {
});

const mockEngagementSettings: EngagementSettings = {
...createDefaultEngagementSettings()
}
...createDefaultEngagementSettings(),
};

jest.mock('axios')
jest.mock('axios');

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
Expand All @@ -36,6 +36,15 @@ jest.mock('react-redux', () => ({
}),
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

jest.mock('@reduxjs/toolkit/query/react', () => ({
...jest.requireActual('@reduxjs/toolkit/query/react'),
fetchBaseQuery: jest.fn(),
Expand Down Expand Up @@ -79,18 +88,16 @@ jest.mock('apiManager/apiSlices/widgets', () => ({

describe('Event Widget tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
const useParamsMock = jest.spyOn(reactRouter, 'useParams');
const getEngagementMock = jest
.spyOn(engagementService, 'getEngagement')
.mockReturnValue(Promise.resolve(draftEngagement));
const getWidgetsMock = jest.spyOn(widgetService, 'getWidgets').mockReturnValue(Promise.resolve([eventWidget]));
jest.spyOn(engagementMetadataService, 'getEngagementMetadata')
.mockReturnValue(Promise.resolve(engagementMetadata));
jest.spyOn(membershipService, 'getTeamMembers')
.mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings')
.mockReturnValue(Promise.resolve(mockEngagementSettings));
jest.spyOn(engagementMetadataService, 'getEngagementMetadata').mockReturnValue(Promise.resolve(engagementMetadata));
jest.spyOn(membershipService, 'getTeamMembers').mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings').mockReturnValue(
Promise.resolve(mockEngagementSettings),
);

beforeEach(() => {
setupEnv();
Expand Down
27 changes: 17 additions & 10 deletions met-web/tests/unit/components/widgets/MapWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { USER_ROLES } from 'services/userService/constants';
import { EngagementSettings, createDefaultEngagementSettings } from 'models/engagement';

const mockEngagementSettings: EngagementSettings = {
...createDefaultEngagementSettings()
}
...createDefaultEngagementSettings(),
};

jest.mock('components/map', () => () => {
return <div></div>;
});

jest.mock('axios')
jest.mock('axios');

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
Expand Down Expand Up @@ -77,21 +77,28 @@ jest.mock('apiManager/apiSlices/widgets', () => ({
useSortWidgetsMutation: () => [jest.fn(() => Promise.resolve())],
}));

jest.spyOn(engagementMetadataService, 'getEngagementMetadata')
.mockReturnValue(Promise.resolve(engagementMetadata));
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(() => ({ search: '' })),
useParams: jest.fn(() => {
return { projectId: '' };
}),
useNavigate: () => jest.fn(),
}));

jest.spyOn(engagementMetadataService, 'getEngagementMetadata').mockReturnValue(Promise.resolve(engagementMetadata));

describe('Map Widget tests', () => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => jest.fn());
jest.spyOn(reactRouter, 'useNavigate').mockImplementation(() => jest.fn());
const useParamsMock = jest.spyOn(reactRouter, 'useParams');
const getEngagementMock = jest
.spyOn(engagementService, 'getEngagement')
.mockReturnValue(Promise.resolve(draftEngagement));
const getWidgetsMock = jest.spyOn(widgetService, 'getWidgets').mockReturnValue(Promise.resolve([mapWidget]));
jest.spyOn(membershipService, 'getTeamMembers')
.mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings')
.mockReturnValue(Promise.resolve(mockEngagementSettings));
jest.spyOn(membershipService, 'getTeamMembers').mockReturnValue(Promise.resolve([]));
jest.spyOn(engagementSettingService, 'getEngagementSettings').mockReturnValue(
Promise.resolve(mockEngagementSettings),
);

beforeEach(() => {
setupEnv();
Expand Down
Loading

0 comments on commit fb43469

Please sign in to comment.