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,20 +30,22 @@ export class LinkDetail extends Component {
}
componentDidMount() {
// if we have no preloaded data, and we aren't expecting it, get it
if (!this.state.data.title) {
this.props.api.getSubmission(
this.props.resourcePath, this.props.url, this.updateData.bind(this)
);
}
this.componentDidUpdate();
}
componentDidUpdate(prevProps) {
if (this.props.url !== prevProps.url) {
this.updateData(this.props.data);
// 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.comments && prevProps.comments['0'] &&
this.props.comments && this.props.comments['0']) {
if (prevProps) {
if (this.props.url !== prevProps.url) {
this.updateData(this.props.data);
}
if (prevProps.comments && prevProps.comments['0'] &&
this.props.comments && this.props.comments['0']) {
const prevFirstComment = prevProps.comments['0'][0];
const thisFirstComment = this.props.comments['0'][0];
if ((prevFirstComment && prevFirstComment.udon) &&
@ -56,6 +58,7 @@ export class LinkDetail extends Component {
});
}
}
}
}
}

View File

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