From 147c3cd7d96b1db76cd769e64d7bd47a45457eb5 Mon Sep 17 00:00:00 2001 From: Iris Scholten Date: Mon, 8 Apr 2019 16:29:35 -0700 Subject: [PATCH] feat(ui): Create new bucket when creating new org --- ui/src/organizations/actions/orgs.ts | 38 +++++++ .../components/CreateOrgOverlay.tsx | 102 +++++++++++++----- 2 files changed, 114 insertions(+), 26 deletions(-) diff --git a/ui/src/organizations/actions/orgs.ts b/ui/src/organizations/actions/orgs.ts index 262d90f6ec..c5cc5be902 100644 --- a/ui/src/organizations/actions/orgs.ts +++ b/ui/src/organizations/actions/orgs.ts @@ -13,9 +13,12 @@ import {defaultTemplates} from 'src/templates/constants/' import { orgCreateSuccess, orgCreateFailed, + bucketCreateSuccess, + bucketCreateFailed, } from 'src/shared/copy/v2/notifications' // Types +import {Bucket} from '@influxdata/influx' import {Organization, RemoteDataState} from 'src/types' import {PublishNotificationAction} from 'src/types/actions/notifications' @@ -135,6 +138,41 @@ export const getOrganizations = () => async ( } } +export const createOrgWithBucket = ( + org: Organization, + bucket: Bucket +) => async ( + dispatch: Dispatch +) => { + let createdOrg: Organization + + try { + createdOrg = await client.organizations.create(org) + await client.templates.create({ + ...defaultTemplates.systemTemplate(), + orgID: createdOrg.id, + }) + dispatch(notify(orgCreateSuccess())) + + dispatch(addOrg(createdOrg)) + dispatch(push(`/orgs/${createdOrg.id}`)) + + await client.buckets.create({ + ...bucket, + organizationID: createdOrg.id, + }) + + dispatch(notify(bucketCreateSuccess())) + } catch (e) { + console.error(e) + + if (!createdOrg) { + dispatch(notify(orgCreateFailed())) + } + dispatch(notify(bucketCreateFailed())) + } +} + export const createOrg = (org: Organization) => async ( dispatch: Dispatch ): Promise => { diff --git a/ui/src/organizations/components/CreateOrgOverlay.tsx b/ui/src/organizations/components/CreateOrgOverlay.tsx index e915c0442e..806fc70e69 100644 --- a/ui/src/organizations/components/CreateOrgOverlay.tsx +++ b/ui/src/organizations/components/CreateOrgOverlay.tsx @@ -10,7 +10,7 @@ import {Form, Input, Button} from '@influxdata/clockface' import {Overlay} from 'src/clockface' // Types -import {Organization} from '@influxdata/influx' +import {Organization, Bucket} from '@influxdata/influx' import { ButtonType, ComponentColor, @@ -18,20 +18,23 @@ import { } from '@influxdata/clockface' // Actions -import {createOrg} from 'src/organizations/actions/orgs' +import {createOrgWithBucket} from 'src/organizations/actions/orgs' interface OwnProps {} interface DispatchProps { - createOrg: typeof createOrg + createOrgWithBucket: typeof createOrgWithBucket } type Props = OwnProps & DispatchProps & WithRouterProps interface State { org: Organization - nameInputStatus: ComponentStatus - errorMessage: string + bucket: Bucket + orgNameInputStatus: ComponentStatus + bucketNameInputStatus: ComponentStatus + orgErrorMessage: string + bucketErrorMessage: string } class CreateOrgOverlay extends PureComponent { @@ -39,13 +42,23 @@ class CreateOrgOverlay extends PureComponent { super(props) this.state = { org: {name: ''}, - nameInputStatus: ComponentStatus.Default, - errorMessage: '', + bucket: {name: '', retentionRules: []}, + orgNameInputStatus: ComponentStatus.Default, + bucketNameInputStatus: ComponentStatus.Default, + orgErrorMessage: '', + bucketErrorMessage: '', } } public render() { - const {org, nameInputStatus, errorMessage} = this.state + const { + org, + orgNameInputStatus, + orgErrorMessage, + bucket, + bucketNameInputStatus, + bucketErrorMessage, + } = this.state return ( @@ -56,14 +69,31 @@ class CreateOrgOverlay extends PureComponent { />
- + + + + @@ -85,9 +115,9 @@ class CreateOrgOverlay extends PureComponent { } private get submitButtonStatus(): ComponentStatus { - const {org} = this.state + const {org, bucket} = this.state - if (org.name) { + if (org.name && bucket.name) { return ComponentStatus.Default } @@ -95,17 +125,17 @@ class CreateOrgOverlay extends PureComponent { } private handleCreateOrg = async () => { - const {org} = this.state - const {createOrg} = this.props + const {org, bucket} = this.state + const {createOrgWithBucket} = this.props - await createOrg(org) + await createOrgWithBucket(org, bucket) } private closeModal = () => { this.props.router.goBack() } - private handleChangeInput = (e: ChangeEvent) => { + private handleChangeOrgInput = (e: ChangeEvent) => { const value = e.target.value const key = e.target.name const org = {...this.state.org, [key]: value} @@ -113,24 +143,44 @@ class CreateOrgOverlay extends PureComponent { if (!value) { return this.setState({ org, - nameInputStatus: ComponentStatus.Error, - errorMessage: this.randomErrorMessage(key), + orgNameInputStatus: ComponentStatus.Error, + orgErrorMessage: this.randomErrorMessage(key, 'organization'), }) } this.setState({ org, - nameInputStatus: ComponentStatus.Valid, - errorMessage: '', + orgNameInputStatus: ComponentStatus.Valid, + orgErrorMessage: '', }) } - private randomErrorMessage = (key: string): string => { + private handleChangeBucketInput = (e: ChangeEvent) => { + const value = e.target.value + const key = e.target.name + const bucket = {...this.state.bucket, [key]: value} + + if (!value) { + return this.setState({ + bucket, + bucketNameInputStatus: ComponentStatus.Error, + bucketErrorMessage: this.randomErrorMessage(key, 'bucket'), + }) + } + + this.setState({ + bucket, + bucketNameInputStatus: ComponentStatus.Valid, + bucketErrorMessage: '', + }) + } + + private randomErrorMessage = (key: string, resource: string): string => { const messages = [ - `Imagine that! An organization without a ${key}`, - `An organization needs a ${key}`, + `Imagine that! ${_.startCase(resource)} without a ${key}`, + `${_.startCase(resource)} needs a ${key}`, `You're not getting far without a ${key}`, - `The organization formerly known as...`, + `The ${resource} formerly known as...`, `Pick a ${key}, any ${key}`, `Any ${key} will do`, ] @@ -140,7 +190,7 @@ class CreateOrgOverlay extends PureComponent { } const mdtp = { - createOrg, + createOrgWithBucket, } export default withRouter(