Skip to content

Commit 86baae1

Browse files
feat: Enhanced withCallState to accept mutliple collection configuration
1 parent eda4138 commit 86baae1

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

libs/ngrx-toolkit/src/lib/with-call-state.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('withCallState', () => {
1515
it('should use the callState for a collection', () => {
1616
const DataStore = signalStore(
1717
{ protectedState: false },
18-
withCallState({ collection: 'entities' })
18+
withCallState({ collections: ['entities'] })
1919
);
2020
const dataStore = new DataStore();
2121

libs/ngrx-toolkit/src/lib/with-call-state.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,17 @@ export type SetCallState<Prop extends string | undefined> = Prop extends string
3131
? NamedCallStateSlice<Prop>
3232
: CallStateSlice;
3333

34-
export function getCallStateKeys(config?: { collection?: string }) {
35-
const prop = config?.collection;
34+
export function getCallStateKeys(collection?: string) {
3635
return {
37-
callStateKey: prop ? `${config.collection}CallState` : 'callState',
38-
loadingKey: prop ? `${config.collection}Loading` : 'loading',
39-
loadedKey: prop ? `${config.collection}Loaded` : 'loaded',
40-
errorKey: prop ? `${config.collection}Error` : 'error',
36+
callStateKey: collection ? `${collection}CallState` : 'callState',
37+
loadingKey: collection ? `${collection}Loading` : 'loading',
38+
loadedKey: collection ? `${collection}Loaded` : 'loaded',
39+
errorKey: collection ? `${collection}Error` : 'error',
4140
};
4241
}
4342

4443
export function withCallState<Collection extends string>(config: {
45-
collection: Collection;
44+
collections: Collection[];
4645
}): SignalStoreFeature<
4746
EmptyFeatureResult,
4847
EmptyFeatureResult & {
@@ -58,14 +57,39 @@ export function withCallState(): SignalStoreFeature<
5857
}
5958
>;
6059
export function withCallState<Collection extends string>(config?: {
61-
collection: Collection;
60+
collections: Collection[];
6261
}): SignalStoreFeature {
63-
const { callStateKey, errorKey, loadedKey, loadingKey } =
64-
getCallStateKeys(config);
65-
6662
return signalStoreFeature(
67-
withState({ [callStateKey]: 'init' }),
63+
withState(
64+
config
65+
? config.collections.reduce(
66+
(acc, cur) => ({
67+
...acc,
68+
...{ [cur ? `${cur}CallState` : 'callState']: 'init' },
69+
}),
70+
{}
71+
)
72+
: { callStateKey: 'init' }
73+
),
6874
withComputed((state: Record<string, Signal<unknown>>) => {
75+
if (config) {
76+
return config.collections.reduce<Record<string, Signal<unknown>>>((acc, cur: string) => {
77+
const { callStateKey, errorKey, loadedKey, loadingKey } =
78+
getCallStateKeys(cur);
79+
const callState = state[callStateKey] as Signal<CallState>;
80+
81+
return {
82+
...acc,
83+
[loadingKey]: computed(() => callState() === 'loading'),
84+
[loadedKey]: computed(() => callState() === 'loaded'),
85+
[errorKey]: computed(() => {
86+
const v = callState();
87+
return typeof v === 'object' ? v.error : null;
88+
}),
89+
};
90+
}, {});
91+
}
92+
const { callStateKey, errorKey, loadedKey, loadingKey } = getCallStateKeys();
6993
const callState = state[callStateKey] as Signal<CallState>;
7094

7195
return {

0 commit comments

Comments
 (0)