feature/app-selector
This commit is contained in:
70
frontend/src/lib/stores/system.svelte.ts
Normal file
70
frontend/src/lib/stores/system.svelte.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
export type SystemType = 'fixed_asset' | 'inventory';
|
||||
|
||||
export const SYSTEM_LABELS: Record<SystemType, { name: string; code: string }> = {
|
||||
fixed_asset: { name: 'Módulo de Activo Fijo', code: 'SCAF' },
|
||||
inventory: { name: 'Módulo Control de Inventarios', code: 'SCAII' },
|
||||
};
|
||||
|
||||
const VALID_SYSTEMS = new Set<string>(['fixed_asset', 'inventory']);
|
||||
|
||||
class SystemStore {
|
||||
_activeSystem = $state<SystemType | null>(null);
|
||||
_allowedSystems = $state<SystemType[]>([]);
|
||||
_switching = $state(false);
|
||||
|
||||
get activeSystem() {
|
||||
return this._activeSystem;
|
||||
}
|
||||
get allowedSystems() {
|
||||
return this._allowedSystems;
|
||||
}
|
||||
get canSwitch() {
|
||||
return this._allowedSystems.length > 1;
|
||||
}
|
||||
get switching() {
|
||||
return this._switching;
|
||||
}
|
||||
get activeLabel() {
|
||||
return this._activeSystem ? SYSTEM_LABELS[this._activeSystem] : null;
|
||||
}
|
||||
|
||||
initialize(allowedSystems: SystemType[], cookieValue: string | null) {
|
||||
this._allowedSystems = allowedSystems;
|
||||
if (cookieValue && VALID_SYSTEMS.has(cookieValue) && allowedSystems.includes(cookieValue as SystemType)) {
|
||||
this._activeSystem = cookieValue as SystemType;
|
||||
} else if (allowedSystems.length === 1) {
|
||||
this._activeSystem = allowedSystems[0];
|
||||
} else {
|
||||
this._activeSystem = null;
|
||||
}
|
||||
}
|
||||
|
||||
async setActiveSystem(system: SystemType): Promise<boolean> {
|
||||
if (!this._allowedSystems.includes(system) || this._switching) return false;
|
||||
this._switching = true;
|
||||
try {
|
||||
const res = await fetch('/api-sveltekit/system/set-active', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ system }),
|
||||
credentials: 'include',
|
||||
});
|
||||
if (res.ok) {
|
||||
this._activeSystem = system;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
this._switching = false;
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._activeSystem = null;
|
||||
this._allowedSystems = [];
|
||||
}
|
||||
}
|
||||
|
||||
export const systemStore = new SystemStore();
|
||||
Reference in New Issue
Block a user