Skip to content

Commit

Permalink
fix: fix resetting colors in color metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-schneider-mw committed Jan 16, 2025
1 parent 1eaa516 commit 6d32d16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ export class UpdateMapColorsEffect {
map(colorMetric => {
const state = this.state.getValue()
const attributeDescriptors = state.fileSettings.attributeDescriptors
const currentMapColors = state.appSettings.mapColors

// Only reverse colors if the metric direction is reversed
if (attributeDescriptors[colorMetric]?.direction === 1) {
const reversedMapColors: MapColors = JSON.parse(stringify(state.appSettings.mapColors))
const reversedMapColors: MapColors = JSON.parse(stringify(currentMapColors))
const temporary = reversedMapColors.negative
reversedMapColors.negative = reversedMapColors.positive
reversedMapColors.positive = temporary

return setMapColors({ value: reversedMapColors })
}
return setMapColors({ value: defaultMapColors })

// Keep current colors if they exist, otherwise use defaults
return setMapColors({ value: currentMapColors ?? defaultMapColors })
})
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { Injectable } from "@angular/core"
import { Store } from "@ngrx/store"
import { Actions, createEffect, ofType } from "@ngrx/effects"
import { map, skip, switchMap, take, withLatestFrom } from "rxjs"
import { selectedColorMetricDataSelector } from "../../../selectors/accumulatedData/metricData/selectedColorMetricData.selector"
import { Store } from "@ngrx/store"
import { map, switchMap, take, withLatestFrom } from "rxjs/operators"
import { calculateInitialColorRange } from "./calculateInitialColorRange"
import { selectedColorMetricDataSelector } from "../../../selectors/accumulatedData/metricData/selectedColorMetricData.selector"
import { setColorRange } from "./colorRange.actions"
import { fileActions } from "../../files/files.actions"
import { setFiles } from "../../files/files.actions"
import { CcState } from "../../../../codeCharta.model"
import { setColorMetric } from "../colorMetric/colorMetric.actions"
import { visibleFileStatesSelector } from "../../../selectors/visibleFileStates/visibleFileStates.selector"
import { colorRangeSelector } from "./colorRange.selector"

@Injectable()
export class ResetColorRangeEffect {
constructor(
private actions$: Actions,
private store: Store<CcState>
private readonly actions$: Actions,
private readonly store: Store<CcState>
) {}

// Only reset colors when loading new files, not when changing metrics
resetColorRange$ = createEffect(() =>
this.actions$.pipe(
ofType(...fileActions),
withLatestFrom(this.store.select(visibleFileStatesSelector)),
switchMap(() => this.store.select(selectedColorMetricDataSelector).pipe(skip(1), take(1))),
map(selectedColorMetricData => setColorRange({ value: calculateInitialColorRange(selectedColorMetricData) }))
)
)

resetColorRangeOnColorMetricChange$ = createEffect(() =>
this.actions$.pipe(
ofType(setColorMetric),
switchMap(() => this.store.select(selectedColorMetricDataSelector).pipe(take(1))),
map(selectedColorMetricData => setColorRange({ value: calculateInitialColorRange(selectedColorMetricData) }))
ofType(setFiles),
withLatestFrom(this.store.select(colorRangeSelector)),
switchMap(([_, currentColorRange]) =>
this.store.select(selectedColorMetricDataSelector).pipe(
take(1),
map(selectedColorMetricData => {
// Only set initial colors if we don't have any custom colors yet
if (currentColorRange.from === 0 && currentColorRange.to === 0) {
return setColorRange({ value: calculateInitialColorRange(selectedColorMetricData) })
}
return setColorRange({ value: currentColorRange })
})
)
)
)
)
}

0 comments on commit 6d32d16

Please sign in to comment.