Skip to content

Commit

Permalink
update file on create variant
Browse files Browse the repository at this point in the history
  • Loading branch information
christianblos committed Feb 16, 2018
1 parent a8e4483 commit bd3e78b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/renderer/actions/createVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
let promise: Promise<DataEntry[]>;
Expand All @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/renderer/stores/schemaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bd3e78b

Please sign in to comment.