Skip to content

Commit

Permalink
[Discover] fix: infinite loop for view single document on disc… (elas…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra authored Mar 16, 2020
1 parent 0f97a58 commit a0605fd
Showing 1 changed file with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,34 @@ export function useEsDocSearch({
const [status, setStatus] = useState(ElasticRequestState.Loading);
const [hit, setHit] = useState<ElasticSearchHit | null>(null);

async function requestData() {
try {
const indexPatternEntity = await indexPatternService.get(indexPatternId);
setIndexPattern(indexPatternEntity);
useEffect(() => {
async function requestData() {
try {
const indexPatternEntity = await indexPatternService.get(indexPatternId);
setIndexPattern(indexPatternEntity);

const { hits } = await esClient.search({
index,
body: buildSearchBody(id, indexPatternEntity),
});
const { hits } = await esClient.search({
index,
body: buildSearchBody(id, indexPatternEntity),
});

if (hits && hits.hits && hits.hits[0]) {
setStatus(ElasticRequestState.Found);
setHit(hits.hits[0]);
} else {
setStatus(ElasticRequestState.NotFound);
}
} catch (err) {
if (err.savedObjectId) {
setStatus(ElasticRequestState.NotFoundIndexPattern);
} else if (err.status === 404) {
setStatus(ElasticRequestState.NotFound);
} else {
setStatus(ElasticRequestState.Error);
if (hits && hits.hits && hits.hits[0]) {
setStatus(ElasticRequestState.Found);
setHit(hits.hits[0]);
} else {
setStatus(ElasticRequestState.NotFound);
}
} catch (err) {
if (err.savedObjectId) {
setStatus(ElasticRequestState.NotFoundIndexPattern);
} else if (err.status === 404) {
setStatus(ElasticRequestState.NotFound);
} else {
setStatus(ElasticRequestState.Error);
}
}
}
}

useEffect(() => {
requestData();
});

}, [esClient, id, index, indexPatternId, indexPatternService]);
return [status, hit, indexPattern];
}

0 comments on commit a0605fd

Please sign in to comment.