All files / src/app/extensions/tacton/store/product-configuration product-configuration.selectors.ts

48.27% Statements 14/29
0% Branches 0/11
11.76% Functions 2/17
50% Lines 12/24

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 69 70 71 72 73 74 75 7616x   16x     16x           16x   16x   16x   16x                                       16x                       16x           16x         16x                     16x        
import { createSelector } from '@ngrx/store';
 
import { selectRouteParam, selectUrl } from 'ish-core/store/core/router';
 
import { TactonNavigationTree } from '../../models/tacton-navigation-tree/tacton-navigation-tree.model';
import { TactonProductConfigurationHelper } from '../../models/tacton-product-configuration/tacton-product-configuration.helper';
import {
  TactonProductConfigurationGroup,
  TactonProductConfigurationParameter,
} from '../../models/tacton-product-configuration/tacton-product-configuration.model';
import { TactonStepConfig } from '../../models/tacton-step-config/tacton-step-config.model';
import { getTactonState } from '../tacton-store';
 
const getProductConfigurationState = createSelector(getTactonState, state => state?.productConfiguration);
 
export const getProductConfigurationLoading = createSelector(getProductConfigurationState, state => state?.loading);
 
export const getCurrentProductConfiguration = createSelector(
  getProductConfigurationState,
  selectUrl,
  (state, url) => url?.startsWith('/configure') && state?.current
);
 
function mapMembers(
  members: (TactonProductConfigurationGroup | TactonProductConfigurationParameter)[] = []
): TactonNavigationTree {
  return members
    .filter(TactonProductConfigurationHelper.isGroup)
    .filter(g => g.description)
    .map(group => ({
      description: group.description,
      name: group.name,
      hasVisibleParameters: group.hasVisibleParameters,
      children: mapMembers(group.members),
    }));
}
 
export const getConfigurationStepTree = createSelector(
  getCurrentProductConfiguration,
  (config): TactonNavigationTree =>
    config?.steps?.map(step => ({
      description: step.description,
      name: step.name,
      active: step.current,
      hasVisibleParameters: step.hasVisibleParameters,
      children: mapMembers(step.rootGroup?.members),
    }))
);
 
export const getCurrentStepConfig = createSelector(
  selectRouteParam('mainStep'),
  getCurrentProductConfiguration,
  (mainStep, config) => config?.steps?.find(m => m.name === mainStep)?.rootGroup
);
 
export const getCurrentProductConfigurationStepName = createSelector(
  getCurrentProductConfiguration,
  config => config?.steps?.find(step => step.current)?.name
);
 
export const getConfigurationStepConfig = createSelector(
  getCurrentProductConfiguration,
  (config): TactonStepConfig =>
    config?.steps && {
      length: config?.steps?.length,
      previousStep: config?.steps?.find((_, idx, arr) => idx < arr.length && arr[idx + 1]?.current)?.name,
      currentStep: config?.steps?.find(s => s.current)?.name,
      nextStep: config?.steps?.find((_, idx, arr) => idx > 0 && arr[idx - 1]?.current)?.name,
    }
);
 
export const getCurrentProductConfigurationConflicts = createSelector(
  getCurrentProductConfiguration,
  config => config?.response?.status !== 'OK' && config?.response?.changed
);