From 11c73c388e3077f6cb3440d3ec4e099a1b247075 Mon Sep 17 00:00:00 2001 From: MatthewPopat-NHS Date: Thu, 18 Dec 2025 11:05:35 +0000 Subject: [PATCH 1/6] Stopped role selection from editing rolesWithAccess data Removed access related calculated fields from local storage Simplified rendering of role selection page --- ...eps-prescription-tracker-ui.code-workspace | 2 +- .../common/commonTypes/src/trackerUserInfo.ts | 2 - .../cpt-ui/__tests__/AuthProvider.test.tsx | 10 +- .../__tests__/BasicDetailsSearch.test.tsx | 2 - .../BasicDetailsSearchResultsPage.test.tsx | 2 - packages/cpt-ui/__tests__/EpsHeader.test.tsx | 2 - .../__tests__/EpsPrescriptionList.test.tsx | 2 - .../__tests__/EpsRoleSelectionPage.test.tsx | 43 ++- .../EpsSearchForAPrescription.test.tsx | 2 - packages/cpt-ui/__tests__/LoginPage.test.tsx | 2 - packages/cpt-ui/__tests__/LogoutPage.test.tsx | 2 - .../PrescriptionDetailsPage.test.tsx | 2 - .../__tests__/PrescriptionIdSearch.test.tsx | 2 - .../__tests__/SessionLoggedOutPage.test.tsx | 2 - .../__tests__/SessionSelectionPage.test.tsx | 2 - .../__tests__/YourSelectedRole.test.tsx | 2 - packages/cpt-ui/__tests__/userInfo.test.tsx | 26 +- packages/cpt-ui/src/components/EpsHeader.tsx | 4 +- .../src/components/EpsRoleSelectionPage.tsx | 167 ++++------ packages/cpt-ui/src/context/AuthProvider.tsx | 21 +- packages/cpt-ui/src/helpers/userInfo.tsx | 8 +- packages/selectedRoleLambda/src/handler.ts | 62 ---- .../tests/test_handler.test.ts | 295 ++---------------- 23 files changed, 127 insertions(+), 537 deletions(-) diff --git a/.vscode/eps-prescription-tracker-ui.code-workspace b/.vscode/eps-prescription-tracker-ui.code-workspace index 357d934377..4dd24e7aee 100644 --- a/.vscode/eps-prescription-tracker-ui.code-workspace +++ b/.vscode/eps-prescription-tracker-ui.code-workspace @@ -67,7 +67,7 @@ { "name": "packages/common/lambdaUtils", "path": "../packages/common/lambdaUtils" - } + }, { "name": "packages/common/testing", "path": "../packages/common/testing" diff --git a/packages/common/commonTypes/src/trackerUserInfo.ts b/packages/common/commonTypes/src/trackerUserInfo.ts index 3fc53d8830..e5bbc19a2e 100644 --- a/packages/common/commonTypes/src/trackerUserInfo.ts +++ b/packages/common/commonTypes/src/trackerUserInfo.ts @@ -30,10 +30,8 @@ export type TrackerUserInfo = { export type TrackerUserInfoResult = { rolesWithAccess: Array, rolesWithoutAccess: Array, - hasNoAccess: boolean selectedRole: RoleDetails | undefined, userDetails: UserDetails | undefined, - hasSingleRoleAccess: boolean, isConcurrentSession: boolean, invalidSessionCause: string | undefined, sessionId: string | undefined, diff --git a/packages/cpt-ui/__tests__/AuthProvider.test.tsx b/packages/cpt-ui/__tests__/AuthProvider.test.tsx index a9ba497966..9bfb88f76a 100644 --- a/packages/cpt-ui/__tests__/AuthProvider.test.tsx +++ b/packages/cpt-ui/__tests__/AuthProvider.test.tsx @@ -41,10 +41,8 @@ const userDetails = { const mockUserInfo = { rolesWithAccess: rolesWithAccess, rolesWithoutAccess: [], - hasNoAccess: false, selectedRole: currentlySelectedRole, userDetails: userDetails, - hasSingleRoleAccess: true, error: undefined } @@ -142,10 +140,8 @@ const TestConsumer = () => {
{auth.isSigningIn.toString()}
{JSON.stringify(auth.rolesWithAccess, null, 2)}
{JSON.stringify(auth.rolesWithoutAccess, null, 2)}
-
{auth.hasNoAccess.toString()}
{JSON.stringify(auth.selectedRole, null, 2)}
{JSON.stringify(auth.userDetails, null, 2)}
-
{auth.hasSingleRoleAccess.toString()}
) } @@ -265,10 +261,8 @@ describe("AuthProvider", () => { expect(screen.getByTestId("user").textContent).toBe("test_user") expect(screen.getByTestId("rolesWithAccess").textContent).toBe(JSON.stringify(rolesWithAccess, null, 2)) expect(screen.getByTestId("rolesWithoutAccess").textContent).toBe("[]") - expect(screen.getByTestId("noAccess").textContent).toBe("false") expect(screen.getByTestId("selectedRole").textContent).toBe(JSON.stringify(currentlySelectedRole, null, 2)) expect(screen.getByTestId("userDetails").textContent).toBe(JSON.stringify(userDetails, null, 2)) - expect(screen.getByTestId("singleAccess").textContent).toBe("true") }) }) @@ -326,10 +320,8 @@ describe("AuthProvider", () => { expect(screen.getByTestId("error").textContent).toBe("") expect(screen.getByTestId("rolesWithAccess").textContent).toBe("[]") expect(screen.getByTestId("rolesWithoutAccess").textContent).toBe("[]") - expect(screen.getByTestId("noAccess").textContent).toBe("true") expect(screen.getByTestId("selectedRole").textContent).toBe("") expect(screen.getByTestId("userDetails").textContent).toBe("") - expect(screen.getByTestId("singleAccess").textContent).toBe("false") }) }) @@ -395,7 +387,7 @@ describe("AuthProvider", () => { }; (updateRemoteSelectedRole as jest.Mock).mockResolvedValue({ - rolesWithAccess: [newRole] + currentlySelectedRole: newRole }) let contextValue: AuthContextType | null diff --git a/packages/cpt-ui/__tests__/BasicDetailsSearch.test.tsx b/packages/cpt-ui/__tests__/BasicDetailsSearch.test.tsx index cf53f88049..eb00c86420 100644 --- a/packages/cpt-ui/__tests__/BasicDetailsSearch.test.tsx +++ b/packages/cpt-ui/__tests__/BasicDetailsSearch.test.tsx @@ -45,8 +45,6 @@ const signedInAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/BasicDetailsSearchResultsPage.test.tsx b/packages/cpt-ui/__tests__/BasicDetailsSearchResultsPage.test.tsx index 5f584be7dd..b99a374f9b 100644 --- a/packages/cpt-ui/__tests__/BasicDetailsSearchResultsPage.test.tsx +++ b/packages/cpt-ui/__tests__/BasicDetailsSearchResultsPage.test.tsx @@ -50,8 +50,6 @@ const mockAuthContext: AuthContextType = { invalidSessionCause: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/EpsHeader.test.tsx b/packages/cpt-ui/__tests__/EpsHeader.test.tsx index 1173d8ff38..33124da518 100644 --- a/packages/cpt-ui/__tests__/EpsHeader.test.tsx +++ b/packages/cpt-ui/__tests__/EpsHeader.test.tsx @@ -54,8 +54,6 @@ const defaultAuthContext: AuthContextType = { invalidSessionCause: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/EpsPrescriptionList.test.tsx b/packages/cpt-ui/__tests__/EpsPrescriptionList.test.tsx index f37ccd4f09..fc1f7b075e 100644 --- a/packages/cpt-ui/__tests__/EpsPrescriptionList.test.tsx +++ b/packages/cpt-ui/__tests__/EpsPrescriptionList.test.tsx @@ -137,8 +137,6 @@ const signedInAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/EpsRoleSelectionPage.test.tsx b/packages/cpt-ui/__tests__/EpsRoleSelectionPage.test.tsx index 7d463b5664..c7851243f1 100644 --- a/packages/cpt-ui/__tests__/EpsRoleSelectionPage.test.tsx +++ b/packages/cpt-ui/__tests__/EpsRoleSelectionPage.test.tsx @@ -13,6 +13,7 @@ import {FRONTEND_PATHS} from "@/constants/environment" import {getSearchParams} from "@/helpers/getSearchParams" import {handleRestartLogin} from "@/helpers/logout" import axios from "axios" +import {RoleDetails} from "@cpt-ui-common/common-types" jest.mock("@/context/AuthProvider") jest.mock("@/helpers/getSearchParams") @@ -89,7 +90,6 @@ describe("RoleSelectionPage", () => { it("renders loading spinner if redirecting during sign in", () => { mockUseAuth.mockReturnValue({ isSigningIn: true, - hasNoAccess: false, rolesWithAccess: [], rolesWithoutAccess: [], error: null, @@ -107,7 +107,6 @@ describe("RoleSelectionPage", () => { it("renders error message if auth.error exists", () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasNoAccess: false, rolesWithAccess: [], rolesWithoutAccess: [], error: "Something went wrong" @@ -118,10 +117,9 @@ describe("RoleSelectionPage", () => { expect(screen.getByText("Something went wrong")).toBeInTheDocument() }) - it("renders titleNoAccess and captionNoAccess when hasNoAccess is true", () => { + it("renders titleNoAccess and captionNoAccess when rolesWithAccess is empty", () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasNoAccess: true, rolesWithAccess: [], rolesWithoutAccess: [], error: null @@ -139,8 +137,6 @@ describe("RoleSelectionPage", () => { mockUseAuth.mockReturnValue({ isSigningIn: true, isSignedIn: false, - hasSingleRoleAccess: true, - hasNoAccess: false, rolesWithAccess: [], rolesWithoutAccess: [], selectedRole: null, @@ -157,16 +153,21 @@ describe("RoleSelectionPage", () => { expect(navigateMock).toHaveBeenCalledWith(FRONTEND_PATHS.LOGIN) }) - it("redirects if user hasSingleRoleAccess", () => { + it("redirects if user has single roleWithAccess", () => { const navigateMock = jest.fn() mockNavigate.mockReturnValue(navigateMock) mockUseAuth.mockReturnValue({ isSigningIn: false, isSignedIn: true, - hasSingleRoleAccess: true, - hasNoAccess: false, - rolesWithAccess: [], + rolesWithAccess: [ + { + role_id: "1", + role_name: "Pharmacist", + org_code: "ABC", + org_name: "Pharmacy Org" + } + ], rolesWithoutAccess: [], selectedRole: null, error: null @@ -180,8 +181,6 @@ describe("RoleSelectionPage", () => { it("renders login info when selectedRole is present", () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasSingleRoleAccess: false, - hasNoAccess: false, selectedRole: { org_name: "Test Org", org_code: "TEST123", @@ -202,7 +201,6 @@ describe("RoleSelectionPage", () => { it("renders roles without access in table", () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasNoAccess: false, rolesWithAccess: [], rolesWithoutAccess: [ { @@ -224,7 +222,6 @@ describe("RoleSelectionPage", () => { it("renders EpsCard components for roles with access", () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasNoAccess: false, selectedRole: { role_id: "1" }, @@ -265,7 +262,6 @@ describe("RoleSelectionPage", () => { it("navigates on confirm and continue button click", async () => { mockUseAuth.mockReturnValue({ isSigningIn: false, - hasNoAccess: false, selectedRole: { role_id: "1", org_name: "Pharmacy A", @@ -284,7 +280,6 @@ describe("RoleSelectionPage", () => { expect(button).toBeEnabled() fireEvent.click(button) - expect(mockNavigate.mock.calls.length).toBe(5) expect(mockNavigate).toHaveBeenCalledWith("/continue") }) @@ -293,11 +288,9 @@ describe("RoleSelectionPage", () => { const authState = { isSigningIn: true, isSignedIn: false, - hasSingleRoleAccess: false, - hasNoAccess: false, - rolesWithAccess: [], + rolesWithAccess: [] as Array, rolesWithoutAccess: [], - selectedRole: null, + selectedRole: undefined as RoleDetails | undefined, error: null, clearAuthState: jest.fn() } @@ -317,8 +310,14 @@ describe("RoleSelectionPage", () => { // Step 2: Simulate login complete and role assignment act(() => { authState.isSigningIn = false - authState.isSignedIn = true - authState.hasSingleRoleAccess = true + const role = { + role_id: "2", + role_name: "Pharmacist", + org_code: "ABC", + org_name: "Pharmacy Org" + } + authState.rolesWithAccess = [role] + authState.selectedRole = role authState.isSignedIn = true mockUseAuth.mockReturnValue(authState) }) diff --git a/packages/cpt-ui/__tests__/EpsSearchForAPrescription.test.tsx b/packages/cpt-ui/__tests__/EpsSearchForAPrescription.test.tsx index 3f4a21d0f5..f38e6e6199 100644 --- a/packages/cpt-ui/__tests__/EpsSearchForAPrescription.test.tsx +++ b/packages/cpt-ui/__tests__/EpsSearchForAPrescription.test.tsx @@ -17,8 +17,6 @@ const defaultAuthContext: AuthContextType = { invalidSessionCause: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/LoginPage.test.tsx b/packages/cpt-ui/__tests__/LoginPage.test.tsx index b14067ac99..736ffe6fd5 100644 --- a/packages/cpt-ui/__tests__/LoginPage.test.tsx +++ b/packages/cpt-ui/__tests__/LoginPage.test.tsx @@ -89,8 +89,6 @@ const defaultAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/LogoutPage.test.tsx b/packages/cpt-ui/__tests__/LogoutPage.test.tsx index b8e1d92edb..567176f043 100644 --- a/packages/cpt-ui/__tests__/LogoutPage.test.tsx +++ b/packages/cpt-ui/__tests__/LogoutPage.test.tsx @@ -69,8 +69,6 @@ const MockAuthProvider = ({ invalidSessionCause: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/PrescriptionDetailsPage.test.tsx b/packages/cpt-ui/__tests__/PrescriptionDetailsPage.test.tsx index 51143d6c10..bcced788a1 100644 --- a/packages/cpt-ui/__tests__/PrescriptionDetailsPage.test.tsx +++ b/packages/cpt-ui/__tests__/PrescriptionDetailsPage.test.tsx @@ -27,8 +27,6 @@ const defaultAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/PrescriptionIdSearch.test.tsx b/packages/cpt-ui/__tests__/PrescriptionIdSearch.test.tsx index b4f5efb905..b2f6a2941f 100644 --- a/packages/cpt-ui/__tests__/PrescriptionIdSearch.test.tsx +++ b/packages/cpt-ui/__tests__/PrescriptionIdSearch.test.tsx @@ -40,8 +40,6 @@ const mockAuthContext: AuthContextType = { invalidSessionCause: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: false, diff --git a/packages/cpt-ui/__tests__/SessionLoggedOutPage.test.tsx b/packages/cpt-ui/__tests__/SessionLoggedOutPage.test.tsx index e34b16a80c..1b4dcbe42a 100644 --- a/packages/cpt-ui/__tests__/SessionLoggedOutPage.test.tsx +++ b/packages/cpt-ui/__tests__/SessionLoggedOutPage.test.tsx @@ -44,8 +44,6 @@ export const defaultAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: true, diff --git a/packages/cpt-ui/__tests__/SessionSelectionPage.test.tsx b/packages/cpt-ui/__tests__/SessionSelectionPage.test.tsx index ad50771ec0..8e2264b418 100644 --- a/packages/cpt-ui/__tests__/SessionSelectionPage.test.tsx +++ b/packages/cpt-ui/__tests__/SessionSelectionPage.test.tsx @@ -58,8 +58,6 @@ export const defaultAuthState: AuthContextType = { error: null, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, selectedRole: undefined, userDetails: undefined, isConcurrentSession: true, diff --git a/packages/cpt-ui/__tests__/YourSelectedRole.test.tsx b/packages/cpt-ui/__tests__/YourSelectedRole.test.tsx index eb86399afc..efeab91cd0 100644 --- a/packages/cpt-ui/__tests__/YourSelectedRole.test.tsx +++ b/packages/cpt-ui/__tests__/YourSelectedRole.test.tsx @@ -21,8 +21,6 @@ const mockUseAuthReturnValue: AuthContextType = { sessionId: undefined, rolesWithAccess: [], rolesWithoutAccess: [], - hasNoAccess: false, - hasSingleRoleAccess: false, userDetails: undefined, isConcurrentSession: false, cognitoSignIn: jest.fn(), diff --git a/packages/cpt-ui/__tests__/userInfo.test.tsx b/packages/cpt-ui/__tests__/userInfo.test.tsx index 89d5e53a80..902e50d3f8 100644 --- a/packages/cpt-ui/__tests__/userInfo.test.tsx +++ b/packages/cpt-ui/__tests__/userInfo.test.tsx @@ -5,8 +5,6 @@ jest.mock("@/helpers/axios") const mockedAxios = axios as jest.Mocked type ExpectedResult = { - hasNoAccess: boolean, - hasSingleRoleAccess: boolean, numberOfCallsToUpdateSelectedRole: number } type TestCase = { @@ -48,8 +46,6 @@ describe("getTrackerUserInfo", () => { given_name: "GIVEN" }, expected: { - hasNoAccess: false, - hasSingleRoleAccess: true, numberOfCallsToUpdateSelectedRole: 0 } }, @@ -71,8 +67,6 @@ describe("getTrackerUserInfo", () => { given_name: "JOHN" }, expected: { - hasNoAccess: true, - hasSingleRoleAccess: false, numberOfCallsToUpdateSelectedRole: 0 } }, @@ -107,8 +101,6 @@ describe("getTrackerUserInfo", () => { given_name: "ANNA" }, expected: { - hasNoAccess: false, - hasSingleRoleAccess: false, numberOfCallsToUpdateSelectedRole: 0 } } @@ -141,15 +133,13 @@ describe("getTrackerUserInfo", () => { expect(result.rolesWithAccess).toStrictEqual(rolesWithAccess) expect(result.rolesWithoutAccess).toStrictEqual(rolesWithoutAccess) expect(result.userDetails).toStrictEqual(userDetails) - expect(result.hasNoAccess).toBe(expected.hasNoAccess) - expect(result.hasSingleRoleAccess).toBe(expected.hasSingleRoleAccess) expect(result.error).toBeNull() expect(mockedAxios.put).toHaveBeenCalledTimes(expected.numberOfCallsToUpdateSelectedRole) } ) describe("updateRemoteSelectedRole", () => { - it("should successfully update selected role and return rolesWithAccess", async () => { + it("should successfully update selected role and return currentlySelectedRole", async () => { const newRole = { role_id: "ROLE456", role_name: "Admin", @@ -158,28 +148,18 @@ describe("getTrackerUserInfo", () => { site_address: "456 Admin Street" } - const expectedRolesWithAccess = [ - { - role_id: "ROLE456", - role_name: "Admin", - org_name: "Admin Org", - org_code: "ORG456", - site_address: "456 Admin Street" - } - ] - mockedAxios.put.mockResolvedValue({ status: 200, data: { userInfo: { - rolesWithAccess: expectedRolesWithAccess + currentlySelectedRole: newRole } } }) const result = await updateRemoteSelectedRole(newRole) - expect(result.rolesWithAccess).toEqual(expectedRolesWithAccess) + expect(result.currentlySelectedRole).toEqual(newRole) }) it("should throw error when server returns non-200 status", async () => { diff --git a/packages/cpt-ui/src/components/EpsHeader.tsx b/packages/cpt-ui/src/components/EpsHeader.tsx index 3eaa3df41d..418adafc4a 100644 --- a/packages/cpt-ui/src/components/EpsHeader.tsx +++ b/packages/cpt-ui/src/components/EpsHeader.tsx @@ -60,7 +60,6 @@ export default function EpsHeader() { path !== FRONTEND_PATHS.LOGOUT && path !== FRONTEND_PATHS.SESSION_LOGGED_OUT && isSignedIn && - !authContext.hasSingleRoleAccess && !authContext.selectedRole ) @@ -71,7 +70,6 @@ export default function EpsHeader() { path !== FRONTEND_PATHS.CHANGE_YOUR_ROLE && path !== FRONTEND_PATHS.LOGOUT && isSignedIn && - !authContext.hasSingleRoleAccess && authContext.selectedRole !== undefined ) @@ -81,7 +79,7 @@ export default function EpsHeader() { // Show the "Exit" button under these conditions setShouldShowExitButton( (path === FRONTEND_PATHS.LOGOUT && !auth?.isSignedIn) || - (path === FRONTEND_PATHS.SELECT_YOUR_ROLE && authContext.hasNoAccess) || + (path === FRONTEND_PATHS.SELECT_YOUR_ROLE && authContext.rolesWithAccess.length === 0) || (path === "/notfound") ) }, [location, auth, authContext]) diff --git a/packages/cpt-ui/src/components/EpsRoleSelectionPage.tsx b/packages/cpt-ui/src/components/EpsRoleSelectionPage.tsx index 838692ad7f..6b8bd402e8 100644 --- a/packages/cpt-ui/src/components/EpsRoleSelectionPage.tsx +++ b/packages/cpt-ui/src/components/EpsRoleSelectionPage.tsx @@ -1,4 +1,4 @@ -import React, {useState, useEffect, useRef} from "react" +import React from "react" import {useNavigate} from "react-router-dom" import { Container, @@ -84,22 +84,49 @@ export default function RoleSelectionPage({ } = contentText const auth = useAuth() - - const [loginInfoMessage, setLoginInfoMessage] = useState(null) const navigate = useNavigate() - const redirecting = useRef(false) - const [roleCardPropsWithAccess, setRoleCardPropsWithAccess] = useState>([]) - const [roleCardPropsWithoutAccess, setRoleCardPropsWithoutAccess] = useState>([]) + if (auth.isSigningIn) { + const {codeParams, stateParams} = getSearchParams(window) + if (codeParams && stateParams) { + // we are in a redirect from login flow so show spinner + return ( +
+ + + + + + + +
+ ) + } else { + // something has gone wrong so go back to login + auth.clearAuthState() + navigate(FRONTEND_PATHS.LOGIN) + return + } + } + + // Handle auto-redirect for single role + if (auth.rolesWithAccess.length === 1 + && auth.rolesWithoutAccess.length === 0 + && auth.isSignedIn + && auth.selectedRole !== undefined + ) { + navigate(FRONTEND_PATHS.SEARCH_BY_PRESCRIPTION_ID) + return + } const handleSetSelectedRole = async ( e: React.MouseEvent | React.KeyboardEvent, - roleCardProps: RolesWithAccessProps + role: RoleDetails ) => { e.preventDefault() try { - await auth.updateSelectedRole(roleCardProps.role) - navigate(roleCardProps.link) + await auth.updateSelectedRole(role) + navigate(FRONTEND_PATHS.YOUR_SELECTED_ROLE) } catch (err) { if (axios.isAxiosError(err) && (err.response?.status === 401)) { const invalidSessionCause = err.response?.data?.invalidSessionCause @@ -110,76 +137,6 @@ export default function RoleSelectionPage({ } } - useEffect(() => { - // Transform roles data for display - setRoleCardPropsWithAccess((!auth.hasNoAccess) - ? auth.rolesWithAccess.map((role: RoleDetails, index) => ({ - uuid: `role_with_access_${index}`, - role, - link: FRONTEND_PATHS.YOUR_SELECTED_ROLE - })) - : [] - ) - - setRoleCardPropsWithoutAccess(auth.rolesWithoutAccess.map((role, index) => ({ - uuid: `role_without_access_${index}`, - roleName: role.role_name || noRoleName, - orgName: role.org_name || noOrgName, - odsCode: role.org_code || noODSCode - }))) - - logger.warn("RoleCardPropsWithAccess length: ", {roleCardPropsWithAccess, error: auth.error}) - }, [auth.rolesWithAccess, auth.rolesWithoutAccess]) - - // Handle auto-redirect for single role - useEffect(() => { - if (auth.isSigningIn) { - const {codeParams, stateParams} = getSearchParams(window) - if (codeParams && stateParams) { - // we are in a redirect from login flow so carry on - redirecting.current = true - return - } else { - // something has gone wrong so go back to login - auth.clearAuthState() - navigate(FRONTEND_PATHS.LOGIN) - } - } else { - redirecting.current = false - } - }, [auth.isSigningIn]) - - useEffect(() => { - if (auth.hasSingleRoleAccess && auth.isSignedIn) { - navigate(FRONTEND_PATHS.SEARCH_BY_PRESCRIPTION_ID) - } - }, [auth.hasSingleRoleAccess, auth.isSignedIn]) - - // Set login message when selected role is available - useEffect(() => { - if (!loginInfoMessage && auth.selectedRole) { - setLoginInfoMessage( - `You are currently logged in at ${auth.selectedRole.org_name || noOrgName} ` + - `(ODS: ${auth.selectedRole.org_code || noODSCode}) with ${auth.selectedRole.role_name || noRoleName}.` - ) - } - }, [auth.selectedRole, loginInfoMessage]) - - // Show spinner while loading or redirecting - if (redirecting.current) { - return ( -
- - - - - - - -
- ) - } - // Show error if present if (auth.error) { return ( @@ -206,6 +163,11 @@ export default function RoleSelectionPage({ ) } + const hasNoAccess = auth.rolesWithAccess.length === 0 + const unselectedRolesWithAccess = auth.rolesWithAccess.filter( + (role) => role.role_id !== auth.selectedRole?.role_id + ) + return (
- {auth.hasNoAccess ? titleNoAccess : title} + {hasNoAccess ? titleNoAccess : title} - - {(!auth.hasNoAccess) && caption} + {(!hasNoAccess) && caption} - {auth.hasNoAccess &&

{captionNoAccess}

} + {hasNoAccess &&

{captionNoAccess}

} {auth.selectedRole && (
{insetText.visuallyHidden} - {loginInfoMessage && ( -

- )} +

+ You are currently logged in at {auth.selectedRole.org_name || noOrgName} + (ODS: {auth.selectedRole.org_code || noODSCode}) + with {auth.selectedRole.role_name || noRoleName}. +