fix(images): filter images and tags

fixes #1285
This commit is contained in:
Sérgio Ramos 2018-02-28 13:14:17 +00:00 committed by GitHub
parent 577df6d187
commit abe7a58e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 26 deletions

View File

@ -28,6 +28,7 @@
"date-fns": "^1.29.0", "date-fns": "^1.29.0",
"declarative-redux-form": "^2.0.8", "declarative-redux-form": "^2.0.8",
"force-array": "^3.1.0", "force-array": "^3.1.0",
"fuse.js": "^3.2.0",
"hapi-render-react": "^2.1.0", "hapi-render-react": "^2.1.0",
"hapi-render-react-joyent-document": "^4.2.3", "hapi-render-react-joyent-document": "^4.2.3",
"joyent-logo-assets": "^1.0.0", "joyent-logo-assets": "^1.0.0",

View File

@ -6,10 +6,9 @@ import remcalc from 'remcalc';
import { Row, Col } from 'joyent-react-styled-flexboxgrid'; import { Row, Col } from 'joyent-react-styled-flexboxgrid';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import get from 'lodash.get'; import get from 'lodash.get';
import find from 'lodash.find';
import Index from '@state/gen-index';
import intercept from 'apr-intercept'; import intercept from 'apr-intercept';
import { set } from 'react-redux-values'; import { set } from 'react-redux-values';
import Fuse from 'fuse.js';
import { import {
ViewContainer, ViewContainer,
@ -105,6 +104,9 @@ export default compose(
}), }),
props: ({ data: { images, loading, error, refetch } }) => ({ props: ({ data: { images, loading, error, refetch } }) => ({
images, images,
index: new Fuse(images, {
keys: ['name', 'os', 'version', 'state', 'type']
}),
loading, loading,
error error
}) })
@ -129,9 +131,7 @@ export default compose(
); );
const filtered = filter const filtered = filter
? Index(images) ? index.search(filter)
.search(filter)
.map(({ ref }) => find(images, ['id', ref]))
: images; : images;
return { return {

View File

@ -9,6 +9,7 @@ import intercept from 'apr-intercept';
import find from 'lodash.find'; import find from 'lodash.find';
import get from 'lodash.get'; import get from 'lodash.get';
import remcalc from 'remcalc'; import remcalc from 'remcalc';
import Fuse from 'fuse.js';
import { import {
H3, H3,
@ -131,10 +132,15 @@ export default compose(
refetch, refetch,
...rest ...rest
} = data; } = data;
const image = find(get(rest, 'images', []), ['name', variables.name]); const image = find(get(rest, 'images', []), ['name', variables.name]);
const tags = get(image || {}, 'tags', []); const tags = get(image || {}, 'tags', []);
const index = new Fuse(tags, {
keys: ['name', 'value']
});
return { return {
index,
image: image || {}, image: image || {},
tags, tags,
loading, loading,
@ -144,11 +150,17 @@ export default compose(
} }
}), }),
connect( connect(
({ values }, { image }) => ({ ({ values, form }, { index, tags, image }) => {
addOpen: get(values, `${image.id}-add-open`, false), const filter = get(form, `${TAGS_TOOLBAR_FORM}.values.filter`, false);
mutationError: get(values, `${image.id}-mutation-error`, false), const filtered = filter ? index.search(filter) : tags;
mutating: get(values, `${image.id}-mutating`, false)
}), 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)
};
},
(dispatch, { image, tags = [], updateTags, refetch }) => ({ (dispatch, { image, tags = [], updateTags, refetch }) => ({
handleToggleAddOpen: addOpen => { handleToggleAddOpen: addOpen => {
dispatch(set({ name: `${image.id}-add-open`, value: addOpen })); dispatch(set({ name: `${image.id}-add-open`, value: addOpen }));

View File

@ -1,16 +0,0 @@
import Lunr from 'lunr';
Lunr.tokenizer.separator = /[\s\-|_]+/;
export default items =>
Lunr(function() {
const fields = items
.map(item => Object.keys(item))
.reduce((all, keys) => all.concat(keys), [])
// eslint-disable-next-line no-implicit-coercion
.reduce((all, key) => (~all.indexOf(key) ? all : all.concat(key)), [])
.filter(key => !key.match(/^__/));
fields.forEach(field => this.field(field));
items.forEach(item => this.add(item));
});