import React from 'react'; import { reduxForm, SubmissionError } from 'redux-form'; import { connect } from 'react-redux'; import { compose, graphql } from 'react-apollo'; import find from 'lodash.find'; import get from 'lodash.get'; import { Title, ViewContainer, StatusLoader, Message, MessageTitle, MessageDescription } from 'joyent-ui-toolkit'; import { CreateSnapshot as InstanceCreateSnapshot } from '@components/instances'; import CreateSnapshotMutation from '@graphql/create-snapshot.gql'; import GetInstance from '@graphql/get-instance.gql'; const CreateSnapshot = ({ match, instance, loading, error, handleSubmit, handleCancel }) => { const _title = Create Snapshot; const _loading = !(loading && !instance) ? null : ; const CreateSnapshotForm = reduxForm({ form: `create-snapshot-${match.params.instance}` })(InstanceCreateSnapshot); const _error = error && !instance && !_loading && ( Ooops! An error occurred while loading your instance ); const _form = !loading && !_error && ( ); return ( {_title} {_loading} {_error} {_form} ); }; export default compose( graphql(CreateSnapshotMutation, { name: 'createSnapshot' }), graphql(GetInstance, { options: ({ match }) => ({ variables: { name: get(match, 'params.instance') } }), props: ({ data: { loading, error, variables, ...rest } }) => ({ instance: find(get(rest, 'machines', []), ['name', variables.name]), loading, error }) }), connect( null, (dispatch, { history, location, instance, createSnapshot }) => ({ handleCancel: () => history.push(location.pathname.split(/\/~/).shift()), handleSubmit: ({ name }) => createSnapshot({ variables: { name, id: instance.id } }) .catch(error => { throw new SubmissionError({ _error: error.graphQLErrors .map(({ message }) => message) .join('\n') }); }) .then(() => history.push(`/instances/${instance.name}/snapshots`)) }) ) )(CreateSnapshot);