From bd3e78bed4d44419acd1fd38a5d330d1dbf197f2 Mon Sep 17 00:00:00 2001 From: Christian Blos Date: Fri, 16 Feb 2018 15:21:06 +0100 Subject: [PATCH] update file on create variant --- src/renderer/actions/createVariant.ts | 3 +++ src/renderer/stores/schemaStore.ts | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/renderer/actions/createVariant.ts b/src/renderer/actions/createVariant.ts index a417d8d..7c02d89 100644 --- a/src/renderer/actions/createVariant.ts +++ b/src/renderer/actions/createVariant.ts @@ -6,6 +6,7 @@ import {readData} from '@/functions/domain/readData'; import {saveData} from '@/functions/domain/saveData'; import {editorStore} from '@/stores/editorStore'; import {projectStore} from '@/stores/projectStore'; +import {schemaStore} from '@/stores/schemaStore'; export function createVariant(file: DataFile, variantId: string, copyEntries: boolean): Promise { let promise: Promise; @@ -28,6 +29,8 @@ function createVariantFile(variantId: string, file: string, entries: DataEntry[] let currentFile: DataFile = editorStore.currentFile.file; const idx: number = currentFile.addVariant(new FileVariant(variantFile, variantId)); + schemaStore.updateFile(currentFile); + return saveData(variantFile, entries) .then(() => { return selectFile(currentFile, idx); diff --git a/src/renderer/stores/schemaStore.ts b/src/renderer/stores/schemaStore.ts index 237cc27..f175ed3 100644 --- a/src/renderer/stores/schemaStore.ts +++ b/src/renderer/stores/schemaStore.ts @@ -35,6 +35,17 @@ export class SchemaStore { this._files = files; } + @action + updateFile(newFile: DataFile): void { + this._files = this.mapFiles(this._files, (file: DataFile) => { + if (file.basename == newFile.basename) { + return newFile; + } + + return file; + }); + } + @action reset(): void { this.schemas.clear(); @@ -71,6 +82,18 @@ export class SchemaStore { } } } + + private mapFiles(files: DataFileType[], fn: (file: DataFile) => DataFile): DataFileType[] { + return files.map((current: DataFileType) => { + if (current.isDir) { + let dir: DataDir = current as DataDir; + dir.children = this.mapFiles(dir.children, fn); + return dir; + } else { + return fn(current as DataFile); + } + }); + } } export const schemaStore = new SchemaStore();