Skip to content

Commit 81748e2

Browse files
committed
Simplify fetchCDXData
1 parent 8e75bd2 commit 81748e2

1 file changed

Lines changed: 23 additions & 51 deletions

File tree

src/components/ymd-timestamp-header.jsx

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -232,74 +232,46 @@ export default class YmdTimestampHeader extends React.Component {
232232
.catch(error => { this._errorHandled(error.message); });
233233
};
234234

235+
_getMonth = (currentMonth, monthSelectRef, timestamp) => {
236+
if (currentMonth !== '' && monthSelectRef.current) {
237+
return monthSelectRef.current.value;
238+
} else if (timestamp) {
239+
return timestamp.substring(4, 6);
240+
}
241+
return '';
242+
};
243+
235244
/**
236245
* On component init get current month from timestamp because the months selects
237246
* aren't rendered yet.
238247
*/
239248
_fetchCDXData = () => {
240-
let leftMonth = '';
241-
let rightMonth = '';
242-
if (this.state.leftMonth !== '' && this.monthSelectLeft.current) {
243-
leftMonth = this.monthSelectLeft.current.value;
244-
} else if (this.state.timestampA) {
245-
leftMonth = this.state.timestampA.substring(4, 6);
246-
}
247-
if (this.state.rightMonth !== '' && this.monthSelectRight.current) {
248-
rightMonth = this.monthSelectRight.current.value;
249-
} else if (this.state.timestampB) {
250-
rightMonth = this.state.timestampB.substring(4, 6);
251-
}
249+
const leftMonth = this._getMonth(this.state.leftMonth, this.monthSelectLeft, this.state.timestampA);
250+
const rightMonth = this._getMonth(this.state.rightMonth, this.monthSelectRight, this.state.timestampB);
252251

253252
this.setState({ leftMonth, rightMonth });
254253

255-
let leftFetchPromise;
256-
let rightFetchPromise;
254+
const leftFetchPromise = leftMonth !== '' ? this.fetchYearMonthCaptures(this.state.leftYear, leftMonth) : null;
255+
const rightFetchPromise = rightMonth !== '' ? this.fetchYearMonthCaptures(this.state.rightYear, rightMonth) : null;
257256

258-
if (leftMonth !== '') {
259-
leftFetchPromise = this.fetchYearMonthCaptures(this.state.leftYear, leftMonth);
260-
}
261-
if (rightMonth !== '') {
262-
rightFetchPromise = this.fetchYearMonthCaptures(this.state.rightYear, rightMonth);
263-
}
257+
Promise.allSettled([leftFetchPromise, rightFetchPromise])
258+
.then(([leftResult, rightResult]) => {
259+
const leftSnaps = leftResult?.value || [];
260+
const rightSnaps = rightResult?.value || [];
264261

265-
if (leftFetchPromise) {
266-
leftFetchPromise
267-
.then((data) => {
268-
if (data && data.length > 0) {
269-
if (rightFetchPromise) {
270-
const leftData = data;
271-
rightFetchPromise
272-
.then((data) => {
273-
if (data && data.length > 0) {
274-
this._prepareCDXData(leftData, data);
275-
}
276-
});
277-
} else {
278-
this._prepareCDXData(data, null);
279-
}
280-
}
281-
})
282-
.catch(error => { this._errorHandled(error.message); });
283-
} else if (rightFetchPromise) {
284-
rightFetchPromise
285-
.then((data) => {
286-
if (data && data.length > 0) {
287-
this._prepareCDXData(null, data);
288-
}
289-
});
290-
}
262+
if (leftSnaps.length > 0 || rightSnaps.length > 0) {
263+
this.props.getTimestampsCallback(this.state.timestampA, this.state.timestampB);
264+
this.setState({ leftSnaps, rightSnaps });
265+
}
266+
})
267+
.catch(error => { this._errorHandled(error.message); });
291268
};
292269

293270
_errorHandled = (error) => {
294271
this.props.errorHandledCallback(error);
295272
this.setState({ showError: true });
296273
};
297274

298-
_prepareCDXData = (leftSnaps, rightSnaps) => {
299-
this.props.getTimestampsCallback(this.state.timestampA, this.state.timestampB);
300-
this.setState({ leftSnaps, rightSnaps });
301-
};
302-
303275
/** Each item has [key, value, counter] **/
304276
_showOptions = (data) => {
305277
const limit = parseInt(this.props.conf.limit);

0 commit comments

Comments
 (0)