All files / app/codeCharta/state/store/files files.reducer.ts

98.03% Statements 50/51
100% Branches 14/14
100% Functions 22/22
97.91% Lines 47/48

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 10643x                       43x 43x 43x 43x   43x 43x     32x 6x 8x 3x 2x 1x   6x   7x     28x       6x 1x   8x 5x 5x 2x         5x       8x 14x 8x   6x 3x   3x         3x 6x 3x   3x 1x   2x         2x 5x 2x   3x 2x   1x         1x 2x 1x   1x 1x             52x          
import {
    addFile,
    removeFiles,
    setDelta,
    setDeltaComparison,
    setDeltaReference,
    setFiles,
    setStandard,
    setStandardByNames,
    switchReferenceAndComparison
} from "./files.actions"
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, setState(defaultFiles)),
    on(addFile, (state, action) => [...state, { file: action.file, selectedAs: FileSelectionState.None }]),
    on(removeFiles, (state, action) => removeFilesFromState(state, action.fileNames)),
    on(setDelta, (state, action) => setDeltaState(state, action.referenceFile, action.comparisonFile)),
    on(setDeltaReference, (state, action) => setDeltaReferenceState(state, action.file)),
    on(setDeltaComparison, (state, action) => setDeltaComparisonState(state, action.file)),
    on(switchReferenceAndComparison, state => switchReferenceAndComparisonState(state)),
    on(setStandard, (state, action) =>
        setStandardByNamesState(
            state,
            action.files.map(x => x.fileMeta.fileName)
        )
    ),
    on(setStandardByNames, (state, action) => setStandardByNamesState(state, action.fileNames))
)
 
function removeFilesFromState(state: FileState[], fileNames: string[]): FileState[] {
    if (fileNames.length === 0) {
        return state
    }
    const newState = state.filter(fileState => !fileNames.includes(fileState.file.fileMeta.fileName))
    const isAnyFileSelected = newState.some(fileState => fileState.selectedAs === FileSelectionState.Partial)
    if (!isAnyFileSelected) {
        newState[0] = {
            ...newState[0],
            selectedAs: FileSelectionState.Partial
        }
    }
    return newState
}
 
function setDeltaState(state: FileState[], reference: CCFile, comparison?: CCFile): FileState[] {
    return state.map(file => {
        if (isEqual(file.file, reference)) {
            return { ...file, selectedAs: FileSelectionState.Reference }
        }
        if (comparison && isEqual(file.file, comparison)) {
            return { ...file, selectedAs: FileSelectionState.Comparison }
        }
        return { ...file, selectedAs: FileSelectionState.None }
    })
}
 
function setDeltaReferenceState(state: FileState[], reference: CCFile): FileState[] {
    return state.map(file => {
        if (isEqual(file.file, reference)) {
            return { ...file, selectedAs: FileSelectionState.Reference }
        }
        if (file.selectedAs === FileSelectionState.Comparison) {
            return file
        }
        return { ...file, selectedAs: FileSelectionState.None }
    })
}
 
function setDeltaComparisonState(state: FileState[], comparison: CCFile): FileState[] {
    return state.map(file => {
        if (file.file === comparison) {
            return { ...file, selectedAs: FileSelectionState.Comparison }
        }
        if (file.selectedAs === FileSelectionState.Reference) {
            return file
        }
        return { ...file, selectedAs: FileSelectionState.None }
    })
}
 
function switchReferenceAndComparisonState(state: FileState[]): FileState[] {
    return state.map(file => {
        if (file.selectedAs === FileSelectionState.Reference) {
            return { ...file, selectedAs: FileSelectionState.Comparison }
        }
        if (file.selectedAs === FileSelectionState.Comparison) {
            return { ...file, selectedAs: FileSelectionState.Reference }
        }
        return file
    })
}
 
function setStandardByNamesState(state: FileState[], partialFileNames: string[]): FileState[] {
    return state.map(fileState => ({
        ...fileState,
        selectedAs: partialFileNames.includes(fileState.file.fileMeta.fileName) ? FileSelectionState.Partial : FileSelectionState.None
    }))
}