All files / src/app/extensions/tacton/store/saved-tacton-configuration saved-tacton-configuration.reducer.ts

93.33% Statements 14/15
100% Branches 4/4
83.33% Functions 5/6
92.85% Lines 13/14

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 473x 3x 3x       3x   3x       3x 5x     3x 2x     3x     2x     3x     2x                                  
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
import { pick } from 'lodash-es';
 
import { TactonProductConfiguration } from '../../models/tacton-product-configuration/tacton-product-configuration.model';
import { TactonSavedConfiguration } from '../../models/tacton-saved-configuration/tacton-saved-configuration.model';
import { submitTactonConfigurationFail, submitTactonConfigurationSuccess } from '../product-configuration';
 
import { saveTactonConfigurationReference } from './saved-tacton-configuration.actions';
 
export type SavedTactonConfigurationState = EntityState<TactonSavedConfiguration>;
 
export function makeTactonSavedConfigurationReference(user: string, productId: string): string {
  return `${user || 'anonymous'}:${productId}`;
}
 
export const tactonSavedConfigurationAdapter = createEntityAdapter<TactonSavedConfiguration>({
  selectId: e => makeTactonSavedConfigurationReference(e.user, e.productId),
});
 
const initialState: SavedTactonConfigurationState = tactonSavedConfigurationAdapter.getInitialState({});
 
function getActiveStep(config: TactonProductConfiguration): string {
  return config.steps.find(s => s.current).name;
}
 
export const savedTactonConfigurationReducer = createReducer(
  initialState,
  on(saveTactonConfigurationReference, (state, action) =>
    tactonSavedConfigurationAdapter.setOne(
      {
        productId: action.payload.tactonProduct,
        user: action.payload.user || 'anonymous',
        step: getActiveStep(action.payload.configuration),
        ...pick(action.payload.configuration, 'configId', 'externalId'),
      },
      state
    )
  ),
  on(submitTactonConfigurationSuccess, submitTactonConfigurationFail, (state, action) =>
    tactonSavedConfigurationAdapter.removeOne(
      makeTactonSavedConfigurationReference(action.payload.user, action.payload.productId),
      state
    )
  )
);