From 0b4fe33ca79404ff292fd9b841d0396bfe96c565 Mon Sep 17 00:00:00 2001 From: shaman-apprentice Date: Thu, 27 Apr 2023 21:19:52 +0200 Subject: [PATCH] fix: restore old behavior to reset state when dispatching undefined ref #3250 --- .../amountOfEdgePreviews.reducer.ts | 3 ++- .../amountOfTopLabels.reducer.ts | 6 ++--- .../store/appSettings/appSettings.reducer.ts | 4 ++-- .../colorLabels/colorLabels.reducer.spec.ts | 4 ++-- .../colorLabels/colorLabels.reducer.ts | 8 +++---- .../edgeHeight/edgeHeight.reducer.ts | 6 ++--- .../screenshotToClipboardEnabled.reducer.ts | 3 ++- .../experimentalFeaturesEnabled.reducer.ts | 3 ++- .../enableFloorLabels.reducer.ts | 6 ++--- .../hideFlatBuildings.reducer.ts | 6 ++--- .../invertArea/invertArea.reducer.ts | 6 ++--- .../invertHeight/invertHeight.reducer.ts | 6 ++--- .../isLoadingFile/isLoadingFile.reducer.ts | 6 ++--- .../isLoadingMap/isLoadingMap.reducer.ts | 6 ++--- .../isPresentationMode.reducer.ts | 6 ++--- .../isWhiteBackground.reducer.ts | 6 ++--- .../layoutAlgorithm.reducer.ts | 6 ++--- .../mapColors/mapColors.reducer.ts | 3 ++- .../maxTreeMapFiles.reducer.ts | 6 ++--- .../resetCameraIfNewFileIsLoaded.reducer.ts | 3 ++- .../appSettings/scaling/scaling.reducer.ts | 6 ++--- .../sharpnessMode/sharpnessMode.reducer.ts | 6 ++--- .../showMetricLabelNameValue.reducer.ts | 3 ++- .../showMetricLabelNodeName.reducer.ts | 3 ++- .../showOnlyBuildingsWithEdges.reducer.ts | 3 ++- .../sortingOrderAscending.reducer.ts | 3 ++- .../hoveredNodeId/hoveredNodeId.reducer.ts | 6 ++--- .../selectedBuildingId.reducer.ts | 6 ++--- .../areaMetric/areaMetric.reducer.ts | 6 ++--- .../colorMetric/colorMetric.reducer.ts | 6 ++--- .../colorMode/colorMode.reducer.ts | 6 ++--- .../colorRange/colorRange.reducer.ts | 6 ++--- .../distributionMetric.reducer.ts | 6 ++--- .../edgeMetric/edgeMetric.reducer.ts | 6 ++--- .../heightMetric/heightMetric.reducer.ts | 6 ++--- .../dynamicSettings/margin/margin.reducer.ts | 6 ++--- .../searchPattern/searchPattern.reducer.ts | 3 ++- .../sortingOption/sortingOption.reducer.ts | 6 ++--- .../attributeDescriptors.reducer.ts | 3 ++- .../attributeTypes/attributeTypes.reducer.ts | 3 ++- .../blacklist/blacklist.reducer.ts | 3 ++- .../store/fileSettings/edges/edges.reducer.ts | 3 ++- .../markedPackages/markedPackages.reducer.ts | 3 ++- .../state/store/files/files.reducer.ts | 3 ++- .../util/setState.reducer.factory.spec.ts | 24 +++++++++++++++++++ .../store/util/setState.reducer.factory.ts | 17 +++++++++++++ 46 files changed, 130 insertions(+), 125 deletions(-) create mode 100644 visualization/app/codeCharta/state/store/util/setState.reducer.factory.spec.ts create mode 100644 visualization/app/codeCharta/state/store/util/setState.reducer.factory.ts diff --git a/visualization/app/codeCharta/state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.reducer.ts index 904f74b2154..d86c0acd1a8 100644 --- a/visualization/app/codeCharta/state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/amountOfEdgePreviews/amountOfEdgePreviews.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setAmountOfEdgePreviews } from "./amountOfEdgePreviews.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultAmountOfEdgesPreviews = 1 export const amountOfEdgePreviews = createReducer( defaultAmountOfEdgesPreviews, - on(setAmountOfEdgePreviews, (_state, action) => action.value) + on(setAmountOfEdgePreviews, setState(defaultAmountOfEdgesPreviews)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/amountOfTopLabels/amountOfTopLabels.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/amountOfTopLabels/amountOfTopLabels.reducer.ts index 477f260b6cf..bc806d4020d 100644 --- a/visualization/app/codeCharta/state/store/appSettings/amountOfTopLabels/amountOfTopLabels.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/amountOfTopLabels/amountOfTopLabels.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setAmountOfTopLabels } from "./amountOfTopLabels.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultAmountOfTopLabels = 1 -export const amountOfTopLabels = createReducer( - defaultAmountOfTopLabels, - on(setAmountOfTopLabels, (_state, action) => action.value) -) +export const amountOfTopLabels = createReducer(defaultAmountOfTopLabels, on(setAmountOfTopLabels, setState(defaultAmountOfTopLabels))) diff --git a/visualization/app/codeCharta/state/store/appSettings/appSettings.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/appSettings.reducer.ts index a671a39766f..0934349c1b2 100644 --- a/visualization/app/codeCharta/state/store/appSettings/appSettings.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/appSettings.reducer.ts @@ -1,4 +1,4 @@ -import { colorLabels, defaultColorLabels } from "./colorLabels/colorLabels.reducer" +import { colorLabels, defaultColorLabelOptions } from "./colorLabels/colorLabels.reducer" import { defaultShowMetricLabelNodeName, showMetricLabelNodeName } from "./showMetricLabelNodeName/showMetricLabelNodeName.reducer" import { defaultShowMetricLabelNameValue, showMetricLabelNameValue } from "./showMetricLabelNameValue/showMetricLabelNameValue.reducer" import { defaultSortingOrderAscending, sortingOrderAscending } from "./sortingOrderAscending/sortingOrderAscending.reducer" @@ -68,7 +68,7 @@ export const appSettings = combineReducers({ }) export const defaultAppSettings = { - colorLabels: defaultColorLabels, + colorLabels: defaultColorLabelOptions, showMetricLabelNodeName: defaultShowMetricLabelNodeName, showMetricLabelNameValue: defaultShowMetricLabelNameValue, sortingOrderAscending: defaultSortingOrderAscending, diff --git a/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.spec.ts b/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.spec.ts index 303ca112719..b739738cf93 100644 --- a/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.spec.ts +++ b/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.spec.ts @@ -1,4 +1,4 @@ -import { colorLabels, defaultColorLabels } from "./colorLabels.reducer" +import { colorLabels, defaultColorLabelOptions } from "./colorLabels.reducer" import { setColorLabels } from "./colorLabels.actions" describe("colorLabels", () => { @@ -9,7 +9,7 @@ describe("colorLabels", () => { } it("should set new colorLabels", () => { - const result = colorLabels(defaultColorLabels, setColorLabels({ value: otherColorLabelOption })) + const result = colorLabels(defaultColorLabelOptions, setColorLabels({ value: otherColorLabelOption })) expect(result).toEqual(otherColorLabelOption) }) diff --git a/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.ts index dbc28226b77..552ccf742d4 100644 --- a/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/colorLabels/colorLabels.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { setColorLabels } from "./colorLabels.actions" import { ColorLabelOptions } from "../../../../codeCharta.model" +import { mergeState } from "../../util/setState.reducer.factory" -export const defaultColorLabels: ColorLabelOptions = { positive: false, negative: false, neutral: false } -export const colorLabels = createReducer( - defaultColorLabels, - on(setColorLabels, (state, action) => ({ ...state, ...action.value })) -) +export const defaultColorLabelOptions: ColorLabelOptions = { positive: false, negative: false, neutral: false } +export const colorLabels = createReducer(defaultColorLabelOptions, on(setColorLabels, mergeState(defaultColorLabelOptions))) diff --git a/visualization/app/codeCharta/state/store/appSettings/edgeHeight/edgeHeight.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/edgeHeight/edgeHeight.reducer.ts index 803c5543464..4affdf75e0a 100644 --- a/visualization/app/codeCharta/state/store/appSettings/edgeHeight/edgeHeight.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/edgeHeight/edgeHeight.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setEdgeHeight } from "./edgeHeight.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultEdgeHeight = 4 -export const edgeHeight = createReducer( - defaultEdgeHeight, - on(setEdgeHeight, (_state, action) => action.value) -) +export const edgeHeight = createReducer(defaultEdgeHeight, on(setEdgeHeight, setState(defaultEdgeHeight))) diff --git a/visualization/app/codeCharta/state/store/appSettings/enableClipboard/screenshotToClipboardEnabled.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/enableClipboard/screenshotToClipboardEnabled.reducer.ts index 8195db7954e..92f32f09e10 100644 --- a/visualization/app/codeCharta/state/store/appSettings/enableClipboard/screenshotToClipboardEnabled.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/enableClipboard/screenshotToClipboardEnabled.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setScreenshotToClipboardEnabled } from "./screenshotToClipboardEnabled.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultScreenshotToClipboardEnabled = false export const screenshotToClipboardEnabled = createReducer( defaultScreenshotToClipboardEnabled, - on(setScreenshotToClipboardEnabled, (_state, action) => action.value) + on(setScreenshotToClipboardEnabled, setState(defaultScreenshotToClipboardEnabled)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/enableExperimentalFeatures/experimentalFeaturesEnabled.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/enableExperimentalFeatures/experimentalFeaturesEnabled.reducer.ts index 8b1b25cdf94..c2f63162d57 100644 --- a/visualization/app/codeCharta/state/store/appSettings/enableExperimentalFeatures/experimentalFeaturesEnabled.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/enableExperimentalFeatures/experimentalFeaturesEnabled.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setExperimentalFeaturesEnabled } from "./experimentalFeaturesEnabled.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultExperimentalFeaturesEnabled = false export const experimentalFeaturesEnabled = createReducer( defaultExperimentalFeaturesEnabled, - on(setExperimentalFeaturesEnabled, (_state, action) => action.value) + on(setExperimentalFeaturesEnabled, setState(defaultExperimentalFeaturesEnabled)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/enableFloorLabels/enableFloorLabels.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/enableFloorLabels/enableFloorLabels.reducer.ts index cbeab15fb07..413b6a54b57 100644 --- a/visualization/app/codeCharta/state/store/appSettings/enableFloorLabels/enableFloorLabels.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/enableFloorLabels/enableFloorLabels.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setEnableFloorLabels } from "./enableFloorLabels.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultEnableFloorLabels = true -export const enableFloorLabels = createReducer( - defaultEnableFloorLabels, - on(setEnableFloorLabels, (_state, action) => action.value) -) +export const enableFloorLabels = createReducer(defaultEnableFloorLabels, on(setEnableFloorLabels, setState(defaultEnableFloorLabels))) diff --git a/visualization/app/codeCharta/state/store/appSettings/hideFlatBuildings/hideFlatBuildings.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/hideFlatBuildings/hideFlatBuildings.reducer.ts index bb185c378b1..34c1fc97fae 100644 --- a/visualization/app/codeCharta/state/store/appSettings/hideFlatBuildings/hideFlatBuildings.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/hideFlatBuildings/hideFlatBuildings.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setHideFlatBuildings } from "./hideFlatBuildings.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultHideFlatBuildings = false -export const hideFlatBuildings = createReducer( - defaultHideFlatBuildings, - on(setHideFlatBuildings, (_state, action) => action.value) -) +export const hideFlatBuildings = createReducer(defaultHideFlatBuildings, on(setHideFlatBuildings, setState(defaultHideFlatBuildings))) diff --git a/visualization/app/codeCharta/state/store/appSettings/invertArea/invertArea.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/invertArea/invertArea.reducer.ts index 7d3a7e2b6e9..b928e2d61c0 100644 --- a/visualization/app/codeCharta/state/store/appSettings/invertArea/invertArea.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/invertArea/invertArea.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setInvertArea } from "./invertArea.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultInvertArea = false -export const invertArea = createReducer( - defaultInvertArea, - on(setInvertArea, (_state, action) => action.value) -) +export const invertArea = createReducer(defaultInvertArea, on(setInvertArea, setState(defaultInvertArea))) diff --git a/visualization/app/codeCharta/state/store/appSettings/invertHeight/invertHeight.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/invertHeight/invertHeight.reducer.ts index 639684f04a7..3754327a870 100644 --- a/visualization/app/codeCharta/state/store/appSettings/invertHeight/invertHeight.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/invertHeight/invertHeight.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setInvertHeight } from "./invertHeight.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultInvertHeight = false -export const invertHeight = createReducer( - defaultInvertHeight, - on(setInvertHeight, (_state, action) => action.value) -) +export const invertHeight = createReducer(defaultInvertHeight, on(setInvertHeight, setState(defaultInvertHeight))) diff --git a/visualization/app/codeCharta/state/store/appSettings/isLoadingFile/isLoadingFile.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/isLoadingFile/isLoadingFile.reducer.ts index 043290265f3..23cc7edb0af 100644 --- a/visualization/app/codeCharta/state/store/appSettings/isLoadingFile/isLoadingFile.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/isLoadingFile/isLoadingFile.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setIsLoadingFile } from "./isLoadingFile.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultIsLoadingFile = true -export const isLoadingFile = createReducer( - defaultIsLoadingFile, - on(setIsLoadingFile, (_state, action) => action.value) -) +export const isLoadingFile = createReducer(defaultIsLoadingFile, on(setIsLoadingFile, setState(defaultIsLoadingFile))) diff --git a/visualization/app/codeCharta/state/store/appSettings/isLoadingMap/isLoadingMap.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/isLoadingMap/isLoadingMap.reducer.ts index 3f8d2e10e46..c6627a19ec2 100644 --- a/visualization/app/codeCharta/state/store/appSettings/isLoadingMap/isLoadingMap.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/isLoadingMap/isLoadingMap.reducer.ts @@ -1,8 +1,6 @@ +import { setState } from "../../util/setState.reducer.factory" import { setIsLoadingMap } from "./isLoadingMap.actions" import { createReducer, on } from "@ngrx/store" export const defaultIsLoadingMap = true -export const isLoadingMap = createReducer( - defaultIsLoadingMap, - on(setIsLoadingMap, (_state, action) => action.value) -) +export const isLoadingMap = createReducer(defaultIsLoadingMap, on(setIsLoadingMap, setState(defaultIsLoadingMap))) diff --git a/visualization/app/codeCharta/state/store/appSettings/isPresentationMode/isPresentationMode.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/isPresentationMode/isPresentationMode.reducer.ts index 784af3f746f..c806c372194 100644 --- a/visualization/app/codeCharta/state/store/appSettings/isPresentationMode/isPresentationMode.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/isPresentationMode/isPresentationMode.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setPresentationMode } from "./isPresentationMode.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultIsPresentationMode = false -export const isPresentationMode = createReducer( - defaultIsPresentationMode, - on(setPresentationMode, (_state, action) => action.value) -) +export const isPresentationMode = createReducer(defaultIsPresentationMode, on(setPresentationMode, setState(defaultIsPresentationMode))) diff --git a/visualization/app/codeCharta/state/store/appSettings/isWhiteBackground/isWhiteBackground.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/isWhiteBackground/isWhiteBackground.reducer.ts index 86bb4366336..45948dbdfa6 100644 --- a/visualization/app/codeCharta/state/store/appSettings/isWhiteBackground/isWhiteBackground.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/isWhiteBackground/isWhiteBackground.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setIsWhiteBackground } from "./isWhiteBackground.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultIsWhiteBackground = false -export const isWhiteBackground = createReducer( - defaultIsWhiteBackground, - on(setIsWhiteBackground, (_state, action) => action.value) -) +export const isWhiteBackground = createReducer(defaultIsWhiteBackground, on(setIsWhiteBackground, setState(defaultIsWhiteBackground))) diff --git a/visualization/app/codeCharta/state/store/appSettings/layoutAlgorithm/layoutAlgorithm.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/layoutAlgorithm/layoutAlgorithm.reducer.ts index 7af5f6ceab0..c84d6171487 100644 --- a/visualization/app/codeCharta/state/store/appSettings/layoutAlgorithm/layoutAlgorithm.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/layoutAlgorithm/layoutAlgorithm.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { LayoutAlgorithm } from "../../../../codeCharta.model" import { setLayoutAlgorithm } from "./layoutAlgorithm.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultLayoutAlgorithm = LayoutAlgorithm.SquarifiedTreeMap -export const layoutAlgorithm = createReducer( - defaultLayoutAlgorithm, - on(setLayoutAlgorithm, (_state, action) => action.value) -) +export const layoutAlgorithm = createReducer(defaultLayoutAlgorithm, on(setLayoutAlgorithm, setState(defaultLayoutAlgorithm))) diff --git a/visualization/app/codeCharta/state/store/appSettings/mapColors/mapColors.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/mapColors/mapColors.reducer.ts index 5c454a57810..50f871c94f3 100644 --- a/visualization/app/codeCharta/state/store/appSettings/mapColors/mapColors.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/mapColors/mapColors.reducer.ts @@ -1,6 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { MapColors } from "../../../../codeCharta.model" import { invertColorRange, invertDeltaColors, setMapColors } from "./mapColors.actions" +import { mergeState } from "../../util/setState.reducer.factory" export const defaultMapColors: MapColors = { positive: "#69AE40", @@ -19,7 +20,7 @@ export const defaultMapColors: MapColors = { export const mapColors = createReducer( defaultMapColors, - on(setMapColors, (state, action) => ({ ...state, ...(action.value ?? defaultMapColors) })), + on(setMapColors, mergeState(defaultMapColors)), on(invertColorRange, state => ({ ...state, positive: state.negative, negative: state.positive })), on(invertDeltaColors, state => ({ ...state, positiveDelta: state.negativeDelta, negativeDelta: state.positiveDelta })) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/maxTreeMapFiles/maxTreeMapFiles.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/maxTreeMapFiles/maxTreeMapFiles.reducer.ts index 799025b1e84..ffe342ac9df 100644 --- a/visualization/app/codeCharta/state/store/appSettings/maxTreeMapFiles/maxTreeMapFiles.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/maxTreeMapFiles/maxTreeMapFiles.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setMaxTreeMapFiles } from "./maxTreeMapFiles.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultMaxTreeMapFiles = 100 -export const maxTreeMapFiles = createReducer( - defaultMaxTreeMapFiles, - on(setMaxTreeMapFiles, (_state, action) => action.value) -) +export const maxTreeMapFiles = createReducer(defaultMaxTreeMapFiles, on(setMaxTreeMapFiles, setState(defaultMaxTreeMapFiles))) diff --git a/visualization/app/codeCharta/state/store/appSettings/resetCameraIfNewFileIsLoaded/resetCameraIfNewFileIsLoaded.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/resetCameraIfNewFileIsLoaded/resetCameraIfNewFileIsLoaded.reducer.ts index 705a82b10e8..f3499bb9ae9 100644 --- a/visualization/app/codeCharta/state/store/appSettings/resetCameraIfNewFileIsLoaded/resetCameraIfNewFileIsLoaded.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/resetCameraIfNewFileIsLoaded/resetCameraIfNewFileIsLoaded.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setResetCameraIfNewFileIsLoaded } from "./resetCameraIfNewFileIsLoaded.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultResetCameraIfNewFileIsLoaded = true export const resetCameraIfNewFileIsLoaded = createReducer( defaultResetCameraIfNewFileIsLoaded, - on(setResetCameraIfNewFileIsLoaded, (_state, action) => action.value) + on(setResetCameraIfNewFileIsLoaded, setState(defaultResetCameraIfNewFileIsLoaded)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/scaling/scaling.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/scaling/scaling.reducer.ts index afc48f8549f..3bb89400eb0 100644 --- a/visualization/app/codeCharta/state/store/appSettings/scaling/scaling.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/scaling/scaling.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { setScaling } from "./scaling.actions" import { Scaling } from "../../../../codeCharta.model" +import { mergeState } from "../../util/setState.reducer.factory" export const defaultScaling: Scaling = { x: 1, y: 1, z: 1 } -export const scaling = createReducer( - defaultScaling, - on(setScaling, (state, action) => ({ ...state, ...action.value })) -) +export const scaling = createReducer(defaultScaling, on(setScaling, mergeState(defaultScaling))) diff --git a/visualization/app/codeCharta/state/store/appSettings/sharpnessMode/sharpnessMode.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/sharpnessMode/sharpnessMode.reducer.ts index 03c8e4da251..f88cc41a133 100644 --- a/visualization/app/codeCharta/state/store/appSettings/sharpnessMode/sharpnessMode.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/sharpnessMode/sharpnessMode.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { SharpnessMode } from "../../../../codeCharta.model" import { setSharpnessMode } from "./sharpnessMode.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultSharpnessMode = SharpnessMode.Standard -export const sharpnessMode = createReducer( - defaultSharpnessMode, - on(setSharpnessMode, (_state, action) => action.value) -) +export const sharpnessMode = createReducer(defaultSharpnessMode, on(setSharpnessMode, setState(defaultSharpnessMode))) diff --git a/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNameValue/showMetricLabelNameValue.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNameValue/showMetricLabelNameValue.reducer.ts index 83e05da982f..a8d193364d9 100644 --- a/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNameValue/showMetricLabelNameValue.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNameValue/showMetricLabelNameValue.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setShowMetricLabelNameValue } from "./showMetricLabelNameValue.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultShowMetricLabelNameValue = false export const showMetricLabelNameValue = createReducer( defaultShowMetricLabelNameValue, - on(setShowMetricLabelNameValue, (_state, action) => action.value) + on(setShowMetricLabelNameValue, setState(defaultShowMetricLabelNameValue)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNodeName/showMetricLabelNodeName.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNodeName/showMetricLabelNodeName.reducer.ts index a5ba342ef51..f343c56944a 100644 --- a/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNodeName/showMetricLabelNodeName.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/showMetricLabelNodeName/showMetricLabelNodeName.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setShowMetricLabelNodeName } from "./showMetricLabelNodeName.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultShowMetricLabelNodeName = true export const showMetricLabelNodeName = createReducer( defaultShowMetricLabelNodeName, - on(setShowMetricLabelNodeName, (_state, action) => action.value) + on(setShowMetricLabelNodeName, setState(defaultShowMetricLabelNodeName)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/showOnlyBuildingsWithEdges/showOnlyBuildingsWithEdges.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/showOnlyBuildingsWithEdges/showOnlyBuildingsWithEdges.reducer.ts index 4bfe7a6294c..6e05c266478 100644 --- a/visualization/app/codeCharta/state/store/appSettings/showOnlyBuildingsWithEdges/showOnlyBuildingsWithEdges.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/showOnlyBuildingsWithEdges/showOnlyBuildingsWithEdges.reducer.ts @@ -1,8 +1,9 @@ import { createReducer, on } from "@ngrx/store" import { setShowOnlyBuildingsWithEdges } from "./showOnlyBuildingsWithEdges.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultShowOnlyBuildingsWithEdges = false export const showOnlyBuildingsWithEdges = createReducer( defaultShowOnlyBuildingsWithEdges, - on(setShowOnlyBuildingsWithEdges, (_state, action) => action.value) + on(setShowOnlyBuildingsWithEdges, setState(defaultShowOnlyBuildingsWithEdges)) ) diff --git a/visualization/app/codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.reducer.ts b/visualization/app/codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.reducer.ts index c0adc8c6e68..122442facfe 100644 --- a/visualization/app/codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.reducer.ts +++ b/visualization/app/codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.reducer.ts @@ -1,9 +1,10 @@ import { createReducer, on } from "@ngrx/store" import { setSortingOrderAscending, toggleSortingOrderAscending } from "./sortingOrderAscending.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultSortingOrderAscending = true export const sortingOrderAscending = createReducer( defaultSortingOrderAscending, - on(setSortingOrderAscending, (_state, action) => action.value), + on(setSortingOrderAscending, setState(defaultSortingOrderAscending)), on(toggleSortingOrderAscending, state => !state) ) diff --git a/visualization/app/codeCharta/state/store/appStatus/hoveredNodeId/hoveredNodeId.reducer.ts b/visualization/app/codeCharta/state/store/appStatus/hoveredNodeId/hoveredNodeId.reducer.ts index 7fa71e36dec..aaf793ad44e 100644 --- a/visualization/app/codeCharta/state/store/appStatus/hoveredNodeId/hoveredNodeId.reducer.ts +++ b/visualization/app/codeCharta/state/store/appStatus/hoveredNodeId/hoveredNodeId.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { CcState } from "../../../../codeCharta.model" import { setHoveredNodeId } from "./hoveredNodeId.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultHoveredNodeId: CcState["appStatus"]["hoveredNodeId"] = null -export const hoveredNodeId = createReducer( - defaultHoveredNodeId, - on(setHoveredNodeId, (_state, action) => action.value) -) +export const hoveredNodeId = createReducer(defaultHoveredNodeId, on(setHoveredNodeId, setState(defaultHoveredNodeId))) diff --git a/visualization/app/codeCharta/state/store/appStatus/selectedBuildingId/selectedBuildingId.reducer.ts b/visualization/app/codeCharta/state/store/appStatus/selectedBuildingId/selectedBuildingId.reducer.ts index 3b4a3107647..541ec40453d 100644 --- a/visualization/app/codeCharta/state/store/appStatus/selectedBuildingId/selectedBuildingId.reducer.ts +++ b/visualization/app/codeCharta/state/store/appStatus/selectedBuildingId/selectedBuildingId.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { CcState } from "../../../../codeCharta.model" import { setSelectedBuildingId } from "./selectedBuildingId.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultSelectedBuildingId: CcState["appStatus"]["selectedBuildingId"] = null -export const selectedBuildingId = createReducer( - defaultSelectedBuildingId, - on(setSelectedBuildingId, (_state, action) => action.value) -) +export const selectedBuildingId = createReducer(defaultSelectedBuildingId, on(setSelectedBuildingId, setState(defaultSelectedBuildingId))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/areaMetric/areaMetric.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/areaMetric/areaMetric.reducer.ts index 7116d726ce5..6647229fa2f 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/areaMetric/areaMetric.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/areaMetric/areaMetric.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setAreaMetric } from "./areaMetric.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultAreaMetric: null | string = null -export const areaMetric = createReducer( - defaultAreaMetric, - on(setAreaMetric, (_state, action) => action.value) -) +export const areaMetric = createReducer(defaultAreaMetric, on(setAreaMetric, setState(defaultAreaMetric))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/colorMetric/colorMetric.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/colorMetric/colorMetric.reducer.ts index 7683c7a55cc..423accac226 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/colorMetric/colorMetric.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/colorMetric/colorMetric.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setColorMetric } from "./colorMetric.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultColorMetric: null | string = null -export const colorMetric = createReducer( - defaultColorMetric, - on(setColorMetric, (_state, action) => action.value) -) +export const colorMetric = createReducer(defaultColorMetric, on(setColorMetric, setState(defaultColorMetric))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/colorMode/colorMode.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/colorMode/colorMode.reducer.ts index cc7fb94a7e6..3d9c0e4ed48 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/colorMode/colorMode.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/colorMode/colorMode.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { ColorMode } from "../../../../codeCharta.model" import { setColorMode } from "./colorMode.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultColorMode = ColorMode.weightedGradient -export const colorMode = createReducer( - defaultColorMode, - on(setColorMode, (_state, action) => action.value) -) +export const colorMode = createReducer(defaultColorMode, on(setColorMode, setState(defaultColorMode))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/colorRange/colorRange.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/colorRange/colorRange.reducer.ts index 6d368b3ddd8..f07ae9a5cc7 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/colorRange/colorRange.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/colorRange/colorRange.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { setColorRange } from "./colorRange.actions" import { ColorRange } from "../../../../codeCharta.model" +import { mergeState } from "../../util/setState.reducer.factory" export const defaultColorRange: ColorRange = { from: 0, to: 0 } -export const colorRange = createReducer( - defaultColorRange, - on(setColorRange, (state, action) => ({ ...state, ...action.value })) -) +export const colorRange = createReducer(defaultColorRange, on(setColorRange, mergeState(defaultColorRange))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/distributionMetric/distributionMetric.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/distributionMetric/distributionMetric.reducer.ts index 765a95b5466..b2048423d9b 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/distributionMetric/distributionMetric.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/distributionMetric/distributionMetric.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setDistributionMetric } from "./distributionMetric.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultDistributionMetric: null | string = null -export const distributionMetric = createReducer( - defaultDistributionMetric, - on(setDistributionMetric, (_state, action) => action.value) -) +export const distributionMetric = createReducer(defaultDistributionMetric, on(setDistributionMetric, setState(defaultDistributionMetric))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/edgeMetric/edgeMetric.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/edgeMetric/edgeMetric.reducer.ts index 87778b87044..3f977144396 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/edgeMetric/edgeMetric.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/edgeMetric/edgeMetric.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setEdgeMetric } from "./edgeMetric.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultEdgeMetric: null | string = null -export const edgeMetric = createReducer( - defaultEdgeMetric, - on(setEdgeMetric, (_state, action) => action.value) -) +export const edgeMetric = createReducer(defaultEdgeMetric, on(setEdgeMetric, setState(defaultEdgeMetric))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/heightMetric/heightMetric.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/heightMetric/heightMetric.reducer.ts index 9131ea1886e..df8bec783b0 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/heightMetric/heightMetric.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/heightMetric/heightMetric.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setHeightMetric } from "./heightMetric.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultHeightMetric: null | string = null -export const heightMetric = createReducer( - defaultHeightMetric, - on(setHeightMetric, (_state, action) => action.value) -) +export const heightMetric = createReducer(defaultHeightMetric, on(setHeightMetric, setState(defaultHeightMetric))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/margin/margin.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/margin/margin.reducer.ts index 4f1eaffbf55..e35641efe93 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/margin/margin.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/margin/margin.reducer.ts @@ -1,8 +1,6 @@ import { createReducer, on } from "@ngrx/store" import { setMargin } from "./margin.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultMargin = 50 -export const margin = createReducer( - defaultMargin, - on(setMargin, (_state, action) => action.value) -) +export const margin = createReducer(defaultMargin, on(setMargin, setState(defaultMargin))) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/searchPattern/searchPattern.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/searchPattern/searchPattern.reducer.ts index b2058f799e0..27cf1b087d5 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/searchPattern/searchPattern.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/searchPattern/searchPattern.reducer.ts @@ -1,10 +1,11 @@ import { createReducer, on } from "@ngrx/store" import { setSearchPattern } from "./searchPattern.actions" import { setStandard } from "../../files/files.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultSearchPattern = "" export const searchPattern = createReducer( defaultSearchPattern, - on(setSearchPattern, (_state, action) => action.value), + on(setSearchPattern, setState(defaultSearchPattern)), on(setStandard, () => defaultSearchPattern) ) diff --git a/visualization/app/codeCharta/state/store/dynamicSettings/sortingOption/sortingOption.reducer.ts b/visualization/app/codeCharta/state/store/dynamicSettings/sortingOption/sortingOption.reducer.ts index fb42801bcca..f584a08fbd1 100644 --- a/visualization/app/codeCharta/state/store/dynamicSettings/sortingOption/sortingOption.reducer.ts +++ b/visualization/app/codeCharta/state/store/dynamicSettings/sortingOption/sortingOption.reducer.ts @@ -1,9 +1,7 @@ import { createReducer, on } from "@ngrx/store" import { SortingOption } from "../../../../codeCharta.model" import { setSortingOption } from "./sortingOption.actions" +import { setState } from "../../util/setState.reducer.factory" export const defaultSortingOption = SortingOption.NAME -export const sortingOption = createReducer( - defaultSortingOption, - on(setSortingOption, (_state, action) => action.value) -) +export const sortingOption = createReducer(defaultSortingOption, on(setSortingOption, setState(defaultSortingOption))) diff --git a/visualization/app/codeCharta/state/store/fileSettings/attributeDescriptors/attributeDescriptors.reducer.ts b/visualization/app/codeCharta/state/store/fileSettings/attributeDescriptors/attributeDescriptors.reducer.ts index f96b4dca026..ac76c413fc6 100644 --- a/visualization/app/codeCharta/state/store/fileSettings/attributeDescriptors/attributeDescriptors.reducer.ts +++ b/visualization/app/codeCharta/state/store/fileSettings/attributeDescriptors/attributeDescriptors.reducer.ts @@ -1,9 +1,10 @@ import { createReducer, on } from "@ngrx/store" import { setAttributeDescriptors } from "./attributeDescriptors.action" import { AttributeDescriptors } from "../../../../codeCharta.model" +import { setState } from "../../util/setState.reducer.factory" export const defaultAttributeDescriptors: AttributeDescriptors = {} export const attributeDescriptors = createReducer( defaultAttributeDescriptors, - on(setAttributeDescriptors, (_state, action) => action.value) + on(setAttributeDescriptors, setState(defaultAttributeDescriptors)) ) diff --git a/visualization/app/codeCharta/state/store/fileSettings/attributeTypes/attributeTypes.reducer.ts b/visualization/app/codeCharta/state/store/fileSettings/attributeTypes/attributeTypes.reducer.ts index 0fc8efc8459..420c09a5196 100644 --- a/visualization/app/codeCharta/state/store/fileSettings/attributeTypes/attributeTypes.reducer.ts +++ b/visualization/app/codeCharta/state/store/fileSettings/attributeTypes/attributeTypes.reducer.ts @@ -1,11 +1,12 @@ import { setAttributeTypes, updateAttributeType } from "./attributeTypes.actions" import { AttributeTypes } from "../../../../codeCharta.model" import { createReducer, on } from "@ngrx/store" +import { setState } from "../../util/setState.reducer.factory" export const defaultAttributeTypes: AttributeTypes = { nodes: {}, edges: {} } export const attributeTypes = createReducer( defaultAttributeTypes, - on(setAttributeTypes, (_state, action) => action.value), + on(setAttributeTypes, setState(defaultAttributeTypes)), on(updateAttributeType, (state, action) => ({ ...state, [action.category]: { ...state[action.category], [action.name]: action.attributeType } diff --git a/visualization/app/codeCharta/state/store/fileSettings/blacklist/blacklist.reducer.ts b/visualization/app/codeCharta/state/store/fileSettings/blacklist/blacklist.reducer.ts index 9c37c64636b..5a2dbd3afbf 100644 --- a/visualization/app/codeCharta/state/store/fileSettings/blacklist/blacklist.reducer.ts +++ b/visualization/app/codeCharta/state/store/fileSettings/blacklist/blacklist.reducer.ts @@ -2,11 +2,12 @@ import { addBlacklistItem, addBlacklistItems, removeBlacklistItem, setBlacklist import { addItemsToArray, addItemToArray, removeItemFromArray } from "../../../../util/arrayHelper" import { createReducer, on } from "@ngrx/store" import { BlacklistItem } from "../../../../codeCharta.model" +import { setState } from "../../util/setState.reducer.factory" export const defaultBlacklist: BlacklistItem[] = [] export const blacklist = createReducer( defaultBlacklist, - on(setBlacklist, (_state, action) => action.value), + on(setBlacklist, setState(defaultBlacklist)), on(addBlacklistItem, (state, action) => addItemToArray(state, action.item)), on(addBlacklistItems, (state, action) => addItemsToArray(state, action.items)), on(removeBlacklistItem, (state, action) => removeItemFromArray(state, action.item)) diff --git a/visualization/app/codeCharta/state/store/fileSettings/edges/edges.reducer.ts b/visualization/app/codeCharta/state/store/fileSettings/edges/edges.reducer.ts index e0ed3bfb50a..afc39b1176c 100644 --- a/visualization/app/codeCharta/state/store/fileSettings/edges/edges.reducer.ts +++ b/visualization/app/codeCharta/state/store/fileSettings/edges/edges.reducer.ts @@ -2,11 +2,12 @@ import { addEdge, removeEdge, setEdges } from "./edges.actions" import { addItemToArray, removeItemFromArray } from "../../../../util/arrayHelper" import { createReducer, on } from "@ngrx/store" import { Edge } from "../../../../codeCharta.model" +import { setState } from "../../util/setState.reducer.factory" export const defaultEdges: Edge[] = [] export const edges = createReducer( defaultEdges, - on(setEdges, (_state, action) => action.value), + on(setEdges, setState(defaultEdges)), on(addEdge, (state, action) => addItemToArray(state, action.edge)), on(removeEdge, (state, action) => removeItemFromArray(state, action.edge)) ) diff --git a/visualization/app/codeCharta/state/store/fileSettings/markedPackages/markedPackages.reducer.ts b/visualization/app/codeCharta/state/store/fileSettings/markedPackages/markedPackages.reducer.ts index 117be2cac75..d1213129757 100644 --- a/visualization/app/codeCharta/state/store/fileSettings/markedPackages/markedPackages.reducer.ts +++ b/visualization/app/codeCharta/state/store/fileSettings/markedPackages/markedPackages.reducer.ts @@ -4,11 +4,12 @@ import { addMarkedPackage } from "./util/addMarkedPackage" import { findIndexOfMarkedPackageOrParent } from "./util/findIndexOfMarkedPackageOrParent" import { createReducer, on } from "@ngrx/store" import { MarkedPackage } from "../../../../codeCharta.model" +import { setState } from "../../util/setState.reducer.factory" export const defaultMarkedPackages: MarkedPackage[] = [] export const markedPackages = createReducer( defaultMarkedPackages, - on(setMarkedPackages, (_state, action) => action.value), + on(setMarkedPackages, setState(defaultMarkedPackages)), on(markPackages, (state, action) => { const markedPackagesMap = new Map(state.map(entry => [entry.path, entry])) diff --git a/visualization/app/codeCharta/state/store/files/files.reducer.ts b/visualization/app/codeCharta/state/store/files/files.reducer.ts index 3b37efea08f..e1e61ed3049 100644 --- a/visualization/app/codeCharta/state/store/files/files.reducer.ts +++ b/visualization/app/codeCharta/state/store/files/files.reducer.ts @@ -15,11 +15,12 @@ import { CCFile } from "../../../codeCharta.model" import { FileSelectionState, FileState } from "../../../model/files/files" import { isEqual } from "../../../model/files/files.helper" import { createReducer, on } from "@ngrx/store" +import { setState } from "../util/setState.reducer.factory" export const defaultFiles: FileState[] = [] export const files = createReducer( defaultFiles, - on(setFiles, (_state, action) => action.value), + on(setFiles, setState(defaultFiles)), on(addFile, (state, action) => [...state, { file: action.file, selectedAs: FileSelectionState.None }]), on(removeFile, (state, action) => removeFileFromState(state, action.fileName)), on(setDelta, (state, action) => setDeltaState(state, action.referenceFile, action.comparisonFile)), diff --git a/visualization/app/codeCharta/state/store/util/setState.reducer.factory.spec.ts b/visualization/app/codeCharta/state/store/util/setState.reducer.factory.spec.ts new file mode 100644 index 00000000000..3711956abb2 --- /dev/null +++ b/visualization/app/codeCharta/state/store/util/setState.reducer.factory.spec.ts @@ -0,0 +1,24 @@ +import { mergeState, setState } from "./setState.reducer.factory" + +describe("setState.reducer.factory", () => { + describe("setState", () => { + it("should return default state when action payload is undefined", () => { + expect(setState(2)(undefined, { type: "generic", value: undefined })).toBe(2) + }) + + it("should return value of action", () => { + expect(setState(2)(undefined, { type: "generic", value: 3 })).toBe(3) + }) + }) + + describe("mergeState", () => { + const defaultState: { a: string; b: string } = { a: "a", b: "b" } + it("should return default state when action payload is undefined", () => { + expect(mergeState(defaultState)(defaultState, { type: "generic", value: undefined })).toBe(defaultState) + }) + + it("should merge in value of action", () => { + expect(mergeState(defaultState)(defaultState, { type: "generic", value: { a: "A" } })).toEqual({ a: "A", b: "b" }) + }) + }) +}) diff --git a/visualization/app/codeCharta/state/store/util/setState.reducer.factory.ts b/visualization/app/codeCharta/state/store/util/setState.reducer.factory.ts new file mode 100644 index 00000000000..fcbadcca62b --- /dev/null +++ b/visualization/app/codeCharta/state/store/util/setState.reducer.factory.ts @@ -0,0 +1,17 @@ +/** + * Most of CodeCharta's action will reset when action's payload is undefined. + * E.g., this has currently an effect when applying a scenario without map colors, + * to ensure that it will have default colors. + */ + +import { Action } from "@ngrx/store" + +export const setState = + (defaultValue: T) => + (_state: T, action: Action & { value: T }) => + action.value === undefined ? defaultValue : action.value + +export const mergeState = + >(defaultValue: T) => + (state: T, action: Action & { value: Partial }) => + action.value === undefined ? defaultValue : { ...state, ...action.value }