Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Saving assets in async foreach loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarlow12 authored and wbreza committed Apr 19, 2019
1 parent f11edfa commit 57f430f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 27 deletions.
79 changes: 55 additions & 24 deletions src/react/components/pages/editorPage/editorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export default class EditorPage extends React.Component<IEditorPageProps, IEdito
</div>
<div className="editor-page-right-sidebar">
<TagInput
tags={this.props.project.tags}
tags={this.state.project ? this.state.project.tags : this.props.project.tags}
lockedTags={this.state.lockedTags}
selectedRegions={this.state.selectedRegions}
onChange={this.onTagsChanged}
Expand Down Expand Up @@ -309,44 +309,75 @@ export default class EditorPage extends React.Component<IEditorPageProps, IEdito
}

private onTagRenamed = async (tagName: string, newTagName: string): Promise<void> => {
const { project, selectedAsset } = this.state;
const assetService = new AssetService(project);
const asset = await assetService.renameTag(project.assets, tagName, newTagName, selectedAsset);

const newProject: IProject = {
...project,
tags: project.tags.map((t) => (t.name === tagName) ? {...t, name: newTagName} : t),
};
const { project } = this.state;
this.setState({
project: newProject,
selectedAsset: asset || selectedAsset,
project: {
...project,
tags: project.tags.map((t) => (t.name === tagName) ? {...t, name: newTagName} : t),
}
}, async () => {
await this.props.actions.saveProject(newProject);
if (asset) {
this.canvas.current.updateCanvasToolsRegions(asset);
await this.props.actions.saveProject(project);
if (this.canvas.current) {
this.canvas.current.updateCanvasToolsRegions();
}
});

// const { project, selectedAsset } = this.state;
// const assetService = new AssetService(project);
// const asset = await assetService.renameTag(project.assets, tagName, newTagName, selectedAsset);

// const newProject: IProject = {
// ...project,
// tags: project.tags.map((t) => (t.name === tagName) ? {...t, name: newTagName} : t),
// };
// this.setState({
// project: newProject,
// selectedAsset: asset || selectedAsset,
// }, async () => {
// await this.props.actions.saveProject(newProject);
// if (asset) {
// this.canvas.current.updateCanvasToolsRegions(asset);
// }
// });
}

private confirmTagDeleted = (tagName: string): void => {
this.deleteTagConfirm.current.open(tagName);
}

private onTagDeleted = async (tagName: string): Promise<void> => {
const { selectedAsset } = this.state;
const { project } = this.props;
const newProject: IProject = {
const { project, selectedAsset } = this.state;
const newProject = {
...project,
tags: project.tags.filter((t) => t.name !== tagName),
};
await this.props.actions.saveProject(newProject);
}

const assetService = new AssetService(project);
const assetService = new AssetService(newProject);
const asset = await assetService.deleteTag(project.assets, tagName, selectedAsset);
if (asset) {
this.canvas.current.updateCanvasToolsRegions(asset);
this.setState({selectedAsset: asset});
}
this.setState({
project: newProject,
selectedAsset: asset || selectedAsset,
}, async () => {
await this.props.actions.saveProject(newProject);
if (this.canvas.current) {
this.canvas.current.updateCanvasToolsRegions(asset);
}
});

// const { selectedAsset } = this.state;
// const { project } = this.props;
// const newProject: IProject = {
// ...project,
// tags: project.tags.filter((t) => t.name !== tagName),
// };
// await this.props.actions.saveProject(newProject);

// const assetService = new AssetService(project);
// const asset = await assetService.deleteTag(project.assets, tagName, selectedAsset);
// if (asset) {
// this.canvas.current.updateCanvasToolsRegions(asset);
// this.setState({selectedAsset: asset});
// }
}

private onCtrlTagClicked = (tag: ITag): void => {
Expand Down
24 changes: 21 additions & 3 deletions src/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,33 @@ export class AssetService {
// Loop over assets and update if necessary
await assetKeys.forEachAsync(async (assetKey) => {
const asset = assets[assetKey];
if (asset.state !== AssetState.Tagged) {
return;
}
const assetMetadata = await this.getAssetMetadata(asset);
const updatedAssetMetadata = this.updateTagInAssetMetadata(assetMetadata, tagName, transformer);
if (updatedAssetMetadata) {
await this.save(updatedAssetMetadata);
}
});

// for (const assetKey of assetKeys) {
// const asset = assets[assetKey];
// if (asset.state !== AssetState.Tagged) {
// debugger;
// return;
// }
// const assetMetadata = await this.getAssetMetadata(asset);
// debugger;
// const updatedAssetMetadata = this.updateTagInAssetMetadata(assetMetadata, tagName, transformer);
// debugger;
// if (updatedAssetMetadata) {
// await this.save(updatedAssetMetadata);
// }
// }

if (currentAsset) {
const asset = this.updateTagInAssetMetadata(currentAsset, tagName, transformer);
debugger;
return asset;
}
}

/**
Expand Down

0 comments on commit 57f430f

Please sign in to comment.