Skip to content

fix: report loading, handle empty parent edge cases #2

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

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Changes from all 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
59 changes: 33 additions & 26 deletions src/ParentWidget.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Select from 'react-select';
import AsyncSelect from 'react-select/async';
import { reactSelectStyles } from 'netlify-cms-ui-default/dist/esm/styles';
import { NestedCollection } from './NestedCollection';

Expand Down Expand Up @@ -45,7 +45,7 @@ const Option = (props) => {
export class ParentControl extends React.Component {
constructor(props) {
super(props);
this.state = { entries: [], title: '' };
this.state = { title: '', optionsLoaded: false, options: [] };
this.selectRef = React.createRef();
}

Expand Down Expand Up @@ -93,56 +93,63 @@ export class ParentControl extends React.Component {
}

async componentDidMount() {
const { forID, query, collection } = this.props;

const collectionName = collection.get('name');

const {
payload: {
response: { hits = [] },
},
} = await query(forID, collectionName, ['path'], '');

this.setState({
entries: hits,
});

if (this.isNewRecord()) {
this.props.onChange(this.getValue() + '/');
// track title field so we can use it for the folder name
const titleInput = document.querySelector('[id*=title-field]');
titleInput.addEventListener('input', (e) => {
const title = e.target.value;
this.setState({ title });
const parent = this.selectRef.current.props.options[0].value;
this.handleChange(parent);
const selectProps = this.selectRef.current.props;
const currentParent = selectProps.value?.value || '/';
this.handleChange(currentParent);
});
}
}

render() {
const { forID, classNameWrapper, setActiveStyle, setInactiveStyle, collection } = this.props;
async loadOptions() {
if (this.state.optionsLoaded) {
return this.state.options;
}
const { forID, query, collection } = this.props;
const collectionName = collection.get('name');
const {
payload: {
response: { hits = [] },
},
} = await query(forID, collectionName, ['path'], '');

const fullPath = this.getFullPath();
const parentPath = this.getParent(fullPath) || '';
const parent = this.state.entries.find((e) => this.getPath(e.path) === parentPath);
const parent = hits.find((e) => this.getPath(e.path) === parentPath);
const label = (parent && parent.data.title) || collection.get('label');

const options = [
{
value: parentPath,
label,
collection: this.props.collection,
entries: this.state.entries,
entries: hits,
onSelectNode: (value) => this.handleChange(value),
},
];

this.setState({
optionsLoaded: true,
options,
});

return options;
}

render() {
const { forID, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;

return (
<Select
value={options[0]}
<AsyncSelect
value={this.state.options[0]}
loadOptions={() => this.loadOptions()}
defaultOptions
inputId={forID}
options={options}
className={classNameWrapper}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
Expand Down