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 { destroy } from 'redux-form';
|
|
|
|
import { Margin } from 'styled-components-spacing';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import get from 'lodash.get';
|
|
|
|
|
2018-02-05 17:12:47 +02:00
|
|
|
import { ScriptIcon, Button, KeyValue } from 'joyent-ui-toolkit';
|
|
|
|
import Editor from 'joyent-ui-toolkit/dist/es/editor';
|
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-01-19 19:37:31 +02:00
|
|
|
|
|
|
|
const FORM_NAME = 'create-instance-user-script';
|
|
|
|
|
|
|
|
export const UserScript = ({
|
|
|
|
expanded,
|
|
|
|
proceeded,
|
|
|
|
create,
|
|
|
|
edit,
|
|
|
|
formOpen,
|
|
|
|
script = {},
|
|
|
|
handleChangeOpenForm,
|
|
|
|
handleSubmit,
|
|
|
|
handleRemove,
|
|
|
|
handleNext,
|
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 ? (
|
|
|
|
<Description>
|
|
|
|
User script can be used to inject a custom boot script.
|
|
|
|
</Description>
|
|
|
|
) : null}
|
|
|
|
<ReduxForm
|
|
|
|
form={FORM_NAME}
|
|
|
|
destroyOnUnmount={false}
|
|
|
|
forceUnregisterOnUnmount={true}
|
|
|
|
initialValues={script}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
|
|
|
{props =>
|
|
|
|
!formOpen && create ? null : (
|
|
|
|
<KeyValue
|
|
|
|
{...props}
|
|
|
|
expanded={formOpen}
|
|
|
|
method={edit ? 'edit' : 'add'}
|
|
|
|
input="textarea"
|
|
|
|
type="user script"
|
|
|
|
onToggleExpanded={() => handleChangeOpenForm(!formOpen)}
|
|
|
|
onCancel={() => handleChangeOpenForm(false)}
|
|
|
|
onRemove={handleRemove}
|
2018-02-05 17:12:47 +02:00
|
|
|
editor={Editor}
|
2018-01-19 19:37:31 +02:00
|
|
|
onlyValue
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</ReduxForm>
|
2018-01-30 18:04:03 +02:00
|
|
|
{expanded ? (
|
|
|
|
<Margin top={formOpen || script.value ? 4 : 2} bottom={7}>
|
|
|
|
{script.value || formOpen ? null : (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={() => handleChangeOpenForm(true)}
|
|
|
|
secondary
|
|
|
|
>
|
|
|
|
Add User Script
|
2018-01-19 19:37:31 +02:00
|
|
|
</Button>
|
2018-01-30 18:04:03 +02:00
|
|
|
)}
|
|
|
|
<Button type="submit" onClick={handleNext}>
|
|
|
|
Next
|
|
|
|
</Button>
|
|
|
|
</Margin>
|
|
|
|
) : proceeded ? (
|
|
|
|
<Margin top={4} bottom={7}>
|
|
|
|
<Button type="button" onClick={handleEdit} secondary>
|
|
|
|
Edit
|
|
|
|
</Button>
|
|
|
|
</Margin>
|
|
|
|
) : null}
|
2018-01-19 19:37:31 +02:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
connect(
|
|
|
|
({ values }, ownProps) => {
|
2018-02-21 18:22:30 +02:00
|
|
|
const formOpen = get(values, 'create-instance-user-script-open', false);
|
|
|
|
|
2018-01-19 19:37:31 +02:00
|
|
|
const script = get(values, 'create-instance-user-script', {
|
|
|
|
name: 'user-script'
|
|
|
|
});
|
2018-02-21 18:22:30 +02:00
|
|
|
|
2018-01-19 19:37:31 +02:00
|
|
|
const proceeded = get(
|
|
|
|
values,
|
|
|
|
'create-instance-user-script-proceeded',
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
script,
|
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 }) => ({
|
|
|
|
handleNext: () => {
|
|
|
|
dispatch(
|
|
|
|
set({ name: 'create-instance-user-script-proceeded', value: true })
|
|
|
|
);
|
|
|
|
|
2018-02-15 16:37:59 +02:00
|
|
|
return history.push(`/~create/networks${history.location.search}`);
|
2018-01-19 19:37:31 +02:00
|
|
|
},
|
2018-02-27 17:08:33 +02:00
|
|
|
handleEdit: () => {
|
|
|
|
dispatch(
|
|
|
|
set({ name: 'create-instance-user-script-proceeded', value: true })
|
|
|
|
);
|
|
|
|
|
|
|
|
return history.push(`/~create/user-script${history.location.search}`);
|
|
|
|
},
|
2018-01-19 19:37:31 +02:00
|
|
|
handleChangeOpenForm: value => {
|
|
|
|
return dispatch([
|
|
|
|
set({ name: `create-instance-user-script-open`, value })
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
handleSubmit: value => {
|
|
|
|
return dispatch([
|
|
|
|
set({ name: `create-instance-user-script`, value: { ...value } }),
|
|
|
|
set({ name: `create-instance-user-script-open`, value: false })
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
handleRemove: () => {
|
|
|
|
return dispatch([
|
|
|
|
destroy(FORM_NAME),
|
|
|
|
set({
|
|
|
|
name: `create-instance-user-script`,
|
|
|
|
value: { name: 'user-script' }
|
|
|
|
}),
|
|
|
|
set({ name: `create-instance-user-script-open`, value: false })
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)(UserScript);
|