Skip to content

Commit

Permalink
Merge pull request #38 from restorecommerce/refactor/state-anagement
Browse files Browse the repository at this point in the history
chore: improved actions naming
  • Loading branch information
yosvelquintero authored Aug 14, 2023
2 parents 069badb + 7abf529 commit 6a3a7dd
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/core/state/src/lib/+state/account/account.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const findUserByTokenError = createAction(
'[ACCOUNT] Find user by token request error',
props<{ error: string }>()
);

export const resetAccountState = createAction('[ACCOUNT] Reset account state');
8 changes: 8 additions & 0 deletions packages/core/state/src/lib/+state/account/account.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ENotificationTypes } from '@console-core/types';

import { AccountService } from '../../services';
import { AppFacade } from '../app';
import * as authnActions from '../authn/authn.actions';

import * as accountActions from './account.actions';

Expand Down Expand Up @@ -47,6 +48,13 @@ export class AccountEffects {
{ dispatch: false }
);

resetAccountState$ = createEffect(() => {
return this.actions$.pipe(
ofType(authnActions.signOut),
map(() => accountActions.resetAccountState())
);
});

constructor(
private readonly actions$: Actions,
private readonly accountService: AccountService,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/state/src/lib/+state/account/account.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const reducer = createReducer<IAccountState>(
actionStatus: EActionStatus.FAILED,
error,
})
),
on(
accountActions.resetAccountState,
(): IAccountState => ({
...initialState,
})
)
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/state/src/lib/+state/app/app.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export const addNotification = createAction(
props<{ payload: TNewNotification }>()
);

export const clearNotification = createAction('[APP] Clear notification');
export const clearNotifications = createAction('[APP] Clear notifications');
4 changes: 2 additions & 2 deletions packages/core/state/src/lib/+state/app/app.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import * as appActions from './app.actions';

@Injectable()
export class AppEffects {
clearNotification$ = createEffect(() => {
clearNotifications$ = createEffect(() => {
return this.actions$.pipe(
ofType(appActions.addNotification),
delay(STORE.config.app.notifications.delay),
switchMap(() => of(appActions.clearNotification()))
switchMap(() => of(appActions.clearNotifications()))
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/state/src/lib/+state/app/app.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const reducer = createReducer<IAppState>(
};
}),
on(
appActions.clearNotification,
appActions.clearNotifications,
(state): IAppState => ({
...state,
notifications: state.notifications.filter(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/state/src/lib/+state/authn/authn.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ export const confirmPasswordError = createAction(
);

export const signOut = createAction('[AUTHN] Sign out');

export const resetAuthnState = createAction('[AUTHN] Reset authn state');
7 changes: 7 additions & 0 deletions packages/core/state/src/lib/+state/authn/authn.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ export class AuthnEffects {
{ dispatch: false }
);

resetAuthnState$ = createEffect(() => {
return this.actions$.pipe(
ofType(authnActions.signOut),
map(() => authnActions.resetAuthnState())
);
});

constructor(
private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
Expand Down
9 changes: 8 additions & 1 deletion packages/core/state/src/lib/+state/authn/authn.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,16 @@ const reducer = createReducer<IAuthnState>(
),
on(
authnActions.signOut,
(state): IAuthnState => ({
...state,
isAuthenticated: false,
actionStatus: EActionStatus.SUCCEEDED,
})
),
on(
authnActions.resetAuthnState,
(_): IAuthnState => ({
...initialState,
actionStatus: EActionStatus.SUCCEEDED,
})
)
);
Expand Down

0 comments on commit 6a3a7dd

Please sign in to comment.