@ercioko/meblotex-api 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/.eslintrc +6 -0
  2. package/.nvmrc +1 -0
  3. package/.prettierrc +11 -0
  4. package/package.json +5 -16
  5. package/src/api/Api.ts +140 -0
  6. package/src/api/Endpoint.ts +76 -0
  7. package/src/api/createApi.ts +116 -0
  8. package/src/api/error.ts +17 -0
  9. package/{dist/api/index.d.ts → src/api/index.ts} +1 -0
  10. package/src/api/status.ts +3 -0
  11. package/src/api/utils/getAlert.ts +75 -0
  12. package/src/app_types.ts +695 -0
  13. package/src/components/ApiHostProvider.tsx +13 -0
  14. package/src/components/StatusTag.tsx +83 -0
  15. package/src/components/Table/DataTableWrapper.tsx +28 -0
  16. package/src/components/Table/FloatingActions.tsx +152 -0
  17. package/src/components/Table/Spinner.tsx +35 -0
  18. package/src/components/Table/Summary.tsx +18 -0
  19. package/src/components/Table/Table.tsx +474 -0
  20. package/src/components/Table/addIndexToObject.ts +6 -0
  21. package/src/components/Table/areProvidedColumnsDifferent.ts +26 -0
  22. package/src/components/Table/clicked.js +12 -0
  23. package/src/components/Table/displayActions.tsx +19 -0
  24. package/src/components/Table/hooks/useFetchPage.tsx +83 -0
  25. package/src/components/Table/hooks/useGetCellRenderer.tsx +197 -0
  26. package/src/components/Table/hooks/useGetColumnsWithCheckbox.tsx +34 -0
  27. package/src/components/Table/hooks/useGetDeleteRow.tsx +37 -0
  28. package/src/components/Table/hooks/useGetResizeColumn.ts +33 -0
  29. package/src/components/Table/hooks/useGetRowCheckbox.tsx +25 -0
  30. package/src/components/Table/hooks/useLoadDataSource.ts +24 -0
  31. package/src/components/Table/hooks/usePageSize.tsx +20 -0
  32. package/src/components/Table/hooks/useRenderRowActions.tsx +45 -0
  33. package/src/components/Table/hooks/useResizeColumns.ts +61 -0
  34. package/src/components/Table/index.ts +1 -0
  35. package/src/components/Table/providers/OrderProvider.tsx +20 -0
  36. package/src/components/Table/styles.ts +208 -0
  37. package/src/config.ts +7 -0
  38. package/src/db_types.ts +220 -0
  39. package/src/hooks/useApi.ts +43 -0
  40. package/src/hooks/useEndpoint.ts +35 -0
  41. package/src/lib/jednostkaNazwa.ts +6 -0
  42. package/src/lib/typDokumentuNazwa.ts +8 -0
  43. package/src/utils/Query.ts +30 -0
  44. package/src/utils/getWZNumber.ts +13 -0
  45. package/src/utils/getZamowienieDokumentNumber.ts +11 -0
  46. package/{dist/src/utils/index.js → src/utils/index.ts} +2 -2
  47. package/tsconfig.json +25 -0
  48. package/tsconfig.tsbuildinfo +1 -0
  49. package/dist/api/Api.d.ts +0 -33
  50. package/dist/api/Api.js +0 -152
  51. package/dist/api/Endpoint.d.ts +0 -13
  52. package/dist/api/Endpoint.js +0 -126
  53. package/dist/api/createApi.d.ts +0 -52
  54. package/dist/api/createApi.js +0 -81
  55. package/dist/api/error.d.ts +0 -15
  56. package/dist/api/error.js +0 -18
  57. package/dist/api/index.js +0 -34
  58. package/dist/api/status.d.ts +0 -3
  59. package/dist/api/status.js +0 -6
  60. package/dist/api/utils/getAlert.d.ts +0 -6
  61. package/dist/api/utils/getAlert.js +0 -65
  62. package/dist/api/utils/index.js +0 -8
  63. package/dist/app_types.d.ts +0 -607
  64. package/dist/app_types.js +0 -75
  65. package/dist/components/ApiHostProvider.d.ts +0 -7
  66. package/dist/components/ApiHostProvider.js +0 -35
  67. package/dist/components/StatusTag.d.ts +0 -11
  68. package/dist/components/StatusTag.js +0 -86
  69. package/dist/components/Table/DataTableWrapper.d.ts +0 -3
  70. package/dist/components/Table/DataTableWrapper.js +0 -44
  71. package/dist/components/Table/FloatingActions.d.ts +0 -14
  72. package/dist/components/Table/FloatingActions.js +0 -32
  73. package/dist/components/Table/Spinner.d.ts +0 -2
  74. package/dist/components/Table/Spinner.js +0 -26
  75. package/dist/components/Table/Summary.js +0 -16
  76. package/dist/components/Table/Table.d.ts +0 -28
  77. package/dist/components/Table/Table.js +0 -330
  78. package/dist/components/Table/addIndexToObject.d.ts +0 -3
  79. package/dist/components/Table/addIndexToObject.js +0 -17
  80. package/dist/components/Table/areProvidedColumnsDifferent.d.ts +0 -2
  81. package/dist/components/Table/areProvidedColumnsDifferent.js +0 -16
  82. package/dist/components/Table/clicked.js +0 -11
  83. package/dist/components/Table/displayActions.js +0 -11
  84. package/dist/components/Table/hooks/useFetchPage.d.ts +0 -18
  85. package/dist/components/Table/hooks/useFetchPage.js +0 -123
  86. package/dist/components/Table/hooks/useGetCellRenderer.d.ts +0 -25
  87. package/dist/components/Table/hooks/useGetCellRenderer.js +0 -166
  88. package/dist/components/Table/hooks/useGetColumnsWithCheckbox.d.ts +0 -5
  89. package/dist/components/Table/hooks/useGetColumnsWithCheckbox.js +0 -53
  90. package/dist/components/Table/hooks/useGetDeleteRow.d.ts +0 -9
  91. package/dist/components/Table/hooks/useGetDeleteRow.js +0 -74
  92. package/dist/components/Table/hooks/useGetResizeColumn.d.ts +0 -8
  93. package/dist/components/Table/hooks/useGetResizeColumn.js +0 -28
  94. package/dist/components/Table/hooks/useGetRowCheckbox.d.ts +0 -5
  95. package/dist/components/Table/hooks/useGetRowCheckbox.js +0 -54
  96. package/dist/components/Table/hooks/useLoadDataSource.d.ts +0 -10
  97. package/dist/components/Table/hooks/useLoadDataSource.js +0 -20
  98. package/dist/components/Table/hooks/usePageSize.d.ts +0 -5
  99. package/dist/components/Table/hooks/usePageSize.js +0 -16
  100. package/dist/components/Table/hooks/useRenderRowActions.d.ts +0 -6
  101. package/dist/components/Table/hooks/useRenderRowActions.js +0 -51
  102. package/dist/components/Table/hooks/useResizeColumns.d.ts +0 -9
  103. package/dist/components/Table/hooks/useResizeColumns.js +0 -60
  104. package/dist/components/Table/index.d.ts +0 -1
  105. package/dist/components/Table/index.js +0 -8
  106. package/dist/components/Table/providers/OrderProvider.d.ts +0 -9
  107. package/dist/components/Table/providers/OrderProvider.js +0 -17
  108. package/dist/components/Table/styles.d.ts +0 -6
  109. package/dist/components/Table/styles.js +0 -37
  110. package/dist/components/index.js +0 -26
  111. package/dist/config.d.ts +0 -6
  112. package/dist/config.js +0 -8
  113. package/dist/db_types.d.ts +0 -208
  114. package/dist/db_types.js +0 -2
  115. package/dist/hooks/index.js +0 -10
  116. package/dist/hooks/useApi.d.ts +0 -2
  117. package/dist/hooks/useApi.js +0 -42
  118. package/dist/hooks/useEndpoint.d.ts +0 -2
  119. package/dist/hooks/useEndpoint.js +0 -34
  120. package/dist/index.js +0 -19
  121. package/dist/lib/index.js +0 -18
  122. package/dist/lib/jednostka.d.ts +0 -6
  123. package/dist/lib/jednostkaNazwa.d.ts +0 -6
  124. package/dist/lib/jednostkaNazwa.js +0 -9
  125. package/dist/lib/typDokumentuNazwa.d.ts +0 -6
  126. package/dist/lib/typDokumentuNazwa.js +0 -11
  127. package/dist/lib/typ_dokument.d.ts +0 -0
  128. package/dist/lib/typ_dokumentu.d.ts +0 -6
  129. package/dist/lib/unit.d.ts +0 -0
  130. package/dist/src/api/Api.js +0 -316
  131. package/dist/src/api/Endpoint.js +0 -365
  132. package/dist/src/api/createApi.js +0 -50
  133. package/dist/src/api/error.js +0 -15
  134. package/dist/src/api/index.js +0 -5
  135. package/dist/src/api/status.js +0 -3
  136. package/dist/src/api/utils/getAlert.js +0 -63
  137. package/dist/src/api/utils/index.js +0 -1
  138. package/dist/src/app_types.js +0 -72
  139. package/dist/src/components/ApiHostProvider.js +0 -11
  140. package/dist/src/components/StatusTag.js +0 -153
  141. package/dist/src/components/Table/DataTableWrapper.js +0 -115
  142. package/dist/src/components/Table/FloatingActions.js +0 -60
  143. package/dist/src/components/Table/Spinner.js +0 -40
  144. package/dist/src/components/Table/Summary.js +0 -16
  145. package/dist/src/components/Table/Table.js +0 -498
  146. package/dist/src/components/Table/addIndexToObject.js +0 -57
  147. package/dist/src/components/Table/areProvidedColumnsDifferent.js +0 -19
  148. package/dist/src/components/Table/clicked.js +0 -12
  149. package/dist/src/components/Table/displayActions.js +0 -11
  150. package/dist/src/components/Table/hooks/useFetchPage.js +0 -254
  151. package/dist/src/components/Table/hooks/useGetCellRenderer.js +0 -231
  152. package/dist/src/components/Table/hooks/useGetColumnsWithCheckbox.js +0 -33
  153. package/dist/src/components/Table/hooks/useGetDeleteRow.js +0 -181
  154. package/dist/src/components/Table/hooks/useGetResizeColumn.js +0 -72
  155. package/dist/src/components/Table/hooks/useGetRowCheckbox.js +0 -55
  156. package/dist/src/components/Table/hooks/useLoadDataSource.js +0 -16
  157. package/dist/src/components/Table/hooks/usePageSize.js +0 -15
  158. package/dist/src/components/Table/hooks/useRenderRowActions.js +0 -50
  159. package/dist/src/components/Table/hooks/useResizeColumns.js +0 -70
  160. package/dist/src/components/Table/index.js +0 -1
  161. package/dist/src/components/Table/providers/OrderProvider.js +0 -65
  162. package/dist/src/components/Table/styles.js +0 -66
  163. package/dist/src/components/index.js +0 -4
  164. package/dist/src/config.js +0 -6
  165. package/dist/src/db_types.js +0 -1
  166. package/dist/src/hooks/index.js +0 -2
  167. package/dist/src/hooks/useApi.js +0 -84
  168. package/dist/src/hooks/useEndpoint.js +0 -60
  169. package/dist/src/index.js +0 -3
  170. package/dist/src/lib/index.js +0 -2
  171. package/dist/src/lib/jednostkaNazwa.js +0 -6
  172. package/dist/src/lib/typDokumentuNazwa.js +0 -16
  173. package/dist/src/utils/Query.js +0 -73
  174. package/dist/src/utils/getWZNumber.js +0 -4
  175. package/dist/src/utils/getZamowienieDokumentNumber.js +0 -4
  176. package/dist/utils/AlertContext.d.ts +0 -3
  177. package/dist/utils/AlertContext.js +0 -3
  178. package/dist/utils/Query.d.ts +0 -6
  179. package/dist/utils/Query.js +0 -33
  180. package/dist/utils/geNumber.d.ts +0 -2
  181. package/dist/utils/getDocNumber.d.ts +0 -2
  182. package/dist/utils/getWZNumber.js +0 -10
  183. package/dist/utils/getWzNumber.d.ts +0 -2
  184. package/dist/utils/getZamowienieDokumentNumber copy.d.ts +0 -2
  185. package/dist/utils/getZamowienieDokumentNumber.d.ts +0 -2
  186. package/dist/utils/getZamowienieDokumentNumber.js +0 -8
  187. package/dist/utils/index.d.ts +0 -3
  188. package/dist/utils/index.js +0 -12
  189. /package/{dist/api/utils/index.d.ts → src/api/utils/index.ts} +0 -0
  190. /package/{dist/components/index.d.ts → src/components/index.ts} +0 -0
  191. /package/{dist/hooks/index.d.ts → src/hooks/index.ts} +0 -0
  192. /package/{dist/index.d.ts → src/index.ts} +0 -0
  193. /package/{dist/lib/index.d.ts → src/lib/index.ts} +0 -0
@@ -0,0 +1,474 @@
1
+ import { Spin, Empty } from 'antd';
2
+ import React, {
3
+ useCallback,
4
+ useEffect,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+ import { useSelector } from 'react-redux';
10
+ import { MultiGrid } from 'react-virtualized';
11
+ import config from '../../config';
12
+ import {
13
+ Actions,
14
+ DataRow,
15
+ TableColumn,
16
+ loadPage as loadPageType,
17
+ State,
18
+ UseData,
19
+ } from '../../app_types';
20
+ import Spinner from './Spinner';
21
+ import useGetRowCheckbox from './hooks/useGetRowCheckbox';
22
+ import useGetCellRenderer from './hooks/useGetCellRenderer';
23
+ import useGetResizeColumn from './hooks/useGetResizeColumn';
24
+ import useRenderRowActions from './hooks/useRenderRowActions';
25
+ import addIndexToObject from './addIndexToObject';
26
+ import useGetColumnsWithCheckbox from './hooks/useGetColumnsWithCheckbox';
27
+ import useDeleteRow from './hooks/useGetDeleteRow';
28
+ import useResizeColumns from './hooks/useResizeColumns';
29
+ import useLoadDataSource from './hooks/useLoadDataSource';
30
+ import areProvidedColumnsDifferent from './areProvidedColumnsDifferent';
31
+ import useFetchPage from './hooks/useFetchPage';
32
+ import FloatingActions from './FloatingActions';
33
+ import DataTableWrapper from './DataTableWrapper';
34
+ import { DataTable, StyledTable } from './styles';
35
+ import { ApiResponse } from '../../api/Api';
36
+ import { usePageSize } from './hooks/usePageSize';
37
+ import { OrderProvider, useOrder } from './providers/OrderProvider';
38
+
39
+ const { ROW_SIZE, ROW_SIZE_MOBILE, MOBILE_BREAKPOINT } = config;
40
+
41
+ const getSearchBarCollapsed = (s: State) => s.searchBarCollapsed;
42
+
43
+ export const MARGIN_FOR_SPINNER = 55;
44
+
45
+ type Props<DataType = DataRow> = {
46
+ idKey?: keyof DataType;
47
+ added?: DataType[];
48
+ style?: React.CSSProperties;
49
+ columns: TableColumn<DataType>[];
50
+ filters?: Record<string, unknown> | undefined;
51
+ actions?: Actions<DataType>;
52
+ loadPage?: loadPageType<DataType>;
53
+ dataSource?: DataType[];
54
+ onDelete?: (row: DataType) => Promise<ApiResponse> | void;
55
+ selected?: number | DataType;
56
+ withCheckbox?: boolean;
57
+ checked?: number[];
58
+ setChecked?: React.Dispatch<React.SetStateAction<number[]>>;
59
+ suspend?: boolean;
60
+ pinned?: DataType[];
61
+ loadingDataSource?: boolean;
62
+ floatingActions?: boolean;
63
+ useData?: UseData<DataType>;
64
+ responsive?: boolean;
65
+ deleteMessage?: (record: DataType) => string;
66
+ };
67
+
68
+ function BaseTable<DataType extends object = DataRow>({
69
+ style,
70
+ columns: providedColumns,
71
+ filters,
72
+ actions,
73
+ loadPage,
74
+ onDelete,
75
+ added,
76
+ dataSource,
77
+ withCheckbox,
78
+ checked,
79
+ setChecked,
80
+ selected,
81
+ loadingDataSource = false,
82
+ floatingActions,
83
+ suspend,
84
+ pinned,
85
+ useData = useState<DataType[]>,
86
+ responsive,
87
+ deleteMessage,
88
+ idKey,
89
+ }: Props<DataType>): JSX.Element {
90
+ const { order } = useOrder();
91
+ const [rowSize, setRowSize] = useState<number>(ROW_SIZE);
92
+ const main = useRef<HTMLDivElement>(null);
93
+ const table = useRef<HTMLDivElement>(null);
94
+ const grid = useRef<MultiGrid>(null);
95
+ const [listWidth, setListWidth] = useState<number>(
96
+ main.current?.offsetWidth || 0
97
+ );
98
+ const [listHeight, setListHeight] = useState(
99
+ (main.current?.offsetHeight || 0) - rowSize
100
+ );
101
+ const [hoveredRow, setHoveredRow] = useState(); // TODO: is it used anywhere?
102
+ const [data, setData] = useData(
103
+ dataSource?.map(addIndexToObject<DataType>) || undefined
104
+ );
105
+ const [total, setTotal] = useState<number>();
106
+ const [columns, setColumns] = useState(providedColumns);
107
+ const [loading, setLoading] = useState(false);
108
+ const [actionsFor, setActionsFor] = useState<undefined>();
109
+ const [actionsDimensions, setActionsDimensions] = useState({
110
+ top: -20,
111
+ width: 0,
112
+ transition: '',
113
+ });
114
+ const collapsed = useSelector(getSearchBarCollapsed);
115
+ const menuCollapsed = useSelector<State>((state) => state.menuCollapsed);
116
+
117
+ useEffect(() => {
118
+ setLoading(loadingDataSource);
119
+ }, [loadingDataSource]);
120
+
121
+ const deleteRow = useDeleteRow<DataType>({
122
+ deleteMessage,
123
+ onDelete,
124
+ setData,
125
+ data,
126
+ });
127
+
128
+ const resizeCol = useGetResizeColumn<DataType>({ columns, setColumns });
129
+
130
+ const CellRenderer = useGetCellRenderer<DataType>({
131
+ idKey,
132
+ columns,
133
+ // TODO: improve
134
+ data: data as (DataType & { index: number })[],
135
+ actions,
136
+ resizeCol,
137
+ setHoveredRow,
138
+ hoveredRow,
139
+ selected:
140
+ typeof selected === 'object'
141
+ ? (selected as unknown as { id: number })?.id
142
+ : selected, // TODO fix types
143
+ checked,
144
+ });
145
+
146
+ const RowActions = useRenderRowActions({ data, actions, deleteRow });
147
+
148
+ const getColumnsWithActions = useCallback(
149
+ (updatedColumns) => {
150
+ const newUpdatedColumns = [...updatedColumns];
151
+
152
+ if (newUpdatedColumns[newUpdatedColumns.length - 1].key === 'akcje') {
153
+ newUpdatedColumns[newUpdatedColumns.length - 1] = {
154
+ ...newUpdatedColumns[newUpdatedColumns.length - 1],
155
+ render: RowActions,
156
+ };
157
+ } else if (actions.width) {
158
+ newUpdatedColumns.push({
159
+ title: 'Akcja',
160
+ width: actions.width,
161
+ key: 'akcje',
162
+ render: RowActions,
163
+ });
164
+ }
165
+ return newUpdatedColumns;
166
+ },
167
+ [RowActions, actions]
168
+ );
169
+
170
+ const RowCheckbox = useGetRowCheckbox({
171
+ checked,
172
+ setChecked,
173
+ });
174
+
175
+ const getColumnsWithCheckbox = useGetColumnsWithCheckbox({
176
+ data,
177
+ checked,
178
+ RowCheckbox,
179
+ });
180
+
181
+ const addCheckboxAndActions = useCallback(() => {
182
+ let updatedColumns = [...columns];
183
+ if (withCheckbox) {
184
+ updatedColumns = getColumnsWithCheckbox(updatedColumns);
185
+ }
186
+
187
+ if (!floatingActions && actions && actions.width) {
188
+ updatedColumns = getColumnsWithActions(updatedColumns);
189
+ }
190
+
191
+ if (withCheckbox || (!floatingActions && actions && actions.width)) {
192
+ // if (JSON.stringify(updatedColumns) !== JSON.stringify(columns)) {
193
+ setColumns(updatedColumns);
194
+ // }
195
+ }
196
+ }, [
197
+ getColumnsWithCheckbox,
198
+ getColumnsWithActions,
199
+ // columns,
200
+ withCheckbox,
201
+ actions,
202
+ floatingActions,
203
+ ]);
204
+
205
+ useEffect(() => {
206
+ addCheckboxAndActions();
207
+ }, [addCheckboxAndActions, checked, data]);
208
+
209
+ const resizeColumns = useResizeColumns({
210
+ providedColumns,
211
+ columns,
212
+ main,
213
+ floatingActions,
214
+ actions,
215
+ setColumns,
216
+ grid,
217
+ });
218
+
219
+ useEffect(() => {
220
+ resizeColumns();
221
+ }, [resizeColumns, columns, collapsed, menuCollapsed, listWidth, listHeight]);
222
+
223
+ useEffect(() => {
224
+ setListHeight(main.current?.offsetHeight || 0 - rowSize);
225
+ }, [collapsed]);
226
+
227
+ const pageSize = usePageSize({ mainRef: main, rowSize });
228
+
229
+ const handleResize = useCallback(() => {
230
+ if (main.current) {
231
+ if (responsive) {
232
+ setRowSize(
233
+ main.current.offsetWidth < MOBILE_BREAKPOINT
234
+ ? ROW_SIZE_MOBILE
235
+ : ROW_SIZE
236
+ );
237
+ }
238
+ setListWidth(main.current.offsetWidth);
239
+ setListHeight(main.current.offsetHeight - rowSize);
240
+ }
241
+ }, [main]);
242
+
243
+ useEffect(() => {
244
+ handleResize();
245
+ }, [handleResize, menuCollapsed]);
246
+
247
+ useEffect(() => {
248
+ if (main.current) {
249
+ if (responsive) {
250
+ setRowSize(
251
+ main.current.offsetWidth < MOBILE_BREAKPOINT
252
+ ? ROW_SIZE_MOBILE
253
+ : ROW_SIZE
254
+ );
255
+ }
256
+ setListWidth(main.current.offsetWidth);
257
+ setListHeight(main.current.offsetHeight - rowSize);
258
+ }
259
+
260
+ window.removeEventListener('resize', handleResize);
261
+ window.addEventListener('resize', handleResize);
262
+ return () => window.removeEventListener('resize', handleResize);
263
+ }, [handleResize]);
264
+
265
+ useEffect(() => {
266
+ if (areProvidedColumnsDifferent(providedColumns, columns)) {
267
+ setColumns(providedColumns);
268
+ }
269
+ }, [providedColumns, columns]);
270
+
271
+ const floatingButtons = useMemo(() => {
272
+ if (actions) {
273
+ return [...(actions.floatingOnly || []), ...(actions.buttons || [])];
274
+ }
275
+ return undefined;
276
+ }, [actions]);
277
+
278
+ const fetchPage = useFetchPage({
279
+ suspend,
280
+ setLoading,
281
+ pageSize,
282
+ loadPage,
283
+ filters,
284
+ // TODO: improve
285
+ data: data as (DataType & { index: number })[],
286
+ pinned,
287
+ setData,
288
+ setTotal,
289
+ });
290
+
291
+ useLoadDataSource({
292
+ filters,
293
+ added,
294
+ data,
295
+ dataSource,
296
+ setData,
297
+ fetchPage,
298
+ pageSize,
299
+ loadPage,
300
+ });
301
+
302
+ const hide = useCallback(
303
+ (e?: MouseEvent) => {
304
+ if (!actionsFor) {
305
+ return;
306
+ }
307
+ if (e) {
308
+ e.stopPropagation();
309
+ }
310
+
311
+ setActionsFor(undefined);
312
+ setActionsDimensions({
313
+ ...actionsDimensions,
314
+ width: 0,
315
+ transition: '',
316
+ });
317
+ },
318
+ [actionsFor, actionsDimensions]
319
+ );
320
+
321
+ const scrolled = useCallback(
322
+ ({ scrollTop, clientHeight }) => {
323
+ if (actionsFor) {
324
+ hide();
325
+ }
326
+ if (dataSource !== undefined || loading || data.length === total) {
327
+ return;
328
+ }
329
+
330
+ const page = Math.ceil(
331
+ (data.length - (pinned?.length || 0)) / pageSize()
332
+ );
333
+
334
+ if (scrollTop < clientHeight * (page - 1)) {
335
+ return;
336
+ }
337
+
338
+ fetchPage(page);
339
+ },
340
+ [
341
+ fetchPage,
342
+ actionsFor,
343
+ dataSource,
344
+ data,
345
+ loading,
346
+ total,
347
+ pageSize,
348
+ hide,
349
+ pinned?.length,
350
+ ]
351
+ );
352
+
353
+ useEffect(() => {
354
+ if (loadPage !== undefined) {
355
+ fetchPage(0);
356
+ }
357
+ }, [order]);
358
+
359
+ useEffect(() => {
360
+ window.addEventListener('click', hide);
361
+
362
+ return () => window.removeEventListener('click', hide);
363
+ }, [hide, actionsFor, actionsDimensions]);
364
+
365
+ const actionHover = useCallback(() => {
366
+ if (Array.isArray(floatingButtons)) {
367
+ setActionsDimensions({
368
+ ...actionsDimensions,
369
+ width:
370
+ floatingButtons.length * 24 +
371
+ (floatingButtons.length - 1) * 28 +
372
+ (actions.delete ? 53 : 0) +
373
+ 98 +
374
+ (actions.additionalWidth || 0),
375
+ transition: 'width 150ms ease-in-out',
376
+ });
377
+ }
378
+ }, [actionsDimensions, floatingButtons, actions]);
379
+
380
+ const actionLeave = useCallback(() => {
381
+ if (Array.isArray(floatingButtons) && actionsFor) {
382
+ setActionsDimensions({
383
+ ...actionsDimensions,
384
+ width:
385
+ floatingButtons.length * 24 +
386
+ (floatingButtons.length - 1) * 28 +
387
+ (actions.delete ? 53 : 0) +
388
+ 74,
389
+ });
390
+ }
391
+ }, [actionsFor, actionsDimensions, floatingButtons, actions]);
392
+
393
+ const isVisibleCellRenderer = useCallback(
394
+ ({
395
+ columnIndex,
396
+ key,
397
+ rowIndex,
398
+ style: cellStyle,
399
+ isVisible,
400
+ isScrolling,
401
+ }) => {
402
+ if (isVisible) {
403
+ return (
404
+ <CellRenderer
405
+ isVisible={isVisible}
406
+ columnIndex={columnIndex}
407
+ key={key}
408
+ rowKey={key}
409
+ rowIndex={rowIndex}
410
+ isScrolling={isScrolling}
411
+ style={cellStyle}
412
+ />
413
+ );
414
+ }
415
+ return null;
416
+ },
417
+ [CellRenderer]
418
+ );
419
+ return (
420
+ <DataTable style={style} className="data-table" ref={main}>
421
+ {(actions?.both || floatingActions) && actions ? (
422
+ <FloatingActions<DataType>
423
+ actionsDimensions={actionsDimensions}
424
+ actionsFor={actionsFor}
425
+ floatingButtons={floatingButtons}
426
+ actionHover={actionHover}
427
+ actionLeave={actionLeave}
428
+ hide={hide}
429
+ actions={actions}
430
+ />
431
+ ) : undefined}
432
+
433
+ <DataTableWrapper>
434
+ <StyledTable className="table" ref={table}>
435
+ {data && (
436
+ <MultiGrid
437
+ cellRenderer={isVisibleCellRenderer}
438
+ height={listHeight}
439
+ width={listWidth}
440
+ columnWidth={({ index }) => columns[index].width}
441
+ rowHeight={rowSize}
442
+ fixedRowCount={1}
443
+ columnCount={columns.length}
444
+ rowCount={data.length + 1}
445
+ overscanRowCount={22}
446
+ styleBottomLeftGrid={{ outline: 'none' }}
447
+ styleBottomRightGrid={{ outline: 'none' }}
448
+ onScroll={scrolled}
449
+ ref={grid}
450
+ />
451
+ )}
452
+ </StyledTable>
453
+
454
+ {data && loading ? <Spinner /> : undefined}
455
+ </DataTableWrapper>
456
+ {/* <div className="data-table-wrapper" ref={wrapper} /> */}
457
+ {!data ? (
458
+ <div className="data-table-no-data">
459
+ {loading ? <Spin /> : <Empty description="Brak danych" />}
460
+ </div>
461
+ ) : undefined}
462
+ </DataTable>
463
+ );
464
+ }
465
+
466
+ function Table<DataType extends object = DataRow>(props: Props<DataType>) {
467
+ return (
468
+ <OrderProvider>
469
+ <BaseTable<DataType> {...props} />
470
+ </OrderProvider>
471
+ );
472
+ }
473
+
474
+ export default Table;
@@ -0,0 +1,6 @@
1
+ export default function addIndexToObject<DataType = Record<string, unknown>>(e: DataType, i: number) {
2
+ return {
3
+ ...e,
4
+ index: i + 1,
5
+ };
6
+ }
@@ -0,0 +1,26 @@
1
+ import { DataRow, TableColumn } from '../../app_types';
2
+
3
+ export default function areProvidedColumnsDifferent<DataType = DataRow>(
4
+ providedColumns: TableColumn<DataType>[],
5
+ currentColumns: TableColumn<DataType>[]
6
+ ) {
7
+ const providedKeys = providedColumns.map((c) => String(c.key).toLowerCase());
8
+ const currentKeys = currentColumns.map((c) => String(c.key).toLowerCase());
9
+ const currentHasActions = currentKeys.includes('akcje');
10
+ const providedHasActions = providedKeys.includes('akcje');
11
+
12
+ const currentHasCheckbox = currentKeys.includes('checkbox');
13
+ const providedHasCheckbox = providedKeys.includes('checkbox');
14
+
15
+ const providedKeysWithAdditional = providedKeys.concat(
16
+ currentHasActions && !providedHasActions ? ['akcje'] : []
17
+ );
18
+
19
+ if (currentHasCheckbox && !providedHasCheckbox) {
20
+ providedKeysWithAdditional.unshift('checkbox');
21
+ }
22
+
23
+ return (
24
+ JSON.stringify(providedKeysWithAdditional) !== JSON.stringify(currentKeys)
25
+ );
26
+ }
@@ -0,0 +1,12 @@
1
+ import { displayActions } from './displayActions';
2
+
3
+ const clicked = (e, row, type) => {
4
+ if (type === 0) {
5
+ // Left-click
6
+ if (actionsFor) hide();
7
+ else if (withCheckbox) onChecked(row, checked.indexOf(row) === -1);
8
+ } else if (type === 1) {
9
+ // Right-click
10
+ if (actions?.both || floatingActions) displayActions(e, row);
11
+ }
12
+ };
@@ -0,0 +1,19 @@
1
+ // TODO: remove?
2
+ import { ROW_SIZE } from './index';
3
+
4
+ export default function displayActions(e, row) {
5
+ if (actionsFor) e.stopPropagation();
6
+
7
+ setActionsFor(row);
8
+ setActionsDimensions({
9
+ top:
10
+ (data ? (ROW_SIZE + 1) * data.indexOf(row) - 50 : -50) -
11
+ wrapper.current.scrollTop,
12
+ width:
13
+ floatingButtons.length * 24 +
14
+ (floatingButtons.length - 1) * 28 +
15
+ (actions.delete ? 53 : 0) +
16
+ 74,
17
+ transition: actionsFor ? 'top 150ms ease-in-out' : undefined,
18
+ });
19
+ }
@@ -0,0 +1,83 @@
1
+ import { message } from 'antd';
2
+ import React, { useCallback } from 'react';
3
+ import addIndexToObject from '../addIndexToObject';
4
+ import { useOrder } from '../providers/OrderProvider';
5
+ import { loadPage as loadPageType } from '../../../app_types';
6
+
7
+ type Props<DataType extends object> = {
8
+ suspend?: boolean;
9
+ setLoading?: (v: boolean) => void;
10
+ pageSize: () => number;
11
+ loadPage?: loadPageType<DataType>;
12
+ filters;
13
+ data: (DataType & { index: number })[];
14
+ pinned: DataType[];
15
+ setData: (rows: (DataType & { index: number })[]) => void;
16
+ setTotal: (total: number) => void;
17
+ };
18
+
19
+ export default function useFetchPage<DataType extends object>({
20
+ suspend,
21
+ setLoading,
22
+ pageSize,
23
+ loadPage,
24
+ filters,
25
+ data,
26
+ pinned,
27
+ setData,
28
+ setTotal,
29
+ }: Props<DataType>) {
30
+ const { order } = useOrder();
31
+
32
+ return useCallback(
33
+ async (page: number, size?: number) => {
34
+ if (typeof suspend === 'boolean' && suspend) {
35
+ setLoading(false);
36
+ return;
37
+ }
38
+
39
+ if (size === undefined) {
40
+ size = pageSize();
41
+ }
42
+ try {
43
+ setLoading(true);
44
+ const loaded = await loadPage(page, size, filters, order);
45
+ let newData;
46
+ if (page !== 0) {
47
+ newData = [...(data || []), ...loaded.data];
48
+ } else {
49
+ newData = loaded.data || [];
50
+ }
51
+
52
+ if (Array.isArray(pinned)) {
53
+ pinned.forEach((pinnedEl, pinnedIdx) => {
54
+ const indexes = newData
55
+ .map(({ id }, index) => ({ id, index }))
56
+ // TODO fix
57
+ // @ts-ignore
58
+ .filter((el) => el.id === pinnedEl.id)
59
+ .map(({ index }) => index);
60
+
61
+ indexes.forEach((index) => {
62
+ if (index !== pinnedIdx) {
63
+ newData.splice(index, 1);
64
+ }
65
+ });
66
+
67
+ if (!indexes.includes(pinnedIdx)) {
68
+ newData.unshift(pinnedEl);
69
+ }
70
+ });
71
+ }
72
+
73
+ setData(newData.map(addIndexToObject));
74
+ setTotal(loaded.total);
75
+ } catch (e) {
76
+ message.error(`${e}`);
77
+ } finally {
78
+ setLoading(false);
79
+ }
80
+ },
81
+ [suspend, pageSize, loadPage, filters, data, order]
82
+ );
83
+ }