Skip to content

Commit

Permalink
list api called when user directly navigates to details
Browse files Browse the repository at this point in the history
  • Loading branch information
parkiino committed Apr 17, 2020
1 parent fd95269 commit 2fb8c40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface ServerReturnedHostList {
payload: HostResultList;
}

interface ServerFailedToReturnHostList {
type: 'serverFailedToReturnHostList';
payload: ServerApiError;
}

interface ServerReturnedHostDetails {
type: 'serverReturnedHostDetails';
payload: HostInfo;
Expand All @@ -24,5 +29,6 @@ interface ServerFailedToReturnHostDetails {

export type HostAction =
| ServerReturnedHostList
| ServerFailedToReturnHostList
| ServerReturnedHostDetails
| ServerFailedToReturnHostDetails;
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('host list middleware', () => {
await sleep();
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: 0 }, { page_size: 10 }],
paging_properties: [{ page_index: '0' }, { page_size: '10' }],
}),
});
expect(listData(getState())).toEqual(apiResponse.hosts.map(hostInfo => hostInfo.metadata));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { MiddlewareFactory } from '../../types';
import { isOnHostPage, hasSelectedHost, uiQueryParams } from './selectors';
import { isOnHostPage, hasSelectedHost, uiQueryParams, listData } from './selectors';
import { HostListState } from '../../types';
import { AppAction } from '../action';

Expand All @@ -19,18 +19,48 @@ export const hostMiddlewareFactory: MiddlewareFactory<HostListState> = coreStart
hasSelectedHost(state) !== true
) {
const { page_index: pageIndex, page_size: pageSize } = uiQueryParams(state);
const response = await coreStart.http.post('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: pageIndex }, { page_size: pageSize }],
}),
});
response.request_page_index = Number(pageIndex);
dispatch({
type: 'serverReturnedHostList',
payload: response,
});
try {
const response = await coreStart.http.post('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: pageIndex }, { page_size: pageSize }],
}),
});
response.request_page_index = Number(pageIndex);
dispatch({
type: 'serverReturnedHostList',
payload: response,
});
} catch (error) {
dispatch({
type: 'serverFailedToReturnHostList',
payload: error,
});
}
}
if (action.type === 'userChangedUrl' && hasSelectedHost(state) !== false) {
// If user navigated directly to a host details page, load the host list
if (listData(state).length === 0) {
const { page_index: pageIndex, page_size: pageSize } = uiQueryParams(state);
try {
const response = await coreStart.http.post('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: pageIndex }, { page_size: pageSize }],
}),
});
response.request_page_index = Number(pageIndex);
dispatch({
type: 'serverReturnedHostList',
payload: response,
});
} catch (error) {
dispatch({
type: 'serverFailedToReturnHostList',
payload: error,
});
}
}

// call the host details api
const { selected_host: selectedHost } = uiQueryParams(state);
try {
const response = await coreStart.http.get(`/api/endpoint/metadata/${selectedHost}`);
Expand Down

0 comments on commit 2fb8c40

Please sign in to comment.