All files / src/app/extensions/product-notifications/store/product-notification product-notification.reducer.ts

61.53% Statements 8/13
100% Branches 0/0
20% Functions 1/5
61.53% Lines 8/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 695x 5x     5x       5x   5x             5x         5x                                                       9x                                    
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { setErrorOn, setLoadingOn, unsetLoadingAndErrorOn } from 'ish-core/utils/ngrx-creators';
 
import { ProductNotification } from '../../models/product-notification/product-notification.model';
 
import { productNotificationsActions, productNotificationsApiActions } from './product-notification.actions';
 
export const productNotificationAdapter = createEntityAdapter<ProductNotification>();
 
export interface ProductNotificationState extends EntityState<ProductNotification> {
  loading: boolean;
  error: HttpError;
}
 
export const initialState: ProductNotificationState = productNotificationAdapter.getInitialState({
  loading: false,
  error: undefined,
});
 
export const productNotificationReducer = createReducer(
  initialState,
  setLoadingOn(
    productNotificationsActions.loadProductNotifications,
    productNotificationsActions.createProductNotification,
    productNotificationsActions.updateProductNotification,
    productNotificationsActions.deleteProductNotification
  ),
  setErrorOn(
    productNotificationsApiActions.loadProductNotificationsFail,
    productNotificationsApiActions.createProductNotificationFail,
    productNotificationsApiActions.updateProductNotificationFail,
    productNotificationsApiActions.deleteProductNotificationFail
  ),
  unsetLoadingAndErrorOn(
    productNotificationsApiActions.loadProductNotificationsSuccess,
    productNotificationsApiActions.createProductNotificationSuccess,
    productNotificationsApiActions.updateProductNotificationSuccess,
    productNotificationsApiActions.deleteProductNotificationSuccess
  ),
  on(productNotificationsApiActions.loadProductNotificationsSuccess, (state, action) =>
    /**
     * Product notifications can be deleted on server side when the notification requirements
     * are met and the notification email was sent. Therefore, all product notifications
     * have to be removed from the state which are not returned from the service before they
     * are loaded, displayed or used. If setAll would be used, the list of notifications would
     * always be empty at first and only filled when the REST request has finished.
     */
    productNotificationAdapter.upsertMany(action.payload.productNotifications, {
      ...productNotificationAdapter.removeMany(entity => entity.type === action.payload.type, state),
    })
  ),
  on(productNotificationsApiActions.createProductNotificationSuccess, (state, action) =>
    productNotificationAdapter.addOne(action.payload.productNotification, state)
  ),
  on(productNotificationsApiActions.updateProductNotificationSuccess, (state, action) =>
    productNotificationAdapter.upsertOne(action.payload.productNotification, state)
  ),
  on(productNotificationsApiActions.deleteProductNotificationSuccess, (state, action) => {
    const id = action.payload.productNotificationId;
 
    return {
      ...productNotificationAdapter.removeOne(id, state),
    };
  })
);