import React, { Fragment } from 'react'; import { compose, graphql } from 'react-apollo'; import { set } from 'react-redux-values'; import ReduxForm from 'declarative-redux-form'; import { Margin } from 'styled-components-spacing'; import { change } from 'redux-form'; import { connect } from 'react-redux'; import intercept from 'apr-intercept'; import get from 'lodash.get'; import { NameIcon, H3, Button } from 'joyent-ui-toolkit'; import Title from '@components/create-instance/title'; import Name from '@components/create-instance/name'; import Description from '@components/description'; import GetRandomName from '@graphql/get-random-name.gql'; import { instanceName as validateName } from '@state/validators'; import createClient from '@state/apollo-client'; import { Forms, Values } from '@root/constants'; const { IC_NAME_F } = Forms; const { IC_NAME_V_PROCEEDED, IC_NAME_V_RANDOMIZING } = Values; const NameContainer = ({ invalid, expanded, proceeded, name, placeholderName, randomizing, handleAsyncValidate, shouldAsyncValidate, handleNext, handleRandomize, handleEdit, step }) => ( } invalid={!expanded && invalid} > Instance name {expanded ? ( Your instance name will be used to identify this specific instance. ) : null} {props => expanded ? ( ) : name ? (

{name}

) : null }
{expanded ? ( ) : proceeded ? ( ) : null}
); export default compose( graphql(GetRandomName, { options: () => ({ ssr: false }), props: ({ data }) => ({ placeholderName: data.rndName || '' }) }), connect( ({ form, values }, ownProps) => { const name = get(form, `${IC_NAME_F}.values.name`, ''); const invalid = get(form, `${IC_NAME_F}.asyncErrors.name`, null); const randomizing = get(values, IC_NAME_V_RANDOMIZING, false); const proceeded = get(values, IC_NAME_V_PROCEEDED, false); return { invalid, proceeded: proceeded || name.length, randomizing, name }; }, (dispatch, { history, query }) => ({ handleNext: () => { dispatch(set({ name: IC_NAME_V_PROCEEDED, value: true })); return history.push( `/instances/~create/${query.image ? 'package' : 'image'}${ history.location.search }` ); }, handleEdit: () => { history.push(`/instances/~create/name${history.location.search}`); }, shouldAsyncValidate: ({ trigger }) => { return trigger === 'change'; }, handleAsyncValidate: validateName, handleRandomize: async () => { dispatch(set({ name: IC_NAME_V_RANDOMIZING, value: true })); const [err, res] = await intercept( createClient().query({ fetchPolicy: 'network-only', query: GetRandomName }) ); dispatch(set({ name: IC_NAME_V_RANDOMIZING, value: false })); if (err) { console.error(err); return; } const { data } = res; const { rndName } = data; return dispatch(change(IC_NAME_F, 'name', rndName)); } }) ) )(NameContainer);