links: wait for api prop instantiation

Now that API is a prop, we can't use it to auto-grab data. We wait
for it as a prop before requesting info.
This commit is contained in:
Matilde Park 2020-06-05 15:43:44 -04:00
parent 24689f852d
commit 50cb14debd
2 changed files with 22 additions and 17 deletions

View File

@ -30,15 +30,17 @@ export class LinkDetail extends Component {
} }
componentDidMount() { componentDidMount() {
// if we have no preloaded data, and we aren't expecting it, get it this.componentDidUpdate();
if (!this.state.data.title) {
this.props.api.getSubmission(
this.props.resourcePath, this.props.url, this.updateData.bind(this)
);
}
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
// if we have no preloaded data, and we aren't expecting it, get it
if ((!this.state.data.title) && (this.props.api)) {
this.props.api?.getSubmission(
this.props.resourcePath, this.props.url, this.updateData.bind(this)
);
}
if (prevProps) {
if (this.props.url !== prevProps.url) { if (this.props.url !== prevProps.url) {
this.updateData(this.props.data); this.updateData(this.props.data);
} }
@ -58,6 +60,7 @@ export class LinkDetail extends Component {
} }
} }
} }
}
onClickPost() { onClickPost() {
const url = this.props.url || ''; const url = this.props.url || '';

View File

@ -26,14 +26,16 @@ export class Links extends Component {
// and don't have links for it yet, // and don't have links for it yet,
// or the links we have might not be complete, // or the links we have might not be complete,
// request the links for that page. // request the links for that page.
if ( (!prevProps || if ( ((!prevProps || // first load?
linkPage !== prevProps.page || linkPage !== prevProps.page || // already waiting on response?
this.props.resourcePath !== prevProps.resourcePath this.props.resourcePath !== prevProps.resourcePath // new page?
) && ) ||
!this.props.links[linkPage] || (prevProps.api !== this.props.api)) // api prop instantiated?
this.props.links.local[linkPage] &&
!this.props.links[linkPage] || // don't have info?
this.props.links.local[linkPage] // waiting on post confirmation?
) { ) {
this.props.api.getPage(this.props.resourcePath, this.props.page); this.props.api?.getPage(this.props.resourcePath, this.props.page);
} }
} }