Skip to content

Commit

Permalink
Renamed all types related to data binding to resource state.
Browse files Browse the repository at this point in the history
  • Loading branch information
efreeti committed Sep 3, 2020
1 parent 7e998f3 commit c624cc7
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 132 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 { Immutable } from '../../../../../common/endpoint/types';
import { ServerApiError } from '../../../../common/types';

export interface UninitialisedResourceState {
type: 'UninitialisedResourceState';
}

export interface LoadingResourceState<Data, Error = ServerApiError> {
type: 'LoadingResourceState';
previousState: StaleResourceState<Data, Error>;
}

export interface LoadedResourceState<Data> {
type: 'LoadedResourceState';
data: Data;
}

export interface FailedResourceState<Data, Error = ServerApiError> {
type: 'FailedResourceState';
error: Error;
lastLoadedState?: LoadedResourceState<Data>;
}

export type StaleResourceState<Data, Error = ServerApiError> =
| UninitialisedResourceState
| LoadedResourceState<Data>
| FailedResourceState<Data, Error>;

export type AsyncResourceState<Data, Error = ServerApiError> =
| UninitialisedResourceState
| LoadingResourceState<Data, Error>
| LoadedResourceState<Data>
| FailedResourceState<Data, Error>;

export const isUninitialisedResourceState = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): state is Immutable<UninitialisedResourceState> => state.type === 'UninitialisedResourceState';

export const isLoadingResourceState = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): state is Immutable<LoadingResourceState<Data, Error>> => state.type === 'LoadingResourceState';

export const isLoadedResourceState = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): state is Immutable<LoadedResourceState<Data>> => state.type === 'LoadedResourceState';

export const isFailedResourceState = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): state is Immutable<FailedResourceState<Data, Error>> => state.type === 'FailedResourceState';

export const getLastLoadedResourceState = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): Immutable<LoadedResourceState<Data>> | undefined => {
if (isLoadedResourceState(state)) {
return state;
} else if (isLoadingResourceState(state)) {
return getLastLoadedResourceState(state.previousState);
} else if (isFailedResourceState(state)) {
return state.lastLoadedState;
} else {
return undefined;
}
};

export const getLastLoadedResourceData = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): Immutable<Data> | undefined => {
return getLastLoadedResourceState(state)?.data;
};

export const getCurrentResourceError = <Data, Error>(
state: Immutable<AsyncResourceState<Data, Error>>
): Immutable<Error> | undefined => {
return isFailedResourceState(state) ? state.error : undefined;
};

export const isOutdatedResourceState = <Data, Error>(
state: AsyncResourceState<Data, Error>,
isFresh: (data: Data) => boolean
): boolean =>
isUninitialisedResourceState(state) ||
(isLoadedResourceState(state) && !isFresh(state.data)) ||
(isFailedResourceState(state) &&
(!state.lastLoadedState || !isFresh(state.lastLoadedState.data)));
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { TrustedApp } from '../../../../../common/endpoint/types/trusted_apps';
import { AsyncDataBinding } from './async_data_binding';
import { AsyncResourceState } from './async_resource_state';

export interface PaginationInfo {
index: number;
Expand All @@ -20,7 +20,7 @@ export interface TrustedAppsListData {

export interface TrustedAppsListPageState {
listView: {
currentListData: AsyncDataBinding<TrustedAppsListData>;
currentListData: AsyncResourceState<TrustedAppsListData>;
currentPaginationInfo: PaginationInfo;
};
active: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AsyncDataBinding } from '../state/async_data_binding';
import { AsyncResourceState } from '../state/async_resource_state';
import { TrustedAppsListData } from '../state/trusted_apps_list_page_state';

export interface TrustedAppsListDataBindingChanged {
type: 'trustedAppsListDataBindingChanged';
export interface TrustedAppsListResourceStateChanged {
type: 'trustedAppsListResourceStateChanged';
payload: {
newBinding: AsyncDataBinding<TrustedAppsListData>;
newState: AsyncResourceState<TrustedAppsListData>;
};
}

export type TrustedAppsPageAction = TrustedAppsListDataBindingChanged;
export type TrustedAppsPageAction = TrustedAppsListResourceStateChanged;
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import { Immutable } from '../../../../../common/endpoint/types';
import { ImmutableMiddlewareAPI, ImmutableMiddlewareFactory } from '../../../../common/store';
import { AppAction } from '../../../../common/store/actions';
import {
AsyncDataBinding,
StaleAsyncBinding,
getLastPresentDataBinding,
} from '../state/async_data_binding';
AsyncResourceState,
StaleResourceState,
getLastLoadedResourceState,
} from '../state/async_resource_state';
import {
TrustedAppsListData,
TrustedAppsListPageState,
} from '../state/trusted_apps_list_page_state';
import { TrustedAppsHttpService, TrustedAppsService } from '../service';
import { needsRefreshOfListData } from './selectors';
import { TrustedAppsListDataBindingChanged } from './action';
import { TrustedAppsListResourceStateChanged } from './action';

const createListDataBindingChangedAction = (
newBinding: Immutable<AsyncDataBinding<TrustedAppsListData>>
): Immutable<TrustedAppsListDataBindingChanged> => ({
type: 'trustedAppsListDataBindingChanged',
payload: { newBinding },
const createTrustedAppsListResourceStateChangedAction = (
newState: Immutable<AsyncResourceState<TrustedAppsListData>>
): Immutable<TrustedAppsListResourceStateChanged> => ({
type: 'trustedAppsListResourceStateChanged',
payload: { newState },
});

const refreshList = async (
Expand All @@ -34,10 +34,10 @@ const refreshList = async (
const list = store.getState().listView;

store.dispatch(
createListDataBindingChangedAction({
type: 'InProgressAsyncBinding',
createTrustedAppsListResourceStateChangedAction({
type: 'LoadingResourceState',
// need to think on how to avoid the casting
previousBinding: list.currentListData as Immutable<StaleAsyncBinding<TrustedAppsListData>>,
previousState: list.currentListData as Immutable<StaleResourceState<TrustedAppsListData>>,
})
);

Expand All @@ -48,8 +48,8 @@ const refreshList = async (
});

store.dispatch(
createListDataBindingChangedAction({
type: 'PresentAsyncBinding',
createTrustedAppsListResourceStateChangedAction({
type: 'LoadedResourceState',
data: {
items: response.data,
paginationInfo: list.currentPaginationInfo,
Expand All @@ -59,10 +59,10 @@ const refreshList = async (
);
} catch (error) {
store.dispatch(
createListDataBindingChangedAction({
type: 'FailedAsyncBinding',
createTrustedAppsListResourceStateChangedAction({
type: 'FailedResourceState',
error,
lastPresentBinding: getLastPresentDataBinding(list.currentListData),
lastLoadedState: getLastLoadedResourceState(list.currentListData),
})
);
}
Expand All @@ -76,7 +76,7 @@ export const trustedAppsPageMiddlewareFactory: ImmutableMiddlewareFactory<Truste
return (store) => (next) => async (action) => {
next(action);

// TODO: need to think if failed binding is a good condition to consider need for refresh
// TODO: need to think if failed state is a good condition to consider need for refresh
if (action.type === 'userChangedUrl' && needsRefreshOfListData(store.getState())) {
await refreshList(store, trustedAppsService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
MANAGEMENT_DEFAULT_PAGE,
MANAGEMENT_DEFAULT_PAGE_SIZE,
} from '../../../common/constants';
import { TrustedAppsListDataBindingChanged } from './action';
import { TrustedAppsListResourceStateChanged } from './action';
import { TrustedAppsListPageState } from '../state/trusted_apps_list_page_state';

type StateReducer = ImmutableReducer<TrustedAppsListPageState, AppAction>;
Expand All @@ -35,15 +35,15 @@ const isTrustedAppsPageLocation = (location: Immutable<AppLocation>) => {
);
};

const trustedAppsListDataBindingChanged: CaseReducer<TrustedAppsListDataBindingChanged> = (
const trustedAppsListResourceStateChanged: CaseReducer<TrustedAppsListResourceStateChanged> = (
state,
action
) => {
return {
...state,
listView: {
...state.listView,
currentListData: action.payload.newBinding,
currentListData: action.payload.newState,
},
};
};
Expand All @@ -70,7 +70,7 @@ const userChangedUrl: CaseReducer<UserChangedUrl> = (state, action) => {

export const initialTrustedAppsPageState: TrustedAppsListPageState = {
listView: {
currentListData: { type: 'UninitialisedAsyncBinding' },
currentListData: { type: 'UninitialisedResourceState' },
currentPaginationInfo: {
index: MANAGEMENT_DEFAULT_PAGE,
size: MANAGEMENT_DEFAULT_PAGE_SIZE,
Expand All @@ -84,8 +84,8 @@ export const trustedAppsPageReducer: StateReducer = (
action
) => {
switch (action.type) {
case 'trustedAppsListDataBindingChanged':
return trustedAppsListDataBindingChanged(state, action);
case 'trustedAppsListResourceStateChanged':
return trustedAppsListResourceStateChanged(state, action);

case 'userChangedUrl':
return userChangedUrl(state, action);
Expand Down
Loading

0 comments on commit c624cc7

Please sign in to comment.