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",
"declarative-redux-form": "^2.0.8",
"force-array": "^3.1.0",
"fuse.js": "^3.2.0",
"hapi-render-react": "^2.1.0",
"hapi-render-react-joyent-document": "^4.2.3",
"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 { connect } from 'react-redux';
import get from 'lodash.get';
import find from 'lodash.find';
import Index from '@state/gen-index';
import intercept from 'apr-intercept';
import { set } from 'react-redux-values';
import Fuse from 'fuse.js';
import {
ViewContainer,
@ -105,6 +104,9 @@ export default compose(
}),
props: ({ data: { images, loading, error, refetch } }) => ({
images,
index: new Fuse(images, {
keys: ['name', 'os', 'version', 'state', 'type']
}),
loading,
error
})
@ -129,9 +131,7 @@ export default compose(
);
const filtered = filter
? Index(images)
.search(filter)
.map(({ ref }) => find(images, ['id', ref]))
? index.search(filter)
: images;
return {

View File

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