Skip to content

Commit

Permalink
Add ability to duplicate environments (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeeSeal authored and gschier committed Oct 7, 2019
1 parent d2d29bd commit 8a29078
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/insomnia-app/app/models/environment.js
Expand Up @@ -82,6 +82,20 @@ export function getById(id: string): Promise<Environment | null> {
return db.get(type, id);
}

export async function duplicate(environment: Environment): Promise<Environment> {
const name = `${environment.name} (Copy)`;

// Get sort key of next environment
const q = { metaSortKey: { $gt: environment.metaSortKey } };
const [nextEnvironment] = await db.find(type, q, { metaSortKey: 1 });
const nextSortKey = nextEnvironment ? nextEnvironment.metaSortKey : environment.metaSortKey + 100;

// Calculate new sort key
const metaSortKey = (environment.metaSortKey + nextSortKey) / 2;

return db.duplicate(environment, { name, metaSortKey });
}

export function remove(environment: Environment): Promise<void> {
return db.remove(environment);
}
Expand Down
Expand Up @@ -205,6 +205,12 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
await this._load(workspace, environment);
}

async _handleDuplicateEnvironment(environment: Environment) {
const { workspace } = this.state;
const newEnvironment = await models.environment.duplicate(environment);
this._load(workspace, newEnvironment);
}

async _handleDeleteEnvironment(environment: Environment) {
const { rootEnvironment, workspace } = this.state;

Expand Down Expand Up @@ -484,6 +490,13 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
</DropdownItem>
</Dropdown>

<Button
value={activeEnvironment}
onClick={this._handleDuplicateEnvironment}
className="btn btn--clicky space-right">
<i className="fa fa-copy" /> Duplicate
</Button>

<PromptButton
value={activeEnvironment}
onClick={this._handleDeleteEnvironment}
Expand Down

0 comments on commit 8a29078

Please sign in to comment.