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

232 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-02-05 17:12:47 +02:00
import React from 'react';
import { compose, graphql } from 'react-apollo';
2018-02-14 22:30:25 +02:00
import { connect } from 'react-redux';
2018-02-05 17:12:47 +02:00
import { Margin } from 'styled-components-spacing';
import ReduxForm from 'declarative-redux-form';
2018-02-14 22:30:25 +02:00
import { destroy } from 'redux-form';
import { set } from 'react-redux-values';
import intercept from 'apr-intercept';
2018-02-05 17:12:47 +02:00
import get from 'lodash.get';
import remcalc from 'remcalc';
import Fuse from 'fuse.js';
2018-02-05 17:12:47 +02:00
import {
H3,
ViewContainer,
StatusLoader,
Divider,
Message,
MessageTitle,
MessageDescription,
TagList
} from 'joyent-ui-toolkit';
2018-02-14 22:30:25 +02:00
import { Forms } from '@root/constants';
import Tag, { AddForm } from '@components/tags';
2018-02-05 17:12:47 +02:00
import ToolbarForm from '@components/toolbar';
2018-02-14 22:30:25 +02:00
import UpdateImageTags from '@graphql/update-image-tags.gql';
2018-02-05 17:12:47 +02:00
import GetTags from '@graphql/get-tags.gql';
2018-03-01 03:15:16 +02:00
import { addTag as validateTag } from '@state/validators';
2018-02-14 22:30:25 +02:00
import parseError from '@state/parse-error';
2018-02-05 17:12:47 +02:00
2018-02-14 22:30:25 +02:00
const { TAGS_TOOLBAR_FORM, TAGS_ADD_FORM } = Forms;
2018-02-05 17:12:47 +02:00
2018-02-14 22:30:25 +02:00
export const Tags = ({
tags = [],
addOpen = false,
loading = false,
error = null,
mutationError = null,
mutating = false,
2018-03-01 03:15:16 +02:00
handleAsyncValidate,
shouldAsyncValidate,
2018-02-14 22:30:25 +02:00
handleToggleAddOpen,
handleRemoveTag,
handleAddTag
}) => (
2018-02-05 17:12:47 +02:00
<ViewContainer main>
2018-02-14 22:30:25 +02:00
<ReduxForm form={TAGS_TOOLBAR_FORM}>
2018-02-05 17:12:47 +02:00
{props => (
<Margin bottom="4">
<ToolbarForm
{...props}
searchable={!loading}
actionLabel="Add Tag"
2018-02-14 22:30:25 +02:00
actionable={!loading && !mutating && !addOpen}
onActionClick={handleToggleAddOpen}
2018-02-05 17:12:47 +02:00
action
/>
<Margin vertical="4">
<Divider height={remcalc(1)} />
</Margin>
2018-02-05 17:12:47 +02:00
</Margin>
)}
</ReduxForm>
{error && !loading && !tags.length ? (
<Margin bottom={5}>
2018-02-05 17:12:47 +02:00
<Message error>
<MessageTitle>Ooops!</MessageTitle>
<MessageDescription>
An error occurred while loading your instance tags
</MessageDescription>
</Message>
</Margin>
) : null}
2018-02-14 22:30:25 +02:00
{mutationError ? (
<Margin bottom={5}>
2018-02-14 22:30:25 +02:00
<Message error>
<MessageTitle>Ooops!</MessageTitle>
<MessageDescription>{mutationError}</MessageDescription>
</Message>
</Margin>
) : null}
2018-03-01 03:15:16 +02:00
<ReduxForm
form={TAGS_ADD_FORM}
shouldAsyncValidate={shouldAsyncValidate}
asyncValidate={handleAsyncValidate}
2018-03-01 03:15:16 +02:00
onSubmit={handleAddTag}
>
2018-02-14 22:30:25 +02:00
{props =>
addOpen ? (
<Margin bottom={5}>
2018-02-14 22:30:25 +02:00
<AddForm
{...props}
onToggleExpanded={() => handleToggleAddOpen(!addOpen)}
onCancel={() => handleToggleAddOpen(!addOpen)}
/>
</Margin>
) : null
}
</ReduxForm>
2018-02-05 17:12:47 +02:00
{!loading ? (
<Margin bottom={5}>
2018-02-05 17:12:47 +02:00
<H3>
{tags.length} tag{tags.length === 1 ? '' : 's'}
</H3>
</Margin>
) : null}
{loading && !tags.length ? <StatusLoader /> : null}
<TagList>
{tags.map(({ id, name, value }) => (
2018-02-14 22:30:25 +02:00
<Tag
key={id}
id={id}
2018-02-14 22:30:25 +02:00
name={name}
value={value}
onRemoveClick={!mutating && (() => handleRemoveTag(name))}
active
/>
2018-02-05 17:12:47 +02:00
))}
</TagList>
</ViewContainer>
);
export default compose(
2018-02-14 22:30:25 +02:00
graphql(UpdateImageTags, {
name: 'updateTags'
}),
2018-02-05 17:12:47 +02:00
graphql(GetTags, {
options: ({ match }) => ({
ssr: false,
2018-02-14 22:30:25 +02:00
fetchPolicy: 'network-only',
pollInterval: 1000,
2018-02-05 17:12:47 +02:00
variables: {
id: get(match, 'params.image')
2018-02-05 17:12:47 +02:00
}
}),
2018-02-14 22:30:25 +02:00
props: ({ data }) => {
const { loading = false, error = null, image, refetch } = data;
2018-02-14 22:30:25 +02:00
const tags = get(image || {}, 'tags', []);
const index = new Fuse(tags, {
keys: ['name', 'value']
});
2018-02-14 22:30:25 +02:00
return {
index,
2018-02-14 22:30:25 +02:00
image: image || {},
tags,
loading,
error,
refetch
};
}
}),
connect(
({ values, form }, { index, tags, image }) => {
const filter = get(form, `${TAGS_TOOLBAR_FORM}.values.filter`, false);
const filtered = filter ? index.search(filter) : tags;
return {
tags: filtered,
addOpen: get(values, `${image.id}-add-open`, false),
mutationError: get(values, `${image.id}-mutation-error`, false),
mutating: get(values, `${image.id}-mutating`, false)
};
},
2018-02-14 22:30:25 +02:00
(dispatch, { image, tags = [], updateTags, refetch }) => ({
2018-03-01 03:15:16 +02:00
shouldAsyncValidate: ({ trigger }) => {
return trigger === 'submit';
},
handleAsyncValidate: validateTag,
2018-02-14 22:30:25 +02:00
handleToggleAddOpen: addOpen => {
dispatch(set({ name: `${image.id}-add-open`, value: addOpen }));
},
handleRemoveTag: async name => {
dispatch(set({ name: `${image.id}-mutating`, value: true }));
const [err] = await intercept(
2018-02-14 22:30:25 +02:00
updateTags({
variables: {
id: image.id,
tags: tags
.map(({ name, value }) => ({ name, value }))
.filter(tag => tag.name !== name)
}
})
);
if (err) {
dispatch([
set({ name: `${image.id}-mutation-error`, value: parseError(err) }),
set({ name: `${image.id}-mutating`, value: false })
]);
}
await refetch();
dispatch(set({ name: `${image.id}-mutating`, value: false }));
},
handleAddTag: async ({ name, value }) => {
dispatch(set({ name: `${image.id}-mutating`, value: true }));
const [err] = await intercept(
2018-02-14 22:30:25 +02:00
updateTags({
variables: {
id: image.id,
tags: tags
.map(({ name, value }) => ({ name, value }))
.concat([{ name, value }])
}
})
);
if (err) {
dispatch([
set({ name: `${image.id}-mutation-error`, value: parseError(err) }),
set({ name: `${image.id}-mutating`, value: false })
]);
}
await refetch();
dispatch([
set({ name: `${image.id}-mutating`, value: false }),
dispatch(set({ name: `${image.id}-add-open`, value: false })),
destroy(TAGS_ADD_FORM)
]);
}
2018-02-05 17:12:47 +02:00
})
2018-02-14 22:30:25 +02:00
)
2018-02-05 17:12:47 +02:00
)(Tags);