2018-01-19 19:37:31 +02:00
|
|
|
import React, { Fragment } from 'react';
|
|
|
|
import { compose } from 'react-apollo';
|
|
|
|
import { set } from 'react-redux-values';
|
|
|
|
import ReduxForm from 'declarative-redux-form';
|
|
|
|
import { Margin } from 'styled-components-spacing';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import get from 'lodash.get';
|
2018-03-01 15:47:30 +02:00
|
|
|
import Flex from 'styled-flex-component';
|
2018-01-19 19:37:31 +02:00
|
|
|
|
2018-03-01 15:47:30 +02:00
|
|
|
import { ScriptIcon, Button } from 'joyent-ui-toolkit';
|
2018-01-19 19:37:31 +02:00
|
|
|
|
|
|
|
import Title from '@components/create-instance/title';
|
2018-01-30 18:04:03 +02:00
|
|
|
import Description from '@components/description';
|
2018-03-01 16:32:55 +02:00
|
|
|
import UserScriptForm, {
|
|
|
|
Overview
|
|
|
|
} from '@components/create-instance/user-script';
|
|
|
|
import { Forms, Values } from '@root/constants';
|
2018-01-19 19:37:31 +02:00
|
|
|
|
2018-03-01 16:32:55 +02:00
|
|
|
const { IC_US_F } = Forms;
|
|
|
|
const { IC_US_V_PROCEEDED, IC_US_V_OPEN } = Values;
|
2018-01-19 19:37:31 +02:00
|
|
|
|
|
|
|
export const UserScript = ({
|
|
|
|
expanded,
|
|
|
|
proceeded,
|
|
|
|
create,
|
|
|
|
edit,
|
|
|
|
formOpen,
|
|
|
|
script = {},
|
2018-03-01 15:47:30 +02:00
|
|
|
lines,
|
2018-01-19 19:37:31 +02:00
|
|
|
handleChangeOpenForm,
|
|
|
|
handleSubmit,
|
2018-01-30 18:27:01 +02:00
|
|
|
handleEdit,
|
|
|
|
step
|
2018-01-19 19:37:31 +02:00
|
|
|
}) => (
|
|
|
|
<Fragment>
|
2018-01-30 18:04:03 +02:00
|
|
|
<Title
|
|
|
|
id={step}
|
|
|
|
onClick={!proceeded && handleEdit}
|
|
|
|
icon={<ScriptIcon />}
|
|
|
|
collapsed={!expanded && !proceeded}
|
|
|
|
>
|
2018-01-19 19:37:31 +02:00
|
|
|
User Script
|
|
|
|
</Title>
|
|
|
|
{expanded ? (
|
2018-03-01 15:47:30 +02:00
|
|
|
<Fragment>
|
|
|
|
<Description>
|
|
|
|
User script can be used to inject a custom boot script.
|
|
|
|
</Description>
|
|
|
|
{formOpen ? (
|
|
|
|
<ReduxForm
|
2018-03-01 16:32:55 +02:00
|
|
|
form={IC_US_F}
|
2018-03-01 15:47:30 +02:00
|
|
|
destroyOnUnmount={false}
|
|
|
|
forceUnregisterOnUnmount={true}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
|
|
|
{props => <UserScriptForm {...props} />}
|
|
|
|
</ReduxForm>
|
|
|
|
) : null}
|
|
|
|
</Fragment>
|
2018-01-19 19:37:31 +02:00
|
|
|
) : null}
|
2018-01-30 18:04:03 +02:00
|
|
|
{expanded ? (
|
|
|
|
<Margin top={formOpen || script.value ? 4 : 2} bottom={7}>
|
|
|
|
{script.value || formOpen ? null : (
|
2018-03-01 15:47:30 +02:00
|
|
|
<Flex alignCenter>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={() => handleChangeOpenForm(true)}
|
|
|
|
secondary
|
|
|
|
>
|
|
|
|
Add User Script
|
|
|
|
</Button>
|
|
|
|
<Button type="submit" onClick={handleSubmit}>
|
|
|
|
Next
|
|
|
|
</Button>
|
|
|
|
</Flex>
|
2018-01-30 18:04:03 +02:00
|
|
|
)}
|
|
|
|
</Margin>
|
|
|
|
) : proceeded ? (
|
2018-03-01 15:47:30 +02:00
|
|
|
<Fragment>
|
|
|
|
<Overview script={script} lines={lines} />
|
|
|
|
<Margin top={4} bottom={7}>
|
|
|
|
<Button type="button" onClick={handleEdit} secondary>
|
|
|
|
Edit
|
|
|
|
</Button>
|
|
|
|
</Margin>
|
|
|
|
</Fragment>
|
2018-01-30 18:04:03 +02:00
|
|
|
) : null}
|
2018-01-19 19:37:31 +02:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
connect(
|
2018-03-01 15:47:30 +02:00
|
|
|
({ values, form }, ownProps) => {
|
2018-03-01 16:32:55 +02:00
|
|
|
const formOpen = get(values, IC_US_V_OPEN, false);
|
|
|
|
const script = get(form, `${IC_US_F}.values.value`, '');
|
2018-03-01 15:47:30 +02:00
|
|
|
const lines = script.split('\n').length;
|
2018-02-21 18:22:30 +02:00
|
|
|
|
2018-03-01 16:32:55 +02:00
|
|
|
const proceeded = get(values, IC_US_V_PROCEEDED, false);
|
2018-01-19 19:37:31 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
script,
|
2018-03-01 15:47:30 +02:00
|
|
|
lines,
|
2018-02-21 18:22:30 +02:00
|
|
|
proceeded: proceeded || script.value,
|
2018-01-19 19:37:31 +02:00
|
|
|
create: !script.value,
|
|
|
|
edit: script.value,
|
|
|
|
formOpen
|
|
|
|
};
|
|
|
|
},
|
|
|
|
(dispatch, { history }) => ({
|
2018-02-27 17:08:33 +02:00
|
|
|
handleEdit: () => {
|
2018-03-01 15:47:30 +02:00
|
|
|
dispatch([
|
2018-03-01 16:32:55 +02:00
|
|
|
set({ name: IC_US_V_PROCEEDED, value: true }),
|
|
|
|
set({ name: IC_US_V_OPEN, value: true })
|
2018-03-01 15:47:30 +02:00
|
|
|
]);
|
2018-02-27 17:08:33 +02:00
|
|
|
|
|
|
|
return history.push(`/~create/user-script${history.location.search}`);
|
|
|
|
},
|
2018-01-19 19:37:31 +02:00
|
|
|
handleChangeOpenForm: value => {
|
2018-03-01 16:32:55 +02:00
|
|
|
return dispatch([set({ name: IC_US_V_OPEN, value })]);
|
2018-01-19 19:37:31 +02:00
|
|
|
},
|
|
|
|
handleSubmit: value => {
|
2018-03-01 15:47:30 +02:00
|
|
|
dispatch([
|
2018-03-01 16:32:55 +02:00
|
|
|
set({ name: IC_US_V_OPEN, value: false }),
|
|
|
|
set({ name: IC_US_V_PROCEEDED, value: true })
|
2018-01-19 19:37:31 +02:00
|
|
|
]);
|
2018-03-01 15:47:30 +02:00
|
|
|
|
|
|
|
return history.push(`/~create/networks${history.location.search}`);
|
2018-01-19 19:37:31 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)(UserScript);
|