Skip to content

Commit

Permalink
feat(cart-customer): reset cart state when the user is unauthenticated (
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 13, 2023
1 parent 33d9c1a commit e26ab24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libs/cart-customer/state/src/reducers/unauthenticated-reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ActionReducer } from '@ngrx/store';

import {
DaffAuthActionTypes,
DaffAuthActions,
} from '@daffodil/auth/state';
import {
DaffCartReducersState,
daffCartItemEntitiesAdapter,
daffCartOrderInitialState,
daffCartReducerInitialState,
} from '@daffodil/cart/state';

const initialState: DaffCartReducersState = {
cart: daffCartReducerInitialState,
cartItems: daffCartItemEntitiesAdapter().getInitialState(),
order: daffCartOrderInitialState,
};

/**
* Resets cart state when the user is unauthenticated.
*/
export const daffCartCustomerUnauthenticatedReset: ActionReducer<DaffCartReducersState> = (
state = initialState,
action: DaffAuthActions,
) => {
switch (action.type) {
case DaffAuthActionTypes.ResetToUnauthenticatedAction:
return initialState;

default:
return state;
}
};
2 changes: 2 additions & 0 deletions libs/cart-customer/state/src/state.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { daffCartProvideExtraReducers } from '@daffodil/cart/state';

import { DaffCartCustomerAuthEffects } from './effects/auth.effects';
import { daffCartCustomerLoginMutatingReducerMap } from './reducers/login-mutating';
import { daffCartCustomerUnauthenticatedReset } from './reducers/unauthenticated-reset';

@NgModule({
imports: [
Expand All @@ -32,6 +33,7 @@ import { daffCartCustomerLoginMutatingReducerMap } from './reducers/login-mutati
multi: true,
},
daffCartProvideExtraReducers(
daffCartCustomerUnauthenticatedReset,
combineReducers(daffCartCustomerLoginMutatingReducerMap),
),
],
Expand Down

0 comments on commit e26ab24

Please sign in to comment.