diff --git a/src/authentication/components/TopBar/TopBar.test.tsx b/src/authentication/components/TopBar/TopBar.test.tsx deleted file mode 100644 index e69de29b..00000000 diff --git a/src/authentication/components/TopBar/TopBar.tsx b/src/authentication/components/TopBar/TopBar.tsx deleted file mode 100644 index e69de29b..00000000 diff --git a/src/authentication/types.ts b/src/authentication/types.ts index 267c6bd0..f3a6b714 100644 --- a/src/authentication/types.ts +++ b/src/authentication/types.ts @@ -1,8 +1,10 @@ -import { +import type { Auth0ClientOptions, GetTokenSilentlyOptions, RedirectLoginOptions, } from '@auth0/auth0-spa-js'; +import type { Product } from '../hooks/useOrfiumProducts/types'; +import { Organization } from '../store/organizations'; export type DecodedTokenResponse = { iss?: string; @@ -17,7 +19,6 @@ export type DecodedTokenResponse = { /** the permissions defined on the user for more info visit https://orfium.atlassian.net/wiki/spaces/OPS/pages/2554134739/Roles+and+Permissions#Organization-Roles **/ permissions?: string[]; }; - export type User = { name?: string; given_name?: string; @@ -41,7 +42,6 @@ export type User = { sub?: string; [key: string]: any; }; - export type AuthenticationContextProps = { isAuthenticated: boolean; isLoading: boolean; @@ -51,7 +51,11 @@ export type AuthenticationContextProps = { token: string; decodedToken: DecodedTokenResponse | Record; } | void>; + orfiumProducts: Product[] | null; user: User | undefined; + organizations: Organization[]; + selectedOrganization: Organization | null; + switchOrganization: (organisation: Organization['org_id']) => void; }; export type AuthenticationProviderProps = { overrides?: Auth0ClientOptions }; diff --git a/src/request/createAPIInstance.ts b/src/request/createAPIInstance.ts index 456fde66..6fa94b3a 100644 --- a/src/request/createAPIInstance.ts +++ b/src/request/createAPIInstance.ts @@ -1,7 +1,6 @@ import axios, { AxiosInstance, CancelTokenSource } from 'axios'; - import { getTokenSilently, logoutAuth } from '../authentication/context'; -import useRequestToken from '../store/useRequestToken'; +import useRequestToken from '../store/requestToken'; import { deleteToken, request, RequestProps, setToken, tokenFormat } from './request'; export { default as MockRequest } from './mock'; diff --git a/src/store/organizations.ts b/src/store/organizations.ts new file mode 100644 index 00000000..b466793a --- /dev/null +++ b/src/store/organizations.ts @@ -0,0 +1,71 @@ +import create from 'zustand'; +import { persist } from 'zustand/middleware'; + +export type Organization = { + org_id: string; + display_name: string; + name: string; + can_administrate: boolean; + metadata: { + type: string; + product_codes: string; + }; + branding: { + logo_url: string; + }; +}; + +type Store = { + // list of organizations that fetched and stored + organizations: Record | null; + organizationsList: Organization['org_id'][] | null; + // the selected organization for the current session + selectedOrganization: Organization | null; + setOrganizations: (organizations: Organization[]) => void; + setSelectedOrganization: (orgID: Organization['org_id']) => void; + reset: () => void; +}; + +const initialState = { + organizations: null, + organizationsList: null, + selectedOrganization: null, +}; +const useOrganization = create( + persist( + (set, get) => ({ + ...initialState, + setOrganizations: (organizations: Organization[]) => + set(() => { + return organizations.reduce( + (acc, org) => { + acc.organizations[org.org_id] = org; + acc.organizationsList.push(org.org_id); + + return acc; + }, + { organizations: {}, organizationsList: [] } as { + organizations: NonNullable; + organizationsList: NonNullable; + } + ); + }), + setSelectedOrganization: (organization: Organization['org_id']) => { + const orgs = get().organizations; + if (orgs === null) { + set({ selectedOrganization: null }); + } else { + set({ selectedOrganization: orgs[organization] }); + } + }, + reset: () => { + set({ ...initialState }); + }, + }), + { + name: 'selectedOrganization', + } + ) +); + +export default useOrganization; diff --git a/src/store/useRequestToken.ts b/src/store/requestToken.ts similarity index 100% rename from src/store/useRequestToken.ts rename to src/store/requestToken.ts diff --git a/src/store/useOrganization.ts b/src/store/useOrganization.ts deleted file mode 100644 index 7da2fdb0..00000000 --- a/src/store/useOrganization.ts +++ /dev/null @@ -1,50 +0,0 @@ -import create from 'zustand'; -import { persist } from 'zustand/middleware'; - -export type Organization = { - org_id: string; - display_name: string; - name: string; - can_administrate: boolean; - metadata: { - type: string; - product_codes: string; - }; - branding: { - logo_url: string; - }; -}; - -type Store = { - // list of organizations that fetched and stored - organizations: Organization[]; - // the selected organization for the current session - selectedOrganization: Organization | undefined; - setOrganizations: (organizations: Organization[]) => void; - setSelectedOrganization: (organizations: Organization) => void; - reset: () => void; -}; - -const initialState = { - organizations: [], - selectedOrganization: undefined, -}; -const useOrganization = create( - persist( - (set, __get) => ({ - ...initialState, - setOrganizations: (organizations: Organization[]) => set(() => ({ organizations })), - setSelectedOrganization: (organization: Organization) => { - set({ selectedOrganization: organization }); - }, - reset: () => { - set({ ...initialState }); - }, - }), - { - name: 'selectedOrganization', - } - ) -); - -export default useOrganization;