Skip to content

Commit

Permalink
feat: reimplement changes from revert #84
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarajohn committed Oct 18, 2023
1 parent 94a7636 commit fbc4ea8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 55 deletions.
Empty file.
Empty file.
10 changes: 7 additions & 3 deletions src/authentication/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -41,7 +42,6 @@ export type User = {
sub?: string;
[key: string]: any;
};

export type AuthenticationContextProps = {
isAuthenticated: boolean;
isLoading: boolean;
Expand All @@ -51,7 +51,11 @@ export type AuthenticationContextProps = {
token: string;
decodedToken: DecodedTokenResponse | Record<string, never>;
} | void>;
orfiumProducts: Product[] | null;
user: User | undefined;
organizations: Organization[];
selectedOrganization: Organization | null;
switchOrganization: (organisation: Organization['org_id']) => void;
};

export type AuthenticationProviderProps = { overrides?: Auth0ClientOptions };
3 changes: 1 addition & 2 deletions src/request/createAPIInstance.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
71 changes: 71 additions & 0 deletions src/store/organizations.ts
Original file line number Diff line number Diff line change
@@ -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<string, Organization> | 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<Store>(
(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<Store['organizations']>;
organizationsList: NonNullable<Store['organizationsList']>;
}
);
}),
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;
File renamed without changes.
50 changes: 0 additions & 50 deletions src/store/useOrganization.ts

This file was deleted.

0 comments on commit fbc4ea8

Please sign in to comment.