moduel setgins fixed
This commit is contained in:
59
frontend/src/store/moduleStore.ts
Normal file
59
frontend/src/store/moduleStore.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Module Settings Store (Zustand)
|
||||
* Manages global module enable/disable state (platform-wide)
|
||||
* Settings are controlled via Django Admin only - NOT account-specific
|
||||
* No persistence needed since this is the same for all users
|
||||
*/
|
||||
import { create } from 'zustand';
|
||||
import { fetchGlobalModuleSettings, GlobalModuleSettings } from '../services/api';
|
||||
|
||||
interface ModuleState {
|
||||
settings: GlobalModuleSettings | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
|
||||
// Actions
|
||||
loadModuleSettings: () => Promise<void>;
|
||||
isModuleEnabled: (moduleName: string) => boolean;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
export const useModuleStore = create<ModuleState>()((set, get) => ({
|
||||
settings: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
loadModuleSettings: async () => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const settings = await fetchGlobalModuleSettings();
|
||||
console.log('Loaded global module settings:', settings);
|
||||
set({ settings, loading: false });
|
||||
} catch (error: any) {
|
||||
console.error('Failed to load global module settings:', error);
|
||||
set({
|
||||
error: error.message || 'Failed to load module settings',
|
||||
loading: false
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
isModuleEnabled: (moduleName: string): boolean => {
|
||||
const { settings } = get();
|
||||
|
||||
// Default to true while settings are loading (better UX)
|
||||
// Once settings load, they will control visibility
|
||||
if (!settings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const fieldName = `${moduleName.toLowerCase()}_enabled` as keyof GlobalModuleSettings;
|
||||
const enabled = settings[fieldName] === true;
|
||||
console.log(`Module check for '${moduleName}' (${fieldName}): ${enabled}`);
|
||||
return enabled;
|
||||
},
|
||||
|
||||
reset: () => {
|
||||
set({ settings: null, loading: false, error: null });
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user