Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(widget-relation): target file collections #3754

Merged
merged 7 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ collections: # A list of collections the CMS should be able to edit
- label: 'Object'
name: 'object'
widget: 'object'
collapsed: true
fields:
- label: 'Related Post'
name: 'post'
Expand Down
66 changes: 50 additions & 16 deletions packages/netlify-cms-widget-relation/src/RelationControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { Async as AsyncSelect } from 'react-select';
import { find, isEmpty, last, debounce } from 'lodash';
import { find, isEmpty, last, debounce, get, trimEnd } from 'lodash';
import { List, Map, fromJS } from 'immutable';
import { reactSelectStyles } from 'netlify-cms-ui-default';
import { stringTemplate } from 'netlify-cms-lib-widgets';
Expand Down Expand Up @@ -35,6 +35,33 @@ function getSelectedValue({ value, options, isMultiple }) {
}
}

export const expandPath = ({ data, path, paths = [] }) => {
if (path.endsWith('.*')) {
path = path + '.';
}

const sep = '.*.';
const parts = path.split(sep);
if (parts.length === 1) {
paths.push(path);
} else {
const partialPath = parts[0];
const value = get(data, partialPath);

if (Array.isArray(value)) {
value.forEach((v, index) => {
expandPath({
data,
path: trimEnd(`${partialPath}.${index}.${parts.slice(1).join(sep)}`, '.'),
paths,
});
});
}
}

return paths;
};

export default class RelationControl extends React.Component {
didInitialSearch = false;

Expand Down Expand Up @@ -128,24 +155,26 @@ export default class RelationControl extends React.Component {
parseHitOptions = hits => {
const { field } = this.props;
const valueField = field.get('valueField');
const displayField = field.get('displayFields') || field.get('valueField');
const displayField = field.get('displayFields') || List(field.get('valueField'));
Copy link
Contributor

@erezrokah erezrokah May 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sure displayField is always a list to avoid checking it later like https://github.com/netlify/netlify-cms/pull/3754/files#diff-1c6e2a499213935b7c1b04dbb4e1ca1cL135


return hits.map(hit => {
let labelReturn;
if (List.isList(displayField)) {
labelReturn = displayField
const options = hits.reduce((acc, hit) => {
const valuesPaths = expandPath({ data: hit.data, path: valueField });
for (let i = 0; i < valuesPaths.length; i++) {
const label = displayField
.toJS()
.map(key => this.parseNestedFields(hit, key))
.map(key => {
const displayPaths = expandPath({ data: hit.data, path: key });
return this.parseNestedFields(hit, displayPaths[i] || displayPaths[0]);
})
.join(' ');
} else {
labelReturn = this.parseNestedFields(hit, displayField);
const value = this.parseNestedFields(hit, valuesPaths[i]);
acc.push({ data: hit.data, value, label });
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is really neat 👍

return {
data: hit.data,
value: this.parseNestedFields(hit, valueField),
label: labelReturn,
};
});

return acc;
}, []);

return options;
};

loadOptions = debounce((term, callback) => {
Expand All @@ -154,8 +183,13 @@ export default class RelationControl extends React.Component {
const searchFields = field.get('searchFields');
const optionsLength = field.get('optionsLength') || 20;
const searchFieldsArray = List.isList(searchFields) ? searchFields.toJS() : [searchFields];
const file = field.get('file');

const queryPromise = file
? query(forID, collection, ['slug'], file)
: query(forID, collection, searchFieldsArray, term);

query(forID, collection, searchFieldsArray, term).then(({ payload }) => {
queryPromise.then(({ payload }) => {
let options =
payload.response && payload.response.hits
? this.parseHitOptions(payload.response.hits)
Expand Down
130 changes: 129 additions & 1 deletion packages/netlify-cms-widget-relation/src/__tests__/relation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fromJS, Map } from 'immutable';
import { last } from 'lodash';
import { render, fireEvent, wait } from '@testing-library/react';
import { NetlifyCmsWidgetRelation } from '../';
import { expandPath } from '../RelationControl';

const RelationControl = NetlifyCmsWidgetRelation.controlComponent;

Expand Down Expand Up @@ -81,6 +82,27 @@ const generateHits = length => {
];
};

const simpleFileCollectionHits = [{ data: { categories: ['category 1', 'category 2'] } }];

const nestedFileCollectionHits = [
{
data: {
nested: {
categories: [
{
name: 'category 1',
id: 'cat1',
},
{
name: 'category 2',
id: 'cat2',
},
],
},
},
},
];

class RelationController extends React.Component {
state = {
value: this.props.value,
Expand All @@ -98,7 +120,12 @@ class RelationController extends React.Component {

query = jest.fn((...args) => {
const queryHits = generateHits(25);
if (last(args) === 'YAML') {

if (last(args) === 'nested_file') {
return Promise.resolve({ payload: { response: { hits: nestedFileCollectionHits } } });
} else if (last(args) === 'simple_file') {
return Promise.resolve({ payload: { response: { hits: simpleFileCollectionHits } } });
} else if (last(args) === 'YAML') {
return Promise.resolve({ payload: { response: { hits: [last(queryHits)] } } });
} else if (last(args) === 'Nested') {
return Promise.resolve({
Expand Down Expand Up @@ -160,6 +187,68 @@ function setup({ field, value }) {
};
}

describe('expandPath', () => {
it('should expand wildcard paths', () => {
const data = {
categories: [
{
name: 'category 1',
},
{
name: 'category 2',
},
],
};

expect(expandPath({ data, path: 'categories.*.name' })).toEqual([
'categories.0.name',
'categories.1.name',
]);
});

it('should handle wildcard at the end of the path', () => {
const data = {
nested: {
otherNested: {
list: [
{
title: 'title 1',
nestedList: [{ description: 'description 1' }, { description: 'description 2' }],
},
{
title: 'title 2',
nestedList: [{ description: 'description 2' }, { description: 'description 2' }],
},
],
},
},
};

expect(expandPath({ data, path: 'nested.otherNested.list.*.nestedList.*' })).toEqual([
'nested.otherNested.list.0.nestedList.0',
'nested.otherNested.list.0.nestedList.1',
'nested.otherNested.list.1.nestedList.0',
'nested.otherNested.list.1.nestedList.1',
]);
});

it('should handle non wildcard index', () => {
const data = {
categories: [
{
name: 'category 1',
},
{
name: 'category 2',
},
],
};
const path = 'categories.0.name';

expect(expandPath({ data, path })).toEqual(['categories.0.name']);
});
});

describe('Relation widget', () => {
it('should list the first 20 option hits on initial load', async () => {
const field = fromJS(fieldConfig);
Expand Down Expand Up @@ -319,4 +408,43 @@ describe('Relation widget', () => {
});
});
});

describe('with file collection', () => {
const fileFieldConfig = {
name: 'categories',
collection: 'file',
file: 'simple_file',
valueField: 'categories.*',
displayFields: ['categories.*'],
};

it('should handle simple list', async () => {
const field = fromJS(fileFieldConfig);
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });

await wait(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});

it('should handle nested list', async () => {
const field = fromJS({
...fileFieldConfig,
file: 'nested_file',
valueField: 'nested.categories.*.id',
displayFields: ['nested.categories.*.name'],
});
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });

await wait(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});
});
});