Skip to content

Commit

Permalink
Use endpoint in the useLogFlyout hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez committed Dec 18, 2019
1 parent edf6a45 commit 45ad644
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { fold } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import { identity } from 'fp-ts/lib/function';
import { kfetch } from 'ui/kfetch';

import { throwErrors, createPlainError } from '../../../../../common/runtime_types';

import {
LOG_ENTRIES_ITEM_PATH,
LogEntriesItemRequest,
logEntriesItemRequestRT,
logEntriesItemResponseRT,
} from '../../../../../common/http_api';

export const fetchLogEntriesItem = async (requestArgs: LogEntriesItemRequest) => {
const response = await kfetch({
method: 'POST',
pathname: LOG_ENTRIES_ITEM_PATH,
body: JSON.stringify(logEntriesItemRequestRT.encode(requestArgs)),
});

return pipe(
logEntriesItemResponseRT.decode(response),
fold(throwErrors(createPlainError), identity)
);
};
25 changes: 5 additions & 20 deletions x-pack/legacy/plugins/infra/public/containers/logs/log_flyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import createContainer from 'constate';
import { isString } from 'lodash';
import React, { useContext, useEffect, useMemo, useState } from 'react';

import { FlyoutItemQuery, InfraLogItem } from '../../graphql/types';
import { useApolloClient } from '../../utils/apollo_context';
import { InfraLogItem } from '../../graphql/types';
import { UrlStateContainer } from '../../utils/url_state';
import { useTrackedPromise } from '../../utils/use_tracked_promise';
import { Source } from '../source';
import { flyoutItemQuery } from './flyout_item.gql_query';
import { fetchLogEntriesItem } from './log_entries/api/fetch_log_entries_item';

export enum FlyoutVisibility {
hidden = 'hidden',
Expand All @@ -33,37 +32,23 @@ export const useLogFlyout = () => {
const [flyoutItem, setFlyoutItem] = useState<InfraLogItem | null>(null);
const [surroundingLogsId, setSurroundingLogsId] = useState<string | null>(null);

const apolloClient = useApolloClient();

const [loadFlyoutItemRequest, loadFlyoutItem] = useTrackedPromise(
{
cancelPreviousOn: 'creation',
createPromise: async () => {
if (!apolloClient) {
throw new Error('Failed to load flyout item: No apollo client available.');
}

if (!flyoutId) {
return;
}

return await apolloClient.query<FlyoutItemQuery.Query, FlyoutItemQuery.Variables>({
fetchPolicy: 'no-cache',
query: flyoutItemQuery,
variables: {
itemId: flyoutId,
sourceId,
},
});
return await fetchLogEntriesItem({ sourceId, id: flyoutId });
},
onResolve: response => {
if (response) {
const { data } = response;
setFlyoutItem((data && data.source && data.source.logItem) || null);
setFlyoutItem(data || null);
}
},
},
[apolloClient, sourceId, flyoutId]
[sourceId, flyoutId]
);

const isLoading = useMemo(() => {
Expand Down

0 comments on commit 45ad644

Please sign in to comment.