Skip to content

Commit

Permalink
Prefetch office staff and cache on login
Browse files Browse the repository at this point in the history
* Async called called API for office staff in Session component

Jira ticket: CAMS-478

Co-authored-by: Arthur Morrow <[email protected]>
Co-authored-by: James Brooks <[email protected]>
Co-authored-by: Brian Posey <[email protected]>,
  • Loading branch information
3 people committed Nov 8, 2024
1 parent 5ea9eb0 commit b74480b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion user-interface/src/login/Session.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { render } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import { BrowserRouter, MemoryRouter } from 'react-router-dom';
import * as reactRouter from 'react-router';
import { CamsSession } from '@common/cams/session';
import LocalStorage from '@/lib/utils/local-storage';
import { LOGIN_PATHS, LOGIN_SUCCESS_PATH } from './login-library';
import { Session, SessionProps } from './Session';
import { MockData } from '@common/cams/test-utilities/mock-data';
import Api2 from '@/lib/models/api2';
import { USTP_OFFICE_DATA_MAP } from '@common/cams/offices';

describe('Session', () => {
const testSession: CamsSession = {
user: {
id: 'mockId',
name: 'Mock User',
offices: [USTP_OFFICE_DATA_MAP.get('USTP_CAMS_Region_2_Office_Manhattan')!],
},
provider: 'mock',
accessToken: MockData.getJwt(),
Expand All @@ -34,6 +37,14 @@ describe('Session', () => {
);
}

test('should prefetch office staff for each division the user is assigned to', () => {
const getOfficeAttorneys = vi.spyOn(Api2, 'getOfficeAttorneys');
renderWithProps();
waitFor(() => {
expect(getOfficeAttorneys).toHaveBeenCalledTimes(testSession.user.offices!.length);
});
});

test('should write the session to local storage', () => {
const setSession = vi.spyOn(LocalStorage, 'setSession');
renderWithProps();
Expand Down
7 changes: 7 additions & 0 deletions user-interface/src/login/Session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ export function useStateAndActions() {
errorMessage: '',
});

function postLoginTasks(session: CamsSession) {
session.user.offices?.forEach((office) => {
Api2.getOfficeAttorneys(office.officeCode);
});
}

function getMe() {
if (state.isLoaded) return;
const newState = { ...state };
Api2.getMe()
.then((response) => {
const session = response.data;
LocalStorage.setSession(session);
postLoginTasks(session);
newState.isLoaded = true;
})
.catch((error) => {
Expand Down

0 comments on commit b74480b

Please sign in to comment.