fix(widget-relation): set initial metadata value (#4326)
This commit is contained in:
parent
db86ea2954
commit
5a4b36527e
@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
import { Async as AsyncSelect } from 'react-select';
|
import { Async as AsyncSelect } from 'react-select';
|
||||||
import { find, isEmpty, last, debounce, get, uniqBy, sortBy } from 'lodash';
|
import { find, isEmpty, last, debounce, get, uniqBy } from 'lodash';
|
||||||
import { List, Map, fromJS } from 'immutable';
|
import { List, Map, fromJS } from 'immutable';
|
||||||
import { reactSelectStyles } from 'netlify-cms-ui-default';
|
import { reactSelectStyles } from 'netlify-cms-ui-default';
|
||||||
import { stringTemplate } from 'netlify-cms-lib-widgets';
|
import { stringTemplate } from 'netlify-cms-lib-widgets';
|
||||||
@ -55,6 +55,10 @@ function uniqOptions(initial, current) {
|
|||||||
return uniqBy(initial.concat(current), o => o.value);
|
return uniqBy(initial.concat(current), o => o.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSearchFieldArray(searchFields) {
|
||||||
|
return List.isList(searchFields) ? searchFields.toJS() : [searchFields];
|
||||||
|
}
|
||||||
|
|
||||||
function getSelectedValue({ value, options, isMultiple }) {
|
function getSelectedValue({ value, options, isMultiple }) {
|
||||||
if (isMultiple) {
|
if (isMultiple) {
|
||||||
const selectedOptions = getSelectedOptions(value);
|
const selectedOptions = getSelectedOptions(value);
|
||||||
@ -111,22 +115,18 @@ export default class RelationControl extends React.Component {
|
|||||||
const initialSearchValues = value && (this.isMultiple() ? getSelectedOptions(value) : [value]);
|
const initialSearchValues = value && (this.isMultiple() ? getSelectedOptions(value) : [value]);
|
||||||
if (initialSearchValues && initialSearchValues.length > 0) {
|
if (initialSearchValues && initialSearchValues.length > 0) {
|
||||||
const metadata = {};
|
const metadata = {};
|
||||||
const allOptions = await Promise.all(
|
const searchFieldsArray = getSearchFieldArray(field.get('search_fields'));
|
||||||
initialSearchValues.map((v, index) => {
|
const { payload } = await query(forID, collection, searchFieldsArray, '', file);
|
||||||
return query(forID, collection, [field.get('value_field')], v, file, 1).then(
|
const hits = payload.response?.hits || [];
|
||||||
({ payload }) => {
|
const options = this.parseHitOptions(hits);
|
||||||
const hits = payload.response?.hits || [];
|
const initialOptions = initialSearchValues
|
||||||
const options = this.parseHitOptions(hits);
|
.map(v => {
|
||||||
metadata[v] = hits[0]?.data;
|
const selectedOption = options.find(o => o.value === v);
|
||||||
return { options, index };
|
metadata[v] = selectedOption?.data;
|
||||||
},
|
return selectedOption;
|
||||||
);
|
})
|
||||||
}),
|
.filter(Boolean);
|
||||||
);
|
|
||||||
|
|
||||||
const initialOptions = [].concat(
|
|
||||||
...sortBy(allOptions, ({ index }) => index).map(({ options }) => options),
|
|
||||||
);
|
|
||||||
this.mounted && this.setState({ initialOptions });
|
this.mounted && this.setState({ initialOptions });
|
||||||
|
|
||||||
//set metadata
|
//set metadata
|
||||||
@ -214,9 +214,8 @@ export default class RelationControl extends React.Component {
|
|||||||
loadOptions = debounce((term, callback) => {
|
loadOptions = debounce((term, callback) => {
|
||||||
const { field, query, forID } = this.props;
|
const { field, query, forID } = this.props;
|
||||||
const collection = field.get('collection');
|
const collection = field.get('collection');
|
||||||
const searchFields = field.get('search_fields');
|
|
||||||
const optionsLength = field.get('options_length') || 20;
|
const optionsLength = field.get('options_length') || 20;
|
||||||
const searchFieldsArray = List.isList(searchFields) ? searchFields.toJS() : [searchFields];
|
const searchFieldsArray = getSearchFieldArray(field.get('search_fields'));
|
||||||
const file = field.get('file');
|
const file = field.get('file');
|
||||||
|
|
||||||
query(forID, collection, searchFieldsArray, term, file, optionsLength).then(({ payload }) => {
|
query(forID, collection, searchFieldsArray, term, file, optionsLength).then(({ payload }) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user