All files / app/codeCharta/ui/resetSettingsButton getPartialDefaultState.ts

100% Statements 27/27
100% Branches 6/6
100% Functions 1/1
100% Lines 26/26

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  9x 9x 9x 9x   9x   9x 6x 6x   6x 14x 14x 14x   14x 42x 38x 26x 26x   38x 14x   24x 24x           6x 6x     6x 1x     6x    
import { RecursivePartial, Settings, CcState } from "../../codeCharta.model"
import { convertToVectors } from "../../util/settingsHelper"
import { codeMapNodesSelector } from "../../state/selectors/accumulatedData/codeMapNodes.selector"
import { getNumberOfTopLabels } from "../../state/effects/updateVisibleTopLabels/getNumberOfTopLabels"
import { defaultState } from "../../state/store/state.manager"
 
const APP_SETTINGS_AMOUNT_OF_TOP_LABELS = "appSettings.amountOfTopLabels"
 
export const getPartialDefaultState = (settingKeys: string[], state: CcState) => {
    const updatedSettings: RecursivePartial<Settings> = {}
    let settingsCounter = 0
 
    for (const token of settingKeys) {
        const steps = token.split(".")
        let defaultSettingsPointer = defaultState
        let updatedSettingsPointer = updatedSettings
 
        for (const [index, step] of steps.entries()) {
            if (defaultSettingsPointer[step] !== undefined) {
                if (!updatedSettingsPointer[step]) {
                    updatedSettingsPointer[step] = {}
                    settingsCounter++
                }
                if (index === steps.length - 1) {
                    updatedSettingsPointer[step] = defaultSettingsPointer[step]
                } else {
                    defaultSettingsPointer = defaultSettingsPointer[step]
                    updatedSettingsPointer = updatedSettingsPointer[step]
                }
            }
        }
    }
 
    if (settingsCounter !== 0) {
        convertToVectors(updatedSettings)
    }
 
    if (settingKeys.includes(APP_SETTINGS_AMOUNT_OF_TOP_LABELS)) {
        updatedSettings.appSettings.amountOfTopLabels = getNumberOfTopLabels(codeMapNodesSelector(state))
    }
 
    return updatedSettings
}