All files / app/codeCharta/state/effects/resetChosenMetrics resetChosenMetrics.effect.ts

100% Statements 28/28
100% Branches 4/4
100% Functions 6/6
100% Lines 27/27

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 465x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x     5x 4x   4x   4x 7x     3x   2x             5x 2x   2x 2x 1x     2x 2x 2x    
import { Injectable } from "@angular/core"
import { createEffect } from "@ngrx/effects"
import { Store } from "@ngrx/store"
import { filter, map, tap, withLatestFrom } from "rxjs"
import { CcState, NodeMetricData } from "../../../codeCharta.model"
import { metricDataSelector } from "../../selectors/accumulatedData/metricData/metricData.selector"
import { areChosenMetricsAvailableSelector } from "../../selectors/allNecessaryRenderDataAvailable/areAllNecessaryRenderDataAvailable.selector"
import { setAreaMetric } from "../../store/dynamicSettings/areaMetric/areaMetric.actions"
import { setColorMetric } from "../../store/dynamicSettings/colorMetric/colorMetric.actions"
import { setDistributionMetric } from "../../store/dynamicSettings/distributionMetric/distributionMetric.actions"
import { setHeightMetric } from "../../store/dynamicSettings/heightMetric/heightMetric.actions"
import { getDefaultDistribution } from "./utils/getDefaultDistributionMetric"
import { defaultNMetrics, isAnyMetricAvailable, preselectCombination } from "./utils/metricHelper"
 
@Injectable()
export class ResetChosenMetricsEffect {
    constructor(private store: Store<CcState>) {}
 
    resetChosenDistributionMetric$ = createEffect(
        () =>
            this.store.select(metricDataSelector).pipe(
                map(metricData => metricData.nodeMetricData),
                filter(isAnyMetricAvailable),
                withLatestFrom(this.store.select(areChosenMetricsAvailableSelector)),
                filter(([, areChosenMetricsAvailable]) => !areChosenMetricsAvailable),
                tap(([nodeMetricData]) => {
                    setDefaultMetrics(this.store, nodeMetricData)
                })
            ),
        { dispatch: false }
    )
}
 
export function setDefaultMetrics(store: Store<CcState>, nodeMetricData: NodeMetricData[]) {
    store.dispatch(setDistributionMetric({ value: getDefaultDistribution(nodeMetricData) }))
 
    let [defaultedAreaMetric, defaultedHeightMetric, defaultedColorMetric] = preselectCombination(nodeMetricData)
    if (!defaultedAreaMetric || !defaultedHeightMetric || !defaultedColorMetric) {
        ;[defaultedAreaMetric, defaultedHeightMetric, defaultedColorMetric] = defaultNMetrics(nodeMetricData, 3)
    }
 
    store.dispatch(setAreaMetric({ value: defaultedAreaMetric }))
    store.dispatch(setHeightMetric({ value: defaultedHeightMetric }))
    store.dispatch(setColorMetric({ value: defaultedColorMetric }))
}