Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset only subset of the state in withReset() #160

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions libs/ngrx-toolkit/src/lib/with-reset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,33 @@ describe('withReset', () => {
'Cannot set reset state, since store is not configured with withReset()'
);
});

it('should reset one slice of state to initial', () => {
const { store } = setup();

store.changeUser(2, 'John');
store.changeAddress('Vilnus', 'LT-00104');
expect(getState(store)).toEqual({
user: { id: 2, name: 'John' },
address: { city: 'Vilnus', zip: 'LT-00104' },
});
store.resetSlice('address');
expect(getState(store)).toEqual({
user: { id: 2, name: 'John' },
address: { city: 'Vienna', zip: '1010' },
});
});

it('should reset both slice of state to initial', () => {
const { store, initialState } = setup();

store.changeUser(2, 'John');
store.changeAddress('Vilnus', 'LT-00104');
expect(getState(store)).toEqual({
user: { id: 2, name: 'John' },
address: { city: 'Vilnus', zip: 'LT-00104' },
});
store.resetSlice(['address', 'user']);
expect(getState(store)).toStrictEqual(initialState);
});
});
14 changes: 13 additions & 1 deletion libs/ngrx-toolkit/src/lib/with-reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

export type PublicMethods = {
resetState(): void;
resetSlice(slice: string | string[]): void;
};

/**
Expand All @@ -20,13 +21,24 @@ export type PublicMethods = {
*/
export function withReset() {
return signalStoreFeature(
withProps(() => ({ _resetState: { value: {} } })),
withProps(() => ({ _resetState: { value: {} as Record<string, any> } })),
withMethods((store): PublicMethods => {
// workaround to TS excessive property check
const methods = {
resetState() {
patchState(store, store._resetState.value);
},
resetSlice(slice: string | string[]) {
patchState(store, (state) => ({
Copy link

@abhijit-chikane abhijit-chikane Mar 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work🎉🎉 @daboudii
It would be even better if we could just reset the part of the slice
in case of obj sometimes we don't want to reset the whole inner object but part of the object

Copy link
Author

@daboudii daboudii Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like resetSlice('address.city') ? If yes it will add even more complexity, is it really necessary ?

Copy link

@abhijit-chikane abhijit-chikane Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like resetSlice('address.city') ? If yes it will add even more complexity, is it really necessary ?

I think it will definitely add complexity but it will add much more value for project like ours and many others where data structures are much more complex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I will add that, in this case just easier to patch your state with the value you which to change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I touched upon that in my review below #160 (review)

...state,
...(typeof slice === 'string'
? { [slice]: store._resetState.value[slice] }
: slice.reduce((acc, key) => {
acc[key] = store._resetState.value[key];
return acc;
}, {} as Record<string, any>)),
}));
},
__setResetState__(state: object) {
store._resetState.value = state;
},
Expand Down