mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-31 09:37:15 +01:00
refactor(tabs): add metadata and navigation properties to Tab model
This commit is contained in:
@@ -1,81 +1,86 @@
|
||||
import {
|
||||
patchState,
|
||||
signalStore,
|
||||
withComputed,
|
||||
withHooks,
|
||||
withMethods,
|
||||
withState,
|
||||
} from '@ngrx/signals';
|
||||
import {
|
||||
addEntities,
|
||||
addEntity,
|
||||
removeEntity,
|
||||
updateEntity,
|
||||
withEntities,
|
||||
} from '@ngrx/signals/entities';
|
||||
import { Tab } from './tab';
|
||||
import { z } from 'zod';
|
||||
import { AddTabSchema, PatchTabSchema } from './tab.schemas';
|
||||
import { computed, effect } from '@angular/core';
|
||||
|
||||
export const TabService = signalStore(
|
||||
{ providedIn: 'root' },
|
||||
withState<{ activatedTabId: number | null }>({
|
||||
activatedTabId: null,
|
||||
}),
|
||||
withEntities<Tab>(),
|
||||
withComputed((store) => ({
|
||||
nextId: computed(
|
||||
() => Math.max(0, ...store.entities().map((e) => e.id)) + 1,
|
||||
),
|
||||
activatedTab: computed<Tab | null>(() => {
|
||||
const activeTabId = store.activatedTabId();
|
||||
if (activeTabId === null) {
|
||||
return null;
|
||||
}
|
||||
return store.entities().find((e) => e.id === activeTabId) ?? null;
|
||||
}),
|
||||
})),
|
||||
withMethods((store) => ({
|
||||
addTab(add: z.infer<typeof AddTabSchema>) {
|
||||
const parsed = AddTabSchema.parse(add);
|
||||
const tab: Tab = {
|
||||
name: parsed.name,
|
||||
id: store.nextId(),
|
||||
createdAt: Date.now(),
|
||||
tags: parsed.tags,
|
||||
};
|
||||
patchState(store, addEntity(tab));
|
||||
return tab;
|
||||
},
|
||||
activateTab(id: number) {
|
||||
patchState(store, {
|
||||
...updateEntity({ id, changes: { activatedAt: Date.now() } }),
|
||||
activatedTabId: id,
|
||||
});
|
||||
},
|
||||
patchTab(id: number, changes: z.infer<typeof PatchTabSchema>) {
|
||||
patchState(
|
||||
store,
|
||||
updateEntity({ id, changes: PatchTabSchema.parse(changes) }),
|
||||
);
|
||||
},
|
||||
removeTab(id: number) {
|
||||
patchState(store, removeEntity(id));
|
||||
},
|
||||
})),
|
||||
withHooks((store) => ({
|
||||
onInit() {
|
||||
const entitiesStr = localStorage.getItem('TabEntities');
|
||||
if (entitiesStr) {
|
||||
const entities = JSON.parse(entitiesStr);
|
||||
patchState(store, addEntities(entities));
|
||||
}
|
||||
|
||||
effect(() => {
|
||||
const state = store.entities();
|
||||
localStorage.setItem('TabEntities', JSON.stringify(state));
|
||||
});
|
||||
},
|
||||
})),
|
||||
);
|
||||
import {
|
||||
patchState,
|
||||
signalStore,
|
||||
withComputed,
|
||||
withHooks,
|
||||
withMethods,
|
||||
withState,
|
||||
} from '@ngrx/signals';
|
||||
import {
|
||||
addEntities,
|
||||
addEntity,
|
||||
removeEntity,
|
||||
updateEntity,
|
||||
withEntities,
|
||||
} from '@ngrx/signals/entities';
|
||||
import { Tab } from './tab';
|
||||
import { z } from 'zod';
|
||||
import { AddTabSchema, PatchTabSchema } from './tab.schemas';
|
||||
import { computed, effect } from '@angular/core';
|
||||
|
||||
export const TabService = signalStore(
|
||||
{ providedIn: 'root' },
|
||||
withState<{ activatedTabId: number | null }>({
|
||||
activatedTabId: null,
|
||||
}),
|
||||
withEntities<Tab>(),
|
||||
withComputed((store) => ({
|
||||
nextId: computed(
|
||||
() => Math.max(0, ...store.entities().map((e) => e.id)) + 1,
|
||||
),
|
||||
activatedTab: computed<Tab | null>(() => {
|
||||
const activeTabId = store.activatedTabId();
|
||||
if (activeTabId === null) {
|
||||
return null;
|
||||
}
|
||||
return store.entities().find((e) => e.id === activeTabId) ?? null;
|
||||
}),
|
||||
})),
|
||||
withMethods((store) => ({
|
||||
addTab(add: z.infer<typeof AddTabSchema>) {
|
||||
const parsed = AddTabSchema.parse(add);
|
||||
const tab: Tab = {
|
||||
name: parsed.name,
|
||||
id: store.nextId(),
|
||||
createdAt: Date.now(),
|
||||
tags: parsed.tags,
|
||||
metadata: {},
|
||||
navigation: {
|
||||
current: 0,
|
||||
locations: [],
|
||||
},
|
||||
};
|
||||
patchState(store, addEntity(tab));
|
||||
return tab;
|
||||
},
|
||||
activateTab(id: number) {
|
||||
patchState(store, {
|
||||
...updateEntity({ id, changes: { activatedAt: Date.now() } }),
|
||||
activatedTabId: id,
|
||||
});
|
||||
},
|
||||
patchTab(id: number, changes: z.infer<typeof PatchTabSchema>) {
|
||||
patchState(
|
||||
store,
|
||||
updateEntity({ id, changes: PatchTabSchema.parse(changes) }),
|
||||
);
|
||||
},
|
||||
removeTab(id: number) {
|
||||
patchState(store, removeEntity(id));
|
||||
},
|
||||
})),
|
||||
withHooks((store) => ({
|
||||
onInit() {
|
||||
const entitiesStr = localStorage.getItem('TabEntities');
|
||||
if (entitiesStr) {
|
||||
const entities = JSON.parse(entitiesStr);
|
||||
patchState(store, addEntities(entities));
|
||||
}
|
||||
|
||||
effect(() => {
|
||||
const state = store.entities();
|
||||
localStorage.setItem('TabEntities', JSON.stringify(state));
|
||||
});
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user