joyent-portal/consoles/my-joy-images/src/containers/list.js

198 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-02-05 17:12:47 +02:00
import React, { Fragment } from 'react';
import { compose, graphql } from 'react-apollo';
import ReduxForm from 'declarative-redux-form';
import { Margin } from 'styled-components-spacing';
2018-02-06 12:32:47 +02:00
import { Row, Col } from 'joyent-react-styled-flexboxgrid';
import { connect } from 'react-redux';
import get from 'lodash.get';
import intercept from 'apr-intercept';
import { set } from 'react-redux-values';
import Fuse from 'fuse.js';
2018-02-05 17:12:47 +02:00
import {
ViewContainer,
Divider,
StatusLoader,
Message,
MessageTitle,
MessageDescription
} from 'joyent-ui-toolkit';
2018-03-06 03:14:33 +02:00
import GLOBAL from '@state/global';
2018-02-05 17:12:47 +02:00
import ToolbarForm from '@components/toolbar';
2018-02-06 12:32:47 +02:00
import Empty from '@components/empty';
2018-02-14 22:30:25 +02:00
import { ImageType, Forms } from '@root/constants';
2018-02-05 17:12:47 +02:00
import ListImages from '@graphql/list-images.gql';
2018-02-06 12:32:47 +02:00
import { Image, Filters } from '@components/image';
import RemoveImage from '@graphql/remove-image.gql';
import parseError from '@state/parse-error';
2018-02-06 12:32:47 +02:00
2018-02-14 22:30:25 +02:00
const { LIST_TOOLBAR_FORM, LIST_TOGGLE_TYPE_FORM } = Forms;
2018-02-05 17:12:47 +02:00
2018-02-06 12:32:47 +02:00
export const List = ({
images = [],
allImages = [],
loading = false,
error = null,
history,
typeValue,
handleCreateInstance,
handleRemove
2018-02-06 12:32:47 +02:00
}) => (
2018-02-05 17:12:47 +02:00
<ViewContainer main>
2018-04-06 17:53:44 +03:00
<Margin top="4">
<ReduxForm form={LIST_TOOLBAR_FORM}>
{props => <ToolbarForm {...props} actionable={!loading} />}
</ReduxForm>
</Margin>
2018-04-06 17:53:44 +03:00
<Margin vertical="4">
<Divider />
</Margin>
2018-02-05 17:12:47 +02:00
{loading && !images.length ? (
<Fragment>
<StatusLoader />
</Fragment>
) : null}
{error && !images.length && !loading ? (
2018-04-06 17:53:44 +03:00
<Margin bottom="5">
2018-02-05 17:12:47 +02:00
<Message error>
<MessageTitle>Ooops!</MessageTitle>
<MessageDescription>
2018-02-13 22:03:57 +02:00
An error occurred while loading your images
2018-02-05 17:12:47 +02:00
</MessageDescription>
</Message>
</Margin>
) : null}
2018-02-06 12:32:47 +02:00
<Fragment>
2018-04-06 17:53:44 +03:00
<Margin bottom="4">
2018-02-06 12:32:47 +02:00
<ReduxForm
2018-02-14 22:30:25 +02:00
form={LIST_TOGGLE_TYPE_FORM}
2018-02-06 12:32:47 +02:00
initialValues={{ 'image-type': 'all' }}
>
{props =>
allImages.length ? (
<Filters selected={typeValue} {...props} />
) : null
}
2018-02-06 12:32:47 +02:00
</ReduxForm>
</Margin>
<Row>
{images.map(image => (
2018-05-23 19:29:04 +03:00
<Col sm="4">
<Image
{...image}
onCreateInstance={() => handleCreateInstance(image)}
onRemove={() => handleRemove(image.id)}
/>
2018-02-06 12:32:47 +02:00
</Col>
))}
{!images.length && !loading ? (
<Empty>No images to see here</Empty>
) : null}
</Row>
</Fragment>
2018-02-05 17:12:47 +02:00
</ViewContainer>
);
export default compose(
2018-02-14 22:30:25 +02:00
graphql(RemoveImage, {
name: 'removeImage'
}),
2018-02-05 17:12:47 +02:00
graphql(ListImages, {
options: () => ({
ssr: false,
pollInterval: 1000
}),
props: ({ data: { images = [], loading, error, refetch } }) => ({
images: images || [],
index: new Fuse(images || [], {
keys: ['name', 'os', 'version', 'state', 'type']
}),
2018-02-14 22:30:25 +02:00
loading,
error
})
2018-02-06 12:32:47 +02:00
}),
connect(
({ form, values }, { index, error, images = [] }) => {
2018-02-14 22:30:25 +02:00
const filter = get(form, `${LIST_TOOLBAR_FORM}.values.filter`, false);
const mutationError = get(values, 'remove-mutation-error', null);
2018-02-06 12:32:47 +02:00
const typeValue = get(
form,
2018-02-14 22:30:25 +02:00
`${LIST_TOGGLE_TYPE_FORM}.values.image-type`,
'all'
);
2018-02-06 12:32:47 +02:00
const virtual = Object.keys(ImageType).filter(
i => ImageType[i] === 'Hardware Virtual Machine'
);
2018-02-06 12:32:47 +02:00
const container = Object.keys(ImageType).filter(
i => ImageType[i] === 'Infrastructure Container'
);
const filtered = filter ? index.search(filter) : images;
return {
images: filtered
.filter(image => {
switch (typeValue) {
case 'all':
return true;
case 'hardware-virtual-machine':
return virtual.includes(image.type);
case 'infrastructure-container':
return container.includes(image.type);
default:
return true;
}
})
.map(({ id, ...image }) => ({
...image,
id,
removing: get(values, `remove-mutation-${id}-loading`, false)
})),
allImages: images,
mutationError,
typeValue
};
},
(dispatch, { removeImage, history }) => ({
2018-03-06 03:14:33 +02:00
handleCreateInstance: image => {
return window
2018-03-06 03:47:30 +02:00
.open(
`${GLOBAL.origin}/instances/~create/?image=${image.name}`,
'_blank'
)
2018-03-06 03:14:33 +02:00
.focus();
},
handleRemove: async id => {
dispatch([set({ name: `remove-mutation-${id}-loading`, value: true })]);
const [err, res] = await intercept(
removeImage({
variables: {
id
}
})
);
if (err) {
dispatch([
set({ name: 'remove-mutation-error', value: parseError(err) }),
set({ name: `remove-mutation-${id}-loading`, value: false })
]);
}
if (res) {
2018-02-14 22:30:25 +02:00
dispatch(
set({ name: `remove-mutation-${id}-loading`, value: false })
2018-02-14 22:30:25 +02:00
);
2018-03-06 03:14:33 +02:00
history.push('/images');
2018-02-06 12:32:47 +02:00
}
}
})
)
2018-02-05 17:12:47 +02:00
)(List);