From d3c95e9c879e8e07d7668e8e44dfba2ba47f717b Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 13 Nov 2017 19:05:46 +0000 Subject: [PATCH] Add flow to EnvironmentsDropdown --- app/common/database.js | 18 +++++++- app/models/environment.js | 12 ++--- app/models/index.js | 10 +++- .../dropdowns/environments-dropdown.js | 46 ++++++++++--------- app/ui/components/request-pane.js | 5 +- 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/app/common/database.js b/app/common/database.js index 2203626d6b..95c9f2ae44 100644 --- a/app/common/database.js +++ b/app/common/database.js @@ -318,6 +318,22 @@ export async function docUpdate (originalDoc: T, patch: Object = { return update(doc); } +export async function docCreateNoMigrate ( + type: string, + ...patches: Array +): Promise { + const doc = await initModel( + type, + ...patches, + + // Fields that the user can't touch + {type: type}, + {__NO_MIGRATE: true} + ); + + return insert(doc); +} + export async function docCreate ( type: string, ...patches: Array @@ -415,7 +431,7 @@ export async function duplicate (originalDoc: T, patch: Object = { delete newDoc.created; delete newDoc.modified; - const createdDoc = await docCreate(newDoc.type, newDoc); + const createdDoc = await docCreateNoMigrate(newDoc.type, newDoc); // 2. Get all the children for (const type of allTypes()) { diff --git a/app/models/environment.js b/app/models/environment.js index 44a8fedaa3..4848938fd0 100644 --- a/app/models/environment.js +++ b/app/models/environment.js @@ -49,18 +49,18 @@ export function findByParentId (parentId: string): Promise> { } export async function getOrCreateForWorkspaceId (workspaceId: string): Promise { - let environment = await db.getWhere(type, { - parentId: workspaceId - }); + const environments = await db.find(type, {parentId: workspaceId}); - if (!environment) { - environment = await create({ + console.log('ALL', await db.find(type, {parentId: workspaceId})); + + if (!environments.length) { + return await create({ parentId: workspaceId, name: 'Base Environment' }); } - return environment; + return environments[environments.length - 1]; } export async function getOrCreateForWorkspace (workspace: Workspace): Promise { diff --git a/app/models/index.js b/app/models/index.js index 8f3964db8f..ac939be891 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -85,7 +85,10 @@ export function getModelName (type: string, count: number = 1) { } } -export async function initModel (type: string, ...sources: Array): Promise { +export async function initModel ( + type: string, + ...sources: Array +): Promise { const model = getModel(type); if (!model) { @@ -111,7 +114,10 @@ export async function initModel (type: string, ...sources: Array< // Migrate the model // NOTE: Do migration before pruning because we might need to look at those fields - const migratedDoc = await model.migrate(fullObject); + const noMigrate = fullObject.__NO_MIGRATE; + const migratedDoc = noMigrate + ? fullObject + : await model.migrate(fullObject); // Prune extra keys from doc for (const key of Object.keys(migratedDoc)) { diff --git a/app/ui/components/dropdowns/environments-dropdown.js b/app/ui/components/dropdowns/environments-dropdown.js index 2b3d278cef..6409328680 100644 --- a/app/ui/components/dropdowns/environments-dropdown.js +++ b/app/ui/components/dropdowns/environments-dropdown.js @@ -1,5 +1,5 @@ -import React, {PureComponent} from 'react'; -import PropTypes from 'prop-types'; +// @flow +import * as React from 'react'; import autobind from 'autobind-decorator'; import EnvironmentsModal from '../modals/workspace-environments-edit-modal'; import {Dropdown, DropdownButton, DropdownDivider, DropdownHint, DropdownItem} from '../base/dropdown'; @@ -9,10 +9,25 @@ import Tooltip from '../tooltip'; import {executeHotKey} from '../../../common/hotkeys'; import * as hotkeys from '../../../common/hotkeys'; import KeydownBinder from '../keydown-binder'; +import type {Workspace} from '../../../models/workspace'; +import type {Environment} from '../../../models/environment'; + +type Props = { + workspace: Workspace, + handleChangeEnvironment: Function, + workspace: Workspace, + environments: Array, + + // Optional + className?: string, + activeEnvironment: Environment | null +}; @autobind -class EnvironmentsDropdown extends PureComponent { - _handleActivateEnvironment (environmentId) { +class EnvironmentsDropdown extends React.PureComponent { + _dropdown: ?Dropdown; + + _handleActivateEnvironment (environmentId: string) { this.props.handleChangeEnvironment(environmentId); trackEvent('Environment', environmentId ? 'Activate' : 'Deactivate'); } @@ -21,11 +36,11 @@ class EnvironmentsDropdown extends PureComponent { showModal(EnvironmentsModal, this.props.workspace); } - _setDropdownRef (n) { + _setDropdownRef (n: ?Dropdown) { this._dropdown = n; } - renderEnvironmentItem (environment) { + renderEnvironmentItem (environment: Environment) { return ( { this._dropdown && this._dropdown.toggle(true); }); @@ -77,9 +92,9 @@ class EnvironmentsDropdown extends PureComponent { )}
- {activeEnvironment && activeEnvironment.color && ( + {(activeEnvironment && activeEnvironment.color) ? ( - )} + ) : null} {description}
@@ -105,17 +120,4 @@ class EnvironmentsDropdown extends PureComponent { } } -EnvironmentsDropdown.propTypes = { - // Functions - handleChangeEnvironment: PropTypes.func.isRequired, - - // Other - workspace: PropTypes.object.isRequired, - environments: PropTypes.arrayOf(PropTypes.object).isRequired, - - // Optional - activeEnvironment: PropTypes.object, - className: PropTypes.string -}; - export default EnvironmentsDropdown; diff --git a/app/ui/components/request-pane.js b/app/ui/components/request-pane.js index 2c9388add9..d55675d41a 100644 --- a/app/ui/components/request-pane.js +++ b/app/ui/components/request-pane.js @@ -316,8 +316,9 @@ class RequestPane extends React.PureComponent { - + { - + {request.description ? (