From 5fbfbccd2a62386f16219da12d175167dae6ddd6 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 3 Jul 2017 13:51:55 -0700 Subject: [PATCH] Add prompt to export private environments (if any) (#343) --- app/common/import.js | 48 ++++++----- .../components/editors/auth/auth-wrapper.js | 16 ++-- app/ui/components/modals/ask-modal.js | 83 +++++++++++++++++++ app/ui/components/wrapper.js | 2 + app/ui/redux/modules/global.js | 26 +++++- 5 files changed, 144 insertions(+), 31 deletions(-) create mode 100644 app/ui/components/modals/ask-modal.js diff --git a/app/common/import.js b/app/common/import.js index 31cb15391b..c6765e6861 100644 --- a/app/common/import.js +++ b/app/common/import.js @@ -79,7 +79,6 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) } const {data} = results; - console.log('IMPORTING', JSON.parse(rawContent)); // Fetch the base environment in case we need it const baseEnvironment = await models.environment.getOrCreateForWorkspace(workspace); @@ -145,7 +144,7 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) }; } -export async function exportJSON (parentDoc = null) { +export async function exportJSON (parentDoc = null, includePrivateDocs = false) { const data = { _type: 'export', __export_format: EXPORT_FORMAT, @@ -156,32 +155,37 @@ export async function exportJSON (parentDoc = null) { const docs = await db.withDescendants(parentDoc); - data.resources = docs.filter(d => ( - !d.isPrivate && ( + data.resources = docs + .filter(d => ( + // Don't include if private, except if we want to + !d.isPrivate || includePrivateDocs + )) + .filter(d => ( + // Only export these model types d.type === models.request.type || d.type === models.requestGroup.type || d.type === models.workspace.type || d.type === models.cookieJar.type || d.type === models.environment.type - ) - )).map(d => { - if (d.type === models.workspace.type) { - d._type = EXPORT_TYPE_WORKSPACE; - } else if (d.type === models.cookieJar.type) { - d._type = EXPORT_TYPE_COOKIE_JAR; - } else if (d.type === models.environment.type) { - d._type = EXPORT_TYPE_ENVIRONMENT; - } else if (d.type === models.requestGroup.type) { - d._type = EXPORT_TYPE_REQUEST_GROUP; - } else if (d.type === models.request.type) { - d._type = EXPORT_TYPE_REQUEST; - } + )) + .map(d => { + if (d.type === models.workspace.type) { + d._type = EXPORT_TYPE_WORKSPACE; + } else if (d.type === models.cookieJar.type) { + d._type = EXPORT_TYPE_COOKIE_JAR; + } else if (d.type === models.environment.type) { + d._type = EXPORT_TYPE_ENVIRONMENT; + } else if (d.type === models.requestGroup.type) { + d._type = EXPORT_TYPE_REQUEST_GROUP; + } else if (d.type === models.request.type) { + d._type = EXPORT_TYPE_REQUEST; + } - // Delete the things we don't want to export - delete d.type; - delete d.isPrivate; - return d; - }); + // Delete the things we don't want to export + delete d.type; + delete d.isPrivate; + return d; + }); return JSON.stringify(data, null, '\t'); } diff --git a/app/ui/components/editors/auth/auth-wrapper.js b/app/ui/components/editors/auth/auth-wrapper.js index 27a3402ebd..ddb0248040 100644 --- a/app/ui/components/editors/auth/auth-wrapper.js +++ b/app/ui/components/editors/auth/auth-wrapper.js @@ -49,15 +49,15 @@ class AuthWrapper extends PureComponent { } else if (authentication.type === AUTH_OAUTH_1) { return (
-

+

-
- Want OAuth 1.0? Please upvote - the - Issue on GitHub - -
-

+

+ Want OAuth 1.0? Please upvote + the + Issue on GitHub + +

+
); } else if (authentication.type === AUTH_DIGEST) { diff --git a/app/ui/components/modals/ask-modal.js b/app/ui/components/modals/ask-modal.js new file mode 100644 index 0000000000..521f39b67a --- /dev/null +++ b/app/ui/components/modals/ask-modal.js @@ -0,0 +1,83 @@ +import React, {PureComponent} from 'react'; +import autobind from 'autobind-decorator'; +import Modal from '../base/modal'; +import ModalBody from '../base/modal-body'; +import ModalHeader from '../base/modal-header'; +import ModalFooter from '../base/modal-footer'; + +@autobind +class AskModal extends PureComponent { + constructor (props) { + super(props); + + this.state = { + title: '', + message: '' + }; + } + + _setModalRef (m) { + this.modal = m; + } + + _handleYes () { + this.hide(); + this._doneCallback && this._doneCallback(true); + this._promiseCallback(true); + } + + _handleNo () { + this.hide(); + this._doneCallback && this._doneCallback(false); + this._promiseCallback(false); + } + + hide () { + this.modal.hide(); + } + + show (options = {}) { + const { + title, + message, + onDone + } = options; + + this.modal.show(); + + this._doneCallback = onDone; + + this.setState({title, message}); + + return new Promise(resolve => { + this._promiseCallback = resolve; + }); + } + + render () { + const {message, title} = this.state; + + return ( + + {title || 'Confirm?'} + + {message} + + +
+ + +
+
+
+ ); + } +} + +AskModal.propTypes = {}; + +export default AskModal; diff --git a/app/ui/components/wrapper.js b/app/ui/components/wrapper.js index 7d96fcb5aa..0b6dfa35b5 100644 --- a/app/ui/components/wrapper.js +++ b/app/ui/components/wrapper.js @@ -11,6 +11,7 @@ import LoginModal from './modals/login-modal'; import PaymentNotificationModal from './modals/payment-notification-modal'; import NunjucksModal from './modals/nunjucks-modal'; import PromptModal from './modals/prompt-modal'; +import AskModal from './modals/ask-modal'; import RequestCreateModal from './modals/request-create-modal'; import RequestPane from './request-pane'; import RequestSwitcherModal from './modals/request-switcher-modal'; @@ -406,6 +407,7 @@ class Wrapper extends PureComponent { + diff --git a/app/ui/redux/modules/global.js b/app/ui/redux/modules/global.js index 96466a2ced..aee489c194 100644 --- a/app/ui/redux/modules/global.js +++ b/app/ui/redux/modules/global.js @@ -3,6 +3,7 @@ import React from 'react'; import {combineReducers} from 'redux'; import fs from 'fs'; import path from 'path'; +import AskModal from '../../../ui/components/modals/ask-modal'; import * as moment from 'moment'; import * as importUtils from '../../../common/import'; @@ -183,7 +184,30 @@ export function exportFile (workspaceId = null) { dispatch(loadStart()); const workspace = await models.workspace.getById(workspaceId); - const json = await importUtils.exportJSON(workspace); + + // Check if we want to export private environments + let environments; + if (workspace) { + const parentEnv = await models.environment.getOrCreateForWorkspace(workspace); + environments = [ + parentEnv, + ...await models.environment.findByParentId(parentEnv._id) + ]; + } else { + environments = await models.environment.all(); + } + + let exportPrivateEnvironments = false; + const privateEnvironments = environments.filter(e => e.isPrivate); + if (privateEnvironments.length) { + const names = privateEnvironments.map(e => e.name).join(', '); + exportPrivateEnvironments = await showModal(AskModal, { + title: 'Export Private Environments?', + message: `Do you want to include private environments (${names}) in your export?` + }); + } + + const json = await importUtils.exportJSON(workspace, exportPrivateEnvironments); const date = moment().format('YYYY-MM-DD'); const name = (workspace ? workspace.name : 'Insomnia All').replace(/ /g, '-');