Skip to content

Commit 8b89c58

Browse files
feat: Enhanced withCallState to accept mutliple collection configuration
1 parent eda4138 commit 8b89c58

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

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

+44-36
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function getCallStateKeys(config?: { collection?: string }) {
4242
}
4343

4444
export function withCallState<Collection extends string>(config: {
45-
collection: Collection;
45+
collection: Collection | Collection[];
4646
}): SignalStoreFeature<
4747
EmptyFeatureResult,
4848
EmptyFeatureResult & {
@@ -58,51 +58,61 @@ export function withCallState(): SignalStoreFeature<
5858
}
5959
>;
6060
export function withCallState<Collection extends string>(config?: {
61-
collection: Collection;
61+
collection: Collection | Collection[];
6262
}): SignalStoreFeature {
63-
const { callStateKey, errorKey, loadedKey, loadingKey } =
64-
getCallStateKeys(config);
63+
const collections = Array.isArray(config?.collection) ? config.collection : [config?.collection];
64+
const keys = collections.reduce((acc, collection) => {
65+
const { callStateKey, errorKey, loadedKey, loadingKey } = getCallStateKeys({ collection });
66+
acc.callStateKeys.push(callStateKey);
67+
acc.errorKeys.push(errorKey);
68+
acc.loadedKeys.push(loadedKey);
69+
acc.loadingKeys.push(loadingKey);
70+
return acc;
71+
}, { callStateKeys: [], errorKeys: [], loadedKeys: [], loadingKeys: [] });
6572

6673
return signalStoreFeature(
67-
withState({ [callStateKey]: 'init' }),
74+
withState(keys.callStateKeys.reduce((acc, key) => {
75+
acc[key] = 'init';
76+
return acc;
77+
}, {})),
6878
withComputed((state: Record<string, Signal<unknown>>) => {
69-
const callState = state[callStateKey] as Signal<CallState>;
70-
71-
return {
72-
[loadingKey]: computed(() => callState() === 'loading'),
73-
[loadedKey]: computed(() => callState() === 'loaded'),
74-
[errorKey]: computed(() => {
79+
return keys.callStateKeys.reduce((acc, callStateKey, index) => {
80+
const callState = state[callStateKey] as Signal<CallState>;
81+
acc[keys.loadingKeys[index]] = computed(() => callState() === 'loading');
82+
acc[keys.loadedKeys[index]] = computed(() => callState() === 'loaded');
83+
acc[keys.errorKeys[index]] = computed(() => {
7584
const v = callState();
7685
return typeof v === 'object' ? v.error : null;
77-
}),
78-
};
86+
});
87+
return acc;
88+
}, {});
7989
})
8090
);
8191
}
8292

83-
export function setLoading<Prop extends string | undefined = undefined>(
84-
prop?: Prop
93+
export function setLoading<Prop extends string>(
94+
prop: Prop | Prop[]
8595
): SetCallState<Prop> {
86-
if (prop) {
87-
return { [`${prop}CallState`]: 'loading' } as SetCallState<Prop>;
88-
}
89-
90-
return { callState: 'loading' } as SetCallState<Prop>;
96+
const props = Array.isArray(prop) ? prop : [prop];
97+
return props.reduce((acc, p) => {
98+
acc[`${p}CallState`] = 'loading';
99+
return acc;
100+
}, {} as SetCallState<Prop>);
91101
}
92102

93-
export function setLoaded<Prop extends string | undefined = undefined>(
94-
prop?: Prop
103+
export function setLoaded<Prop extends string>(
104+
prop: Prop | Prop[]
95105
): SetCallState<Prop> {
96-
if (prop) {
97-
return { [`${prop}CallState`]: 'loaded' } as SetCallState<Prop>;
98-
} else {
99-
return { callState: 'loaded' } as SetCallState<Prop>;
100-
}
106+
const props = Array.isArray(prop) ? prop : [prop];
107+
return props.reduce((acc, p) => {
108+
acc[`${p}CallState`] = 'loaded';
109+
return acc;
110+
}, {} as SetCallState<Prop>);
101111
}
102112

103-
export function setError<Prop extends string | undefined = undefined>(
113+
export function setError<Prop extends string>(
104114
error: unknown,
105-
prop?: Prop
115+
prop: Prop | Prop[]
106116
): SetCallState<Prop> {
107117
let errorMessage: string;
108118

@@ -114,11 +124,9 @@ export function setError<Prop extends string | undefined = undefined>(
114124
errorMessage = String(error);
115125
}
116126

117-
if (prop) {
118-
return {
119-
[`${prop}CallState`]: { error: errorMessage },
120-
} as SetCallState<Prop>;
121-
} else {
122-
return { callState: { error: errorMessage } } as SetCallState<Prop>;
123-
}
127+
const props = Array.isArray(prop) ? prop : [prop];
128+
return props.reduce((acc, p) => {
129+
acc[`${p}CallState`] = { error: errorMessage };
130+
return acc;
131+
}, {} as SetCallState<Prop>);
124132
}

0 commit comments

Comments
 (0)