import React, { Fragment } from 'react'; import { set } from 'react-redux-values'; import { Margin } from 'styled-components-spacing'; import { compose, graphql } from 'react-apollo'; import ReduxForm from 'declarative-redux-form'; import { connect } from 'react-redux'; import get from 'lodash.get'; import forceArray from 'force-array'; import { NetworkIcon, P, Button, H3, StatusLoader } from 'joyent-ui-toolkit'; import Title from '@components/create-instance/title'; import Network from '@components/create-instance/network'; import ListNetworks from '@graphql/list-networks.gql'; const FORM_NAME = 'CREATE-INSTANCE-NETWORKS'; export const Networks = ({ networks = [], expanded = false, proceeded = false, loading = false, setInfoExpanded, handleNext, handleEdit }) => { const selected = networks.filter(({ selected }) => selected); return ( }>Networks {expanded ? (

Instances are automatically connected to a private fabric network, which is the best choice for internal communication within your application. Data center networks are the best choice for exposing your application to the public internet (if the data center network is a public network).{' '} Read more

) : null} {proceeded && !expanded ? (

{selected.length} network{selected.length === 1 ? '' : 's'} added

) : null} {loading && expanded ? : null} {!loading ? ( {props => (
{networks.map( ({ id, selected, infoExpanded, ...network }) => !expanded && !selected ? null : ( setInfoExpanded(id, !infoExpanded)} {...network} /> ) )} )}
) : null} {expanded ? ( ) : proceeded ? ( ) : null}
); }; export default compose( graphql(ListNetworks, { props: ({ data }) => { const { networks = [], loading = false, error = null, refetch } = data; return { networks: forceArray(networks), loading, error, refetch }; } }), connect( ({ values, form }, { networks }) => { const selected = get(form, `${FORM_NAME}.values`, {}); return { proceeded: get(values, 'create-instance-networks-proceeded', false), networks: networks.map(({ id, ...network }) => ({ ...network, selected: Boolean(selected[id]), infoExpanded: get( values, `create-instance-networks-${id}-expanded`, false ), id })) }; }, (dispatch, { history }) => ({ handleNext: () => { dispatch( set({ name: 'create-instance-networks-proceeded', value: true }) ); return history.push('/instances/~create/firewall'); }, handleEdit: () => { return history.push('/instances/~create/networks'); }, setInfoExpanded: (id, expanded) => { return dispatch( set({ name: `create-instance-networks-${id}-expanded`, value: expanded }) ); } }) ) )(Networks);