Skip to content

Commit

Permalink
Simplify actions.js
Browse files Browse the repository at this point in the history
- merge fetchPredecessorRows & fetchSuccessorRows
- new function fetchSurroundingRows
- less code duplication
  • Loading branch information
kertal committed Jun 6, 2019
1 parent 3f67ade commit a1e5d10
Showing 1 changed file with 22 additions and 64 deletions.
86 changes: 22 additions & 64 deletions src/legacy/core_plugins/kibana/public/context/query/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { FAILURE_REASONS, LOADING_STATUS } from './constants';

export function QueryActionsProvider(Private, Promise) {
const fetchAnchor = Private(fetchAnchorProvider);
const { fetchPredecessors, fetchSuccessors } = Private(fetchContextProvider);
const { fetchSurroundingDocs } = Private(fetchContextProvider);
const {
increasePredecessorCount,
increaseSuccessorCount,
Expand Down Expand Up @@ -92,41 +92,45 @@ export function QueryActionsProvider(Private, Promise) {
);
};

const fetchPredecessorRows = (state) => () => {
const fetchSurroundingRows = (type, state) => {
const {
queryParameters: { indexPatternId, filters, predecessorCount, sort, tieBreakerField },
queryParameters: { indexPatternId, filters, sort, tieBreakerField },
rows: { anchor },
} = state;
const count = type === 'successors'
? state.queryParameters.successorCount
: state.queryParameters.predecessorCount;

if (!tieBreakerField) {
return Promise.reject(setFailedStatus(state)('predecessors', {
return Promise.reject(setFailedStatus(state)(type, {
reason: FAILURE_REASONS.INVALID_TIEBREAKER
}));
}

setLoadingStatus(state)('predecessors');
setLoadingStatus(state)(type);

return Promise.try(() => (
fetchPredecessors(
fetchSurroundingDocs(
type,
indexPatternId,
sort[0],
sort[1],
anchor.fields[sort[0]][0],
anchor.sort[0],
tieBreakerField,
anchor.sort[1],
predecessorCount,
count,
filters
)
))
.then(
(predecessorDocuments) => {
setLoadedStatus(state)('predecessors');
state.rows.predecessors = predecessorDocuments;
return predecessorDocuments;
(documents) => {
setLoadedStatus(state)(type);
state.rows[type] = documents;
return documents;
},
(error) => {
setFailedStatus(state)('predecessors', { error });
setFailedStatus(state)(type, { error });
toastNotifications.addDanger({
title: i18n.translate('kbn.context.unableToLoadDocumentDescription', {
defaultMessage: 'Unable to load documents'
Expand All @@ -138,54 +142,10 @@ export function QueryActionsProvider(Private, Promise) {
);
};

const fetchSuccessorRows = (state) => () => {
const {
queryParameters: { indexPatternId, filters, sort, successorCount, tieBreakerField },
rows: { anchor },
} = state;

if (!tieBreakerField) {
return Promise.reject(setFailedStatus(state)('successors', {
reason: FAILURE_REASONS.INVALID_TIEBREAKER
}));
}

setLoadingStatus(state)('successors');

return Promise.try(() => (
fetchSuccessors(
indexPatternId,
sort[0],
sort[1],
anchor.fields[sort[0]][0],
anchor.sort[0],
tieBreakerField,
anchor.sort[1],
successorCount,
filters
)
))
.then(
(successorDocuments) => {
setLoadedStatus(state)('successors');
state.rows.successors = successorDocuments;
return successorDocuments;
},
(error) => {
setFailedStatus(state)('successors', { error });
toastNotifications.addDanger({
title: 'Unable to load documents',
text: <MarkdownSimple>{error.message}</MarkdownSimple>,
});
throw error;
},
);
};

const fetchContextRows = (state) => () => (
Promise.all([
fetchPredecessorRows(state)(),
fetchSuccessorRows(state)(),
fetchSurroundingRows('predecessors', state),
fetchSurroundingRows('successors', state),
])
);

Expand All @@ -206,22 +166,22 @@ export function QueryActionsProvider(Private, Promise) {

const fetchGivenPredecessorRows = (state) => (count) => {
setPredecessorCount(state)(count);
return fetchPredecessorRows(state)();
return fetchSurroundingRows('predecessors', state);
};

const fetchGivenSuccessorRows = (state) => (count) => {
setSuccessorCount(state)(count);
return fetchSuccessorRows(state)();
return fetchSurroundingRows('successors', state);
};

const fetchMorePredecessorRows = (state) => () => {
increasePredecessorCount(state)();
return fetchPredecessorRows(state)();
return fetchSurroundingRows('predecessors', state);
};

const fetchMoreSuccessorRows = (state) => () => {
increaseSuccessorCount(state)();
return fetchSuccessorRows(state)();
return fetchSurroundingRows('successors', state);
};

const setAllRows = (state) => (predecessorRows, anchorRow, successorRows) => (
Expand All @@ -242,8 +202,6 @@ export function QueryActionsProvider(Private, Promise) {
fetchGivenSuccessorRows,
fetchMorePredecessorRows,
fetchMoreSuccessorRows,
fetchPredecessorRows,
fetchSuccessorRows,
setAllRows,
};
}

0 comments on commit a1e5d10

Please sign in to comment.