2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import { Action, IActionHandler } from '../../flux/action';
19 import { Dispatch } from '../../flux/store';
21 import { AddErrorInfoAction } from '../../actions/errorActions';
22 import { IApplicationStoreState } from '../../store/applicationStore';
24 import { DataCallback } from ".";
25 export interface IExternalTableState<TData> {
26 order: 'asc' | 'desc';
27 orderBy: string | null;
28 selected: any[] | null;
35 filter: { [property: string]: string };
36 preFilter: { [property: string]: string };
39 /** Create an actionHandler and actions for external table states. */
40 export function createExternal<TData>(callback: DataCallback<TData>, selectState: (appState: IApplicationStoreState) => IExternalTableState<TData>) {
43 abstract class TableAction extends Action { }
46 class RequestSortAction extends TableAction {
47 constructor(public orderBy: string) {
52 class SetSelectedAction extends TableAction {
53 constructor(public selected: TData[] | null) {
58 class SetPageAction extends TableAction {
59 constructor(public page: number) {
64 class SetRowsPerPageAction extends TableAction {
65 constructor(public rowsPerPage: number) {
70 class SetPreFilterChangedAction extends TableAction {
71 constructor(public preFilter: {[key: string]: string}) {
76 class SetFilterChangedAction extends TableAction {
77 constructor (public filter: { [key: string]: string }) {
82 class SetShowFilterAction extends TableAction {
83 constructor(public show: boolean) {
88 class RefreshAction extends TableAction {
94 class SetResultAction extends TableAction {
95 constructor(public result: { page: number, rowCount: number, rows: TData[] }) {
102 //#region Action Handler
103 const externalTableStateInit: IExternalTableState<TData> = {
117 const externalTableStateActionHandler: IActionHandler<IExternalTableState<TData>> = (state = externalTableStateInit, action) => {
118 if (!(action instanceof TableAction)) return state;
119 if (action instanceof RefreshAction) {
124 } else if (action instanceof SetResultAction) {
128 rows: action.result.rows,
129 rowCount: action.result.rowCount,
130 page: action.result.page,
132 } else if (action instanceof RequestSortAction) {
136 orderBy : state.orderBy === action.orderBy && state.order === 'desc' ? null : action.orderBy ,
137 order: state.orderBy === action.orderBy && state.order === 'asc' ? 'desc' : 'asc',
139 } else if (action instanceof SetShowFilterAction) {
143 showFilter: action.show
145 } else if (action instanceof SetPreFilterChangedAction) {
149 preFilter: action.preFilter
151 } else if (action instanceof SetFilterChangedAction) {
155 filter: action.filter
157 } else if (action instanceof SetPageAction) {
163 } else if (action instanceof SetRowsPerPageAction) {
167 rowsPerPage: action.rowsPerPage
173 //const createTableAction(tableAction)
176 const reloadAction = (dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
177 dispatch(new RefreshAction());
178 const ownState = selectState(getAppState());
179 const filter = { ...ownState.preFilter, ...(ownState.showFilter && ownState.filter || {})};
180 Promise.resolve(callback(ownState.page, ownState.rowsPerPage, ownState.orderBy, ownState.order, filter )).then(result => {
181 dispatch(new SetResultAction(result));
182 }).catch(error => new AddErrorInfoAction(error));
185 const createPreActions = (dispatch: Dispatch, skipRefresh: boolean = false) => {
187 onPreFilterChanged: (preFilter: { [key: string]: string }) => {
188 dispatch(new SetPreFilterChangedAction(preFilter));
189 (!skipRefresh) && dispatch(reloadAction);
194 const createActions = (dispatch: Dispatch, skipRefresh: boolean = false) => {
197 dispatch(reloadAction);
199 onHandleRequestSort: (orderBy: string) => {
200 dispatch((dispatch: Dispatch) => {
201 dispatch(new RequestSortAction(orderBy));
202 (!skipRefresh) && dispatch(reloadAction);
205 onToggleFilter: () => {
206 dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
207 const { showFilter } = selectState(getAppState());
208 dispatch(new SetShowFilterAction(!showFilter));
209 (!skipRefresh) && dispatch(reloadAction);
212 onFilterChanged: (property: string, filterTerm: string) => {
213 dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
214 let { filter } = selectState(getAppState());
215 filter = { ...filter, [property]: filterTerm };
216 dispatch(new SetFilterChangedAction(filter));
217 (!skipRefresh) && dispatch(reloadAction);
220 onHandleChangePage: (page: number) => {
221 dispatch((dispatch: Dispatch) => {
222 dispatch(new SetPageAction(page));
223 (!skipRefresh) && dispatch(reloadAction);
226 onHandleChangeRowsPerPage: (rowsPerPage: number | null) => {
227 dispatch((dispatch: Dispatch) => {
228 dispatch(new SetRowsPerPageAction(rowsPerPage || 10));
229 (!skipRefresh) && dispatch(reloadAction);
236 const createProperties = (state: IApplicationStoreState) => {
238 ...selectState(state)
243 reloadAction: reloadAction,
244 createActions: createActions,
245 createProperties: createProperties,
246 createPreActions: createPreActions,
247 actionHandler: externalTableStateActionHandler