import React, { Fragment } from 'react'; import { set } from 'react-redux-values'; import { Margin } from 'styled-components-spacing'; import { compose } from 'react-apollo'; import { destroy, reset } from 'redux-form'; import ReduxForm from 'declarative-redux-form'; import { connect } from 'react-redux'; import get from 'lodash.get'; import remcalc from 'remcalc'; import { AffinityIcon, Button, Divider, KeyValue } from 'joyent-ui-toolkit'; import Title from '@components/create-instance/title'; import { Rule, Header } from '@components/create-instance/affinity'; import Description from '@components/description'; import { addAffinityRule as validateRule } from '@state/validators'; const FORM_NAME_CREATE = 'CREATE-INSTANCE-AFFINITY-ADD'; const FORM_NAME_EDIT = 'CREATE-INSTANCE-AFFINITY-EDIT'; const RULE_DEFAULTS = { conditional: 'should', placement: 'same', type: 'name', pattern: 'equalling', key: '', value: '' }; export const Affinity = ({ step, expanded, addOpen, editOpen, editingRule, creatingRule, exitingRule, shouldAsyncValidate, handleAsyncValidate, handleCreateAffinityRules, handleRemoveAffinityRule, handleUpdateAffinityRule, handleToggleExpanded, handleCancelEdit, handleChangeAddOpen, handleEdit }) => ( } > Affinity {expanded ? ( Control placement of instances on the physical servers. Design applications to adapt at failure by distributing application components. Instances are only provisioned when the exact criteria is met.{' '} Read the docs ) : null} {formProps => exitingRule ? ( } method="edit" input={inputProps => ( )} type="an affinity rule" onToggleExpanded={() => handleToggleExpanded(!exitingRule.expanded) } onCancel={handleCancelEdit} onRemove={handleRemoveAffinityRule} /> ) : null } {formProps => expanded && addOpen ? ( ( )} type="an affinity rule" expanded noRemove onCancel={() => handleChangeAddOpen(false)} /> ) : null } {expanded ? ( {!addOpen && !exitingRule ? ( ) : null} ) : exitingRule ? ( ) : null} {expanded ? : null} ); export default compose( connect(({ values, form }, ownProps) => { const editingRule = get(form, `${FORM_NAME_EDIT}.values`, null); const creatingRule = get(form, `${FORM_NAME_CREATE}.values`, null); const exitingRule = get(values, 'create-instance-affinity', null); const addOpen = get(values, 'create-instance-affinity-add-open', false); const editOpen = get(values, 'create-instance-affinity-edit-open', false); return { addOpen, editOpen, creatingRule, editingRule, exitingRule }; }), connect(null, (dispatch, { history }) => ({ shouldAsyncValidate: ({ trigger }) => { return trigger === 'submit'; }, handleAsyncValidate: validateRule, handleEdit: () => { return history.push(`/~create/affinity${history.location.search}`); }, handleCreateAffinityRules: value => { const toggleToClosed = set({ name: 'create-instance-affinity-add-open', value: false }); const appendAffinityRule = set({ name: 'create-instance-affinity', value }); return dispatch([ destroy(FORM_NAME_CREATE), toggleToClosed, appendAffinityRule ]); }, handleUpdateAffinityRule: value => { return dispatch([ destroy(FORM_NAME_EDIT), set({ name: 'create-instance-affinity', value }) ]); }, handleChangeAddOpen: value => { return dispatch([ reset(FORM_NAME_CREATE), set({ name: 'create-instance-affinity-add-open', value }) ]); }, handleToggleExpanded: value => { return dispatch( set({ name: 'create-instance-affinity-edit-open', value }) ); }, handleCancelEdit: () => { return dispatch([ set({ name: 'create-instance-affinity-edit-open', value: false }) ]); }, handleRemoveAffinityRule: () => { return dispatch([ destroy(FORM_NAME_EDIT), set({ name: 'create-instance-affinity', value: null }) ]); } })) )(Affinity);