2018-01-25 20:14:11 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { compose, graphql } from 'react-apollo';
|
|
|
|
import { Margin } from 'styled-components-spacing';
|
|
|
|
import find from 'lodash.find';
|
|
|
|
import get from 'lodash.get';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ViewContainer,
|
|
|
|
Message,
|
|
|
|
MessageTitle,
|
|
|
|
MessageDescription,
|
|
|
|
StatusLoader
|
|
|
|
} from 'joyent-ui-toolkit';
|
|
|
|
|
|
|
|
import Editor from 'joyent-ui-toolkit/dist/es/editor';
|
|
|
|
import Description from '@components/description';
|
|
|
|
import Empty from '@components/empty';
|
|
|
|
import GetMetadata from '@graphql/list-metadata.gql';
|
|
|
|
|
|
|
|
export const UserScript = ({ metadata, loading = false, error = null }) => (
|
|
|
|
<ViewContainer main>
|
|
|
|
<Margin bottom={1}>
|
|
|
|
<Description>
|
|
|
|
User script can be used to inject a custom boot script.
|
|
|
|
</Description>
|
|
|
|
</Margin>
|
|
|
|
{loading ? <StatusLoader /> : null}
|
|
|
|
{!loading && error ? (
|
2018-01-30 18:04:03 +02:00
|
|
|
<Margin bottom={4}>
|
|
|
|
<Message error>
|
|
|
|
<MessageTitle>Ooops!</MessageTitle>
|
|
|
|
<MessageDescription>
|
|
|
|
An error occurred while loading the instance user-script
|
|
|
|
</MessageDescription>
|
|
|
|
</Message>
|
|
|
|
</Margin>
|
2018-01-25 20:14:11 +02:00
|
|
|
) : null}
|
|
|
|
{!loading && metadata ? (
|
|
|
|
<Editor defaultValue={metadata.value} readOnly />
|
|
|
|
) : null}
|
|
|
|
{!loading && !error && !metadata ? (
|
2018-02-26 15:47:37 +02:00
|
|
|
<Empty borderTop>No User Script defined</Empty>
|
2018-01-25 20:14:11 +02:00
|
|
|
) : null}
|
|
|
|
</ViewContainer>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
graphql(GetMetadata, {
|
|
|
|
options: ({ match }) => ({
|
2018-02-20 02:35:31 +02:00
|
|
|
ssr: false,
|
2018-01-25 20:14:11 +02:00
|
|
|
variables: {
|
|
|
|
fetchPolicy: 'network-only',
|
|
|
|
name: get(match, 'params.instance')
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
props: ({ data }) => {
|
|
|
|
const { loading, error, variables, refetch, ...rest } = data;
|
|
|
|
const { name } = variables;
|
|
|
|
|
|
|
|
const instance = find(get(rest, 'machines', []), ['name', name]);
|
|
|
|
const metadata = get(instance, 'metadata', [])
|
|
|
|
.filter(({ name = '' }) => name === 'user-script')
|
|
|
|
.shift();
|
|
|
|
|
|
|
|
return {
|
|
|
|
metadata,
|
|
|
|
instance,
|
|
|
|
loading,
|
|
|
|
error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)(UserScript);
|