summaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/components/material-table/utilities.ts
blob: e52fdb73120d336e9015043e9d713ed515ab0553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Action, IActionHandler } from '../../flux/action';
import { Dispatch } from '../../flux/store';

import { AddErrorInfoAction } from '../../actions/errorActions';
import { IApplicationStoreState } from '../../store/applicationStore';

import { DataCallback } from ".";
export interface IExternalTableState<TData> {
  order: 'asc' | 'desc';
  orderBy: string | null;
  selected: any[] | null;
  rows: TData[];
  rowCount: number;
  page: number;
  rowsPerPage: number;
  loading: boolean;
  showFilter: boolean;
  filter: { [property: string]: string };
}

/** Create an actionHandler and actions for external table states. */
export function createExternal<TData>(callback: DataCallback<TData>, selectState: (appState: IApplicationStoreState) => IExternalTableState<TData>) {

  //#region Actions
  abstract class TableAction extends Action { }

 
  class RequestSortAction extends TableAction {
    constructor(public orderBy: string) {
      super();
    }
  }

  class SetSelectedAction extends TableAction {
    constructor(public selected: TData[] | null) {
      super();
    }
  }

  class SetPageAction extends TableAction {
    constructor(public page: number) {
      super();
    }
  }

  class SetRowsPerPageAction extends TableAction {
    constructor(public rowsPerPage: number) {
      super();
    }
  }

  class SetFilterChangedAction extends TableAction {
    constructor(public filter: {[key: string]: string}) {
      super();
    }
  }

  class SetShowFilterAction extends TableAction {
    constructor(public show: boolean) {
      super();
    }
  }

  class RefreshAction extends TableAction {
    constructor() {
      super();
    }
  }

  class SetResultAction extends TableAction {
    constructor(public result: { page: number, rowCount: number, rows: TData[] }) {
      super();
    }
  }

  // #endregion
 
  //#region Action Handler
  const externalTableStateInit: IExternalTableState<TData> = {
    order: 'asc',
    orderBy: null,
    selected: null,
    rows: [],
    rowCount: 0,
    page: 0,
    rowsPerPage: 10,
    loading: false,
    showFilter: false,
    filter: {}
  };

  const externalTableStateActionHandler: IActionHandler<IExternalTableState<TData>> = (state = externalTableStateInit, action) => {
    if (!(action instanceof TableAction)) return state;
    if (action instanceof RefreshAction) {
      state = {
        ...state,
        loading: true
      }
    } else if (action instanceof SetResultAction) {
      state = {
        ...state,
        loading: false,
        rows: action.result.rows,
        rowCount: action.result.rowCount,
        page: action.result.page,
      }
    } else if (action instanceof RequestSortAction) {
      state = {
        ...state,
        loading: true,
        orderBy : state.orderBy === action.orderBy && state.order === 'desc' ? null : action.orderBy ,
        order: state.orderBy === action.orderBy && state.order === 'asc' ? 'desc' : 'asc',
      }
    } else if (action instanceof SetShowFilterAction) {
      state = {
        ...state,
        loading: true,
        showFilter: action.show
      }
    } else if (action instanceof SetFilterChangedAction) {
      state = {
        ...state,
        loading: true,
        filter: action.filter
      }
    } else if (action instanceof SetPageAction) {
      state = {
        ...state,
        loading: true,
        page: action.page
      }
    } else if (action instanceof SetRowsPerPageAction) {
      state = {
        ...state,
        loading: true,
        rowsPerPage: action.rowsPerPage
      }
    } 
    return state;
  }

  //const createTableAction(tableAction)

  //#endregion
  const reloadAction = (dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
    dispatch(new RefreshAction());
    const ownState = selectState(getAppState());
    Promise.resolve(callback(ownState.page, ownState.rowsPerPage, ownState.orderBy, ownState.order, ownState.showFilter && ownState.filter || {})).then(result => {
      dispatch(new SetResultAction(result));
    }).catch(error => new AddErrorInfoAction(error));
  };

  const createActions = (dispatch: Dispatch, skipRefresh: boolean = false) => {
    return {
      onRefresh: () => {
        dispatch(reloadAction);
      },
      onHandleRequestSort: (orderBy: string) => {
        dispatch((dispatch: Dispatch) => {
          dispatch(new RequestSortAction(orderBy));
          (!skipRefresh) && dispatch(reloadAction);
        });
      },
      onToggleFilter: () => {
        dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
          const { showFilter } = selectState(getAppState());
          dispatch(new SetShowFilterAction(!showFilter));
          (!skipRefresh) && dispatch(reloadAction);
        });
      },
      onFilterChanged: (property: string, filterTerm: string) => {
        dispatch((dispatch: Dispatch, getAppState: () => IApplicationStoreState) => {
          let { filter } = selectState(getAppState());
          filter = { ...filter, [property]: filterTerm };
          dispatch(new SetFilterChangedAction(filter));
          (!skipRefresh) && dispatch(reloadAction);
        });
      },
      onHandleChangePage: (page: number) => { 
        dispatch((dispatch: Dispatch) => {
          dispatch(new SetPageAction(page));
          (!skipRefresh) && dispatch(reloadAction);
        });
      },
      onHandleChangeRowsPerPage: (rowsPerPage: number | null) => {
        dispatch((dispatch: Dispatch) => {
          dispatch(new SetRowsPerPageAction(rowsPerPage || 10));
          (!skipRefresh) && dispatch(reloadAction);
        });
      }
      // selected:
    };
  };

  const createProperties = (state: IApplicationStoreState) => {
    return {
      ...selectState(state)
     }   
  }
  
  return {
    reloadAction: reloadAction,
    createActions: createActions,
    createProperties: createProperties,
    actionHandler: externalTableStateActionHandler
  }
}