Skip to content

Commit

Permalink
feat(cart-customer): set cart to mutating when the user is logged in (#…
Browse files Browse the repository at this point in the history
…2508)

this is to track customer cart merging/resolving
  • Loading branch information
griest024 committed Jul 13, 2023
1 parent e8fc2c4 commit 33d9c1a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
57 changes: 57 additions & 0 deletions libs/cart-customer/state/src/reducers/login-mutating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
ActionReducer,
ActionReducerMap,
} from '@ngrx/store';

import {
DaffAuthLoginActionTypes,
DaffAuthLoginActions,
DaffAuthRegisterActionTypes,
DaffAuthRegisterActions,
DaffAuthResetPasswordActionTypes,
DaffAuthResetPasswordActions,
} from '@daffodil/auth/state';
import {
DaffCartOperationType,
DaffCartReducerState,
DaffCartReducersState,
daffCartReducerInitialState,
} from '@daffodil/cart/state';
import {
DaffState,
daffIdentityReducer,
} from '@daffodil/core/state';

/**
* Sets cart to mutating when the user logs in.
* This is to correctly represent the cart merge operation that is performed after a login.
*/
export const daffCartCustomerLoginMutatingReducer: ActionReducer<DaffCartReducerState> = (
state = daffCartReducerInitialState,
action: DaffAuthLoginActions | DaffAuthRegisterActions | DaffAuthResetPasswordActions,
) => {
switch (action.type) {
case DaffAuthLoginActionTypes.LoginSuccessAction:
case DaffAuthRegisterActionTypes.RegisterSuccessAction:
case DaffAuthResetPasswordActionTypes.ResetPasswordSuccessAction:
if ((action.type === DaffAuthRegisterActionTypes.RegisterSuccessAction || action.type === DaffAuthResetPasswordActionTypes.ResetPasswordSuccessAction) && !action.token) {
return state;
}
return {
...state,
loading: {
...state.loading,
[DaffCartOperationType.Cart]: DaffState.Mutating,
},
};

default:
return state;
}
};

export const daffCartCustomerLoginMutatingReducerMap: ActionReducerMap<DaffCartReducersState> = {
cart: daffCartCustomerLoginMutatingReducer,
cartItems: daffIdentityReducer,
order: daffIdentityReducer,
};
6 changes: 6 additions & 0 deletions libs/cart-customer/state/src/state.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {
inject,
} from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { combineReducers } from '@ngrx/store';

import { DAFF_AUTH_UNAUTHENTICATED_HOOKS } from '@daffodil/auth/state';
import { DaffCartStorageService } from '@daffodil/cart';
import { daffCartProvideExtraReducers } from '@daffodil/cart/state';

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

@NgModule({
imports: [
Expand All @@ -28,6 +31,9 @@ import { DaffCartCustomerAuthEffects } from './effects/auth.effects';
},
multi: true,
},
daffCartProvideExtraReducers(
combineReducers(daffCartCustomerLoginMutatingReducerMap),
),
],
})
export class DaffCartCustomerStateModule {}

0 comments on commit 33d9c1a

Please sign in to comment.