mirror of
https://github.com/Kong/insomnia.git
synced 2026-04-21 06:37:36 -04:00
Add ability to duplicate environments (#1706)
This commit is contained in:
committed by
Gregory Schier
parent
d2d29bd1e1
commit
8a290784aa
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user