Skip to content

Commit

Permalink
fix: persist acl version
Browse files Browse the repository at this point in the history
  • Loading branch information
imadtassaoui committed Nov 13, 2024
1 parent e96f371 commit c0a0b4d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ function App() {
};
}, [store.serverStatus.isHealthy]);

useEffect(() => {
setTimeout(() => {
store.setAclVersion(isServerEE ? store.aclVersion : 1);
}, 1000);
}, [isServerEE]);

Check warning on line 222 in src/App.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'store'. Either include it or remove the dependency array

return (
<div className={`App ${isDarkMode ? 'dark' : ''}`}>
{' '}
Expand Down
1 change: 1 addition & 0 deletions src/models/ServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface TenantConfig {
amuiAuthToken?: string;
amuiUserId?: string;
isNewTenant?: boolean;
aclVersion?: 1 | 2;
}

export interface ServerStatus {
Expand Down
3 changes: 3 additions & 0 deletions src/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY = 'nmui-amuiuid-lsk';
export const NMUI_USER_LOCALSTORAGE_KEY = 'nmui-u-lsk';
export const NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY = 'nmui-upr-lsk';
export const NMUI_SHOW_RAC_BANNER_LOCALSTORAGE_KEY = 'nmui-show-rac-banner';
export const NMUI_ACL_VERSION = 'nmui-acl-version';

// function to resolve the particular SaaS tenant's backend URL, ...
export async function setupTenantConfig(): Promise<void> {
Expand All @@ -34,6 +35,7 @@ export async function setupTenantConfig(): Promise<void> {
username: window?.localStorage?.getItem(NMUI_USERNAME_LOCALSTORAGE_KEY) ?? '',
user: JSON.parse(window?.localStorage?.getItem(NMUI_USER_LOCALSTORAGE_KEY) ?? 'null'),
userPlatformRole: JSON.parse(window?.localStorage?.getItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY) ?? 'null'),
aclVersion: Number(window?.localStorage?.getItem(NMUI_ACL_VERSION) ?? '1') as 1 | 2,
});
axiosService.defaults.baseURL = resolvedBaseUrl;
return;
Expand Down Expand Up @@ -113,6 +115,7 @@ export async function setupTenantConfig(): Promise<void> {
user: user || JSON.parse(window?.localStorage?.getItem(NMUI_USER_LOCALSTORAGE_KEY) ?? '{}'),
userPlatformRole:
userPlatformRole || JSON.parse(window?.localStorage?.getItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY) ?? '{}'),
aclVersion: Number(window?.localStorage?.getItem(NMUI_ACL_VERSION) ?? '1') as 1 | 2,
});
}

Expand Down
12 changes: 11 additions & 1 deletion src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TenantConfig } from '../models/ServerConfig';
import { User, UserRole } from '@/models/User';
import {
NMUI_ACCESS_TOKEN_LOCALSTORAGE_KEY,
NMUI_ACL_VERSION,
NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY,
NMUI_BASE_URL_LOCALSTORAGE_KEY,
NMUI_TENANT_ID_LOCALSTORAGE_KEY,
Expand All @@ -28,7 +29,15 @@ export interface IAuthSlice {

// methods
isLoggedIn: () => boolean;
setStore: (config: Partial<TenantConfig & { user: User; userPlatformRole: UserRole }>) => void;
setStore: (
config: Partial<
TenantConfig & {
user: User | null;
userPlatformRole: UserRole | null;
aclVersion?: 1 | 2;
}
>,
) => void;
logout: () => void;
}

Expand Down Expand Up @@ -71,6 +80,7 @@ const createAuthSlice: StateCreator<IAuthSlice, [], [], IAuthSlice> = (set, get)
window?.localStorage?.removeItem(NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY);
window?.localStorage?.removeItem(NMUI_USER_LOCALSTORAGE_KEY);
window?.localStorage?.removeItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY);
window?.localStorage?.removeItem(NMUI_ACL_VERSION);
// TODO: consider using localStorage.clear()
// window?.localStorage?.clear();
},
Expand Down
3 changes: 2 additions & 1 deletion src/store/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NetworksService } from '@/services/NetworksService';
import { convertNetworkPayloadToUiNetwork } from '@/utils/NetworkUtils';
import { StateCreator } from 'zustand';
import { Network, NetworkStat } from '../models/Network';
import { NMUI_ACL_VERSION } from '@/services/BaseService';

export interface INetworkSlice {
// state
Expand All @@ -23,7 +24,6 @@ const createNetworkSlice: StateCreator<INetworkSlice, [], [], INetworkSlice> = (
networks: [],
isFetchingNetworks: false,
aclVersion: 1,

async fetchNetworks() {
try {
set(() => ({ isFetchingNetworks: true }));
Expand Down Expand Up @@ -59,6 +59,7 @@ const createNetworkSlice: StateCreator<INetworkSlice, [], [], INetworkSlice> = (
set((state) => ({ networks: state.networks.filter((network) => network.netid !== networkId) }));
},
setAclVersion(version: 1 | 2) {
window?.localStorage?.setItem(NMUI_ACL_VERSION, version.toString());
set(() => ({ aclVersion: version }));
},
});
Expand Down

0 comments on commit c0a0b4d

Please sign in to comment.