All files / app/codeCharta/services/loadFile loadFile.service.ts

97.43% Statements 38/39
100% Branches 3/3
85.71% Functions 6/7
97.29% Lines 36/37

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 76104x 104x 104x 104x   104x     104x 104x 104x 104x 104x 104x 104x   104x     104x 104x   22x       24x 1x             22x 22x 22x               33x 33x 33x   33x         38x 3x     33x 6x         33x 6x     27x   27x 32x 27x 27x      
import { Injectable, OnDestroy } from "@angular/core"
import { tap } from "rxjs"
import { MatDialog } from "@angular/material/dialog"
import { clone } from "../../util/clone"
import { CCFileValidationResult } from "../../util/fileValidator"
import { setFiles, setStandardByNames } from "../../state/store/files/files.actions"
import { FileState } from "../../model/files/files"
import { NameDataPair, CcState } from "../../codeCharta.model"
import { referenceFileSelector } from "../../state/selectors/referenceFile/referenceFile.selector"
import { ErrorDialogComponent } from "../../ui/dialogs/errorDialog/errorDialog.component"
import { loadFilesValidationToErrorDialog } from "../../util/loadFilesValidationToErrorDialog"
import { enrichFileStatesAndRecentFilesWithValidationResults } from "./fileParser"
import { fileRoot } from "./fileRoot"
import { Store, State } from "@ngrx/store"
import { setCurrentFilesAreSampleFiles } from "../../state/store/appStatus/currentFilesAreSampleFiles/currentFilesAreSampleFiles.actions"
 
export const NO_FILES_LOADED_ERROR_MESSAGE = "File(s) could not be loaded"
 
@Injectable({ providedIn: "root" })
export class LoadFileService implements OnDestroy {
    static readonly CC_FILE_EXTENSION = ".cc.json"
 
    referenceFileSubscription = this.store
        .select(referenceFileSelector)
        .pipe(
            tap(newReferenceFile => {
                if (newReferenceFile) {
                    fileRoot.updateRoot(newReferenceFile.map.name)
                }
            })
        )
        .subscribe()
 
    constructor(
        private store: Store<CcState>,
        private state: State<CcState>,
        private dialog: MatDialog
    ) {}
 
    ngOnDestroy(): void {
        this.referenceFileSubscription.unsubscribe()
    }
 
    loadFiles(nameDataPairs: NameDataPair[]) {
        const fileStates: FileState[] = clone(this.state.getValue().files)
        const recentFiles: string[] = []
        const fileValidationResults: CCFileValidationResult[] = []
 
        enrichFileStatesAndRecentFilesWithValidationResults(
            fileStates,
            recentFiles,
            nameDataPairs,
            fileValidationResults,
            () => this.state.getValue().appStatus.currentFilesAreSampleFiles,
            () => this.store.dispatch(setCurrentFilesAreSampleFiles({ value: false }))
        )
 
        if (fileValidationResults.length > 0) {
            this.dialog.open(ErrorDialogComponent, {
                data: loadFilesValidationToErrorDialog(fileValidationResults)
            })
        }
 
        if (recentFiles.length === 0) {
            throw new Error(NO_FILES_LOADED_ERROR_MESSAGE)
        }
 
        this.store.dispatch(setFiles({ value: fileStates }))
 
        const recentFile = recentFiles[0]
        const rootName = this.state.getValue().files.find(f => f.file.fileMeta.fileName === recentFile).file.map.name
        this.store.dispatch(setStandardByNames({ fileNames: recentFiles }))
        fileRoot.updateRoot(rootName)
    }
}