@addev-be/ui 0.17.2 → 0.17.3

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 (264) hide show
  1. package/assets/icons/arrow-down-1-9.svg +1 -1
  2. package/assets/icons/arrow-down-a-z.svg +1 -1
  3. package/assets/icons/arrow-up-z-a.svg +1 -1
  4. package/assets/icons/check.svg +1 -1
  5. package/assets/icons/circle-check.svg +1 -1
  6. package/assets/icons/down.svg +1 -1
  7. package/assets/icons/filter-full.svg +1 -1
  8. package/assets/icons/filter.svg +1 -1
  9. package/assets/icons/hashtag.svg +1 -1
  10. package/assets/icons/image-slash.svg +1 -1
  11. package/assets/icons/left.svg +1 -1
  12. package/assets/icons/magnifier.svg +1 -1
  13. package/assets/icons/phone.svg +1 -1
  14. package/assets/icons/right.svg +1 -1
  15. package/assets/icons/sort-calendar-ascending.svg +5 -5
  16. package/assets/icons/spinner-third.svg +1 -1
  17. package/assets/icons/table-columns.svg +1 -1
  18. package/assets/icons/table-footer-slash.svg +4 -4
  19. package/assets/icons/table-footer.svg +3 -3
  20. package/assets/icons/table.svg +1 -1
  21. package/assets/icons/up.svg +1 -1
  22. package/assets/icons/user-tie.svg +1 -1
  23. package/assets/icons/x-bar.svg +3 -3
  24. package/dist/components/data/DataGrid/styles.d.ts +1 -1
  25. package/dist/components/data/DataGrid/styles.js +4 -4
  26. package/dist/components/forms/Form/Checkbox.d.ts +2 -2
  27. package/dist/components/forms/Form/Checkbox.js +2 -2
  28. package/dist/components/forms/Form/Input.d.ts +2 -2
  29. package/dist/components/forms/Form/Input.js +3 -2
  30. package/dist/components/forms/Form/Row.d.ts +9 -0
  31. package/dist/components/forms/Form/Row.js +10 -0
  32. package/dist/components/forms/Form/Select.d.ts +1 -0
  33. package/dist/components/forms/Form/Select.js +5 -4
  34. package/dist/components/forms/Form/TextArea.d.ts +2 -2
  35. package/dist/components/forms/Form/TextArea.js +3 -2
  36. package/dist/components/forms/Form/index.d.ts +22 -8
  37. package/dist/components/forms/Form/index.js +6 -1
  38. package/dist/components/forms/Form/styles.d.ts +9 -4
  39. package/dist/components/forms/Form/styles.js +13 -11
  40. package/dist/components/forms/NumberInput.d.ts +9 -0
  41. package/dist/components/forms/NumberInput.js +41 -0
  42. package/dist/components/forms/index.d.ts +2 -1
  43. package/dist/components/forms/index.js +2 -1
  44. package/dist/components/ui/ToastNotification.d.ts +14 -0
  45. package/dist/components/ui/ToastNotification.js +78 -0
  46. package/dist/hooks/index.d.ts +1 -0
  47. package/dist/hooks/index.js +1 -0
  48. package/dist/hooks/useMutableState.d.ts +2 -0
  49. package/dist/hooks/useMutableState.js +16 -0
  50. package/dist/poppins-700.woff2 +0 -0
  51. package/package.json +1 -1
  52. package/src/Icons.tsx +134 -134
  53. package/src/components/auth/LoginForm.tsx +84 -84
  54. package/src/components/auth/LoginPage.tsx +32 -32
  55. package/src/components/auth/PasswordRecoveryForm.tsx +52 -52
  56. package/src/components/auth/PasswordResetForm.tsx +112 -112
  57. package/src/components/auth/index.ts +4 -4
  58. package/src/components/auth/styles.ts +14 -14
  59. package/src/components/data/DataGrid/DataGridCell.tsx +77 -77
  60. package/src/components/data/DataGrid/DataGridColumnsModal/helpers.ts +9 -9
  61. package/src/components/data/DataGrid/DataGridColumnsModal/hooks.tsx +59 -59
  62. package/src/components/data/DataGrid/DataGridColumnsModal/index.tsx +182 -182
  63. package/src/components/data/DataGrid/DataGridColumnsModal/styles.ts +104 -104
  64. package/src/components/data/DataGrid/DataGridEditableCell/CheckboxEditableCell.tsx +38 -38
  65. package/src/components/data/DataGrid/DataGridEditableCell/DateEditableCell.tsx +39 -39
  66. package/src/components/data/DataGrid/DataGridEditableCell/NumberEditableCell.tsx +68 -68
  67. package/src/components/data/DataGrid/DataGridEditableCell/TextEditableCell.tsx +38 -38
  68. package/src/components/data/DataGrid/DataGridEditableCell/index.tsx +106 -106
  69. package/src/components/data/DataGrid/DataGridEditableCell/styles.ts +35 -35
  70. package/src/components/data/DataGrid/DataGridEditableCell/types.ts +19 -19
  71. package/src/components/data/DataGrid/DataGridFilterMenu/FilterValuesScroller.tsx +131 -131
  72. package/src/components/data/DataGrid/DataGridFilterMenu/hooks.tsx +81 -81
  73. package/src/components/data/DataGrid/DataGridFilterMenu/index.tsx +376 -376
  74. package/src/components/data/DataGrid/DataGridFilterMenu/styles.ts +97 -97
  75. package/src/components/data/DataGrid/DataGridFooter.tsx +45 -45
  76. package/src/components/data/DataGrid/DataGridHeader.tsx +185 -185
  77. package/src/components/data/DataGrid/DataGridHeaderCell.tsx +111 -111
  78. package/src/components/data/DataGrid/DataGridRowTemplate.tsx +82 -82
  79. package/src/components/data/DataGrid/FilterModalContent/index.tsx +136 -136
  80. package/src/components/data/DataGrid/FilterModalContent/styles.ts +22 -22
  81. package/src/components/data/DataGrid/constants.ts +6 -6
  82. package/src/components/data/DataGrid/helpers/columns.tsx +386 -386
  83. package/src/components/data/DataGrid/helpers/filters.ts +287 -287
  84. package/src/components/data/DataGrid/helpers/index.ts +2 -2
  85. package/src/components/data/DataGrid/hooks/index.ts +29 -29
  86. package/src/components/data/DataGrid/hooks/useDataGrid.tsx +381 -381
  87. package/src/components/data/DataGrid/hooks/useDataGridChangedRows.ts +97 -97
  88. package/src/components/data/DataGrid/hooks/useDataGridCopy.ts +174 -174
  89. package/src/components/data/DataGrid/hooks/useDataGridSettings.ts +48 -48
  90. package/src/components/data/DataGrid/hooks/useRefreshModal.tsx +48 -48
  91. package/src/components/data/DataGrid/index.tsx +112 -112
  92. package/src/components/data/DataGrid/styles.ts +379 -379
  93. package/src/components/data/DataGrid/types.ts +369 -369
  94. package/src/components/data/SqlRequestDataGrid/SqlRequestForeignListEditableCell.tsx +16 -16
  95. package/src/components/data/SqlRequestDataGrid/helpers/columns.tsx +481 -481
  96. package/src/components/data/SqlRequestDataGrid/helpers/index.ts +2 -2
  97. package/src/components/data/SqlRequestDataGrid/helpers/sqlRequests.ts +16 -16
  98. package/src/components/data/SqlRequestDataGrid/index.tsx +403 -403
  99. package/src/components/data/SqlRequestDataGrid/styles.ts +15 -15
  100. package/src/components/data/SqlRequestDataGrid/types.ts +74 -74
  101. package/src/components/data/SqlRequestForeignList/index.tsx +259 -259
  102. package/src/components/data/SqlRequestForeignList/styles.ts +43 -43
  103. package/src/components/data/SqlRequestForeignList/types.ts +34 -34
  104. package/src/components/data/SqlRequestGrid/filters/FiltersSidebar.tsx +110 -110
  105. package/src/components/data/SqlRequestGrid/filters/styles.ts +88 -88
  106. package/src/components/data/SqlRequestGrid/helpers/index.ts +1 -1
  107. package/src/components/data/SqlRequestGrid/helpers/sqlRequests.ts +16 -16
  108. package/src/components/data/SqlRequestGrid/index.tsx +316 -316
  109. package/src/components/data/SqlRequestGrid/styles.ts +20 -20
  110. package/src/components/data/SqlRequestGrid/types.ts +75 -75
  111. package/src/components/data/VirtualScroller/hooks.ts +71 -71
  112. package/src/components/data/VirtualScroller/index.tsx +91 -91
  113. package/src/components/data/VirtualScroller/styles.ts +58 -58
  114. package/src/components/data/VirtualScroller/types.ts +12 -12
  115. package/src/components/data/index.ts +16 -16
  116. package/src/components/forms/AutoTextArea.tsx +41 -41
  117. package/src/components/forms/Button.tsx +133 -133
  118. package/src/components/forms/Form/Checkbox.tsx +12 -12
  119. package/src/components/forms/Form/FormGroup.tsx +31 -27
  120. package/src/components/forms/Form/Input.tsx +16 -16
  121. package/src/components/forms/Form/Row.tsx +32 -32
  122. package/src/components/forms/Form/Select.tsx +81 -81
  123. package/src/components/forms/Form/TextArea.tsx +17 -17
  124. package/src/components/forms/Form/index.tsx +45 -45
  125. package/src/components/forms/Form/styles.ts +180 -171
  126. package/src/components/forms/IconButton.tsx +57 -57
  127. package/src/components/forms/IndeterminateCheckbox.tsx +46 -46
  128. package/src/components/forms/NumberInput.tsx +53 -53
  129. package/src/components/forms/Select.tsx +35 -35
  130. package/src/components/forms/VerticalLabel.tsx +20 -20
  131. package/src/components/forms/index.ts +9 -9
  132. package/src/components/forms/styles.ts +29 -29
  133. package/src/components/index.ts +6 -6
  134. package/src/components/layout/Columns.ts +24 -20
  135. package/src/components/layout/Dropdown/index.tsx +113 -113
  136. package/src/components/layout/Dropdown/styles.ts +45 -45
  137. package/src/components/layout/Grid/index.tsx +8 -8
  138. package/src/components/layout/Grid/styles.ts +34 -34
  139. package/src/components/layout/Loading/index.tsx +29 -29
  140. package/src/components/layout/Loading/styles.ts +29 -29
  141. package/src/components/layout/Masonry/index.tsx +29 -29
  142. package/src/components/layout/Masonry/styles.ts +20 -20
  143. package/src/components/layout/Modal/index.tsx +51 -51
  144. package/src/components/layout/Modal/styles.ts +125 -125
  145. package/src/components/layout/index.ts +6 -6
  146. package/src/components/search/HighlightedText.tsx +41 -41
  147. package/src/components/search/QuickSearchBar.tsx +109 -109
  148. package/src/components/search/QuickSearchResults.tsx +86 -86
  149. package/src/components/search/index.ts +9 -9
  150. package/src/components/search/styles.ts +96 -96
  151. package/src/components/search/types.ts +26 -26
  152. package/src/components/ui/Avatar/index.tsx +54 -54
  153. package/src/components/ui/Avatar/styles.ts +61 -61
  154. package/src/components/ui/Card/index.tsx +14 -14
  155. package/src/components/ui/Card/styles.ts +37 -37
  156. package/src/components/ui/ContextMenu/index.tsx +79 -79
  157. package/src/components/ui/ContextMenu/styles.ts +119 -119
  158. package/src/components/ui/Ellipsis.tsx +34 -34
  159. package/src/components/ui/Message/index.tsx +57 -57
  160. package/src/components/ui/Message/styles.ts +40 -40
  161. package/src/components/ui/index.ts +6 -6
  162. package/src/config/index.ts +14 -14
  163. package/src/helpers/dates.ts +17 -17
  164. package/src/helpers/getScrollbarSize.ts +14 -14
  165. package/src/helpers/index.ts +4 -4
  166. package/src/helpers/numbers.ts +63 -63
  167. package/src/helpers/responsive.ts +83 -83
  168. package/src/helpers/text.ts +13 -13
  169. package/src/hooks/index.ts +7 -7
  170. package/src/hooks/useContainerMediaQuery.ts +7 -7
  171. package/src/hooks/useElementSize.ts +24 -24
  172. package/src/hooks/useMediaQuery.ts +9 -9
  173. package/src/hooks/useMediaQueryForWidth.ts +35 -35
  174. package/src/hooks/useMutableState.ts +19 -19
  175. package/src/hooks/useShowArchived.ts +28 -28
  176. package/src/hooks/useWindowSize.ts +20 -20
  177. package/src/index.ts +9 -9
  178. package/src/providers/AuthenticationProvider/helpers.ts +3 -3
  179. package/src/providers/AuthenticationProvider/index.tsx +243 -243
  180. package/src/providers/LoadingProvider/index.tsx +47 -47
  181. package/src/providers/PortalsProvider/index.tsx +54 -54
  182. package/src/providers/PortalsProvider/styles.ts +31 -31
  183. package/src/providers/SettingsProvider/index.tsx +70 -70
  184. package/src/providers/ThemeProvider/ThemeProvider.ts +63 -63
  185. package/src/providers/ThemeProvider/defaultTheme.ts +457 -457
  186. package/src/providers/ThemeProvider/helpers.ts +11 -11
  187. package/src/providers/ThemeProvider/index.ts +4 -4
  188. package/src/providers/ThemeProvider/types.ts +131 -131
  189. package/src/providers/TrackingProvider/hooks.ts +14 -14
  190. package/src/providers/TrackingProvider/index.tsx +71 -71
  191. package/src/providers/UiProviders/index.tsx +68 -68
  192. package/src/providers/UiProviders/styles.ts +10 -10
  193. package/src/providers/hooks.ts +12 -12
  194. package/src/providers/index.ts +8 -8
  195. package/src/services/HttpService.ts +92 -92
  196. package/src/services/WebSocketService.ts +137 -137
  197. package/src/services/advancedRequests.ts +102 -102
  198. package/src/services/base.ts +23 -23
  199. package/src/services/globalSearch.ts +29 -29
  200. package/src/services/hooks.ts +44 -44
  201. package/src/services/index.ts +21 -21
  202. package/src/services/requests/auth.ts +44 -44
  203. package/src/services/requests/generic.ts +62 -62
  204. package/src/services/requests/tracking.ts +12 -12
  205. package/src/services/requests/userProfiles.ts +35 -35
  206. package/src/services/requests/users.ts +28 -28
  207. package/src/services/smartQueries.ts +122 -122
  208. package/src/services/sqlRequests.ts +111 -111
  209. package/src/services/types/auth.ts +131 -131
  210. package/src/services/types/base.ts +10 -10
  211. package/src/services/types/generic.ts +96 -96
  212. package/src/services/types/tracking.ts +39 -39
  213. package/src/services/types/userProfiles.ts +107 -107
  214. package/src/services/types/users.ts +106 -106
  215. package/src/services/updateSqlRequests.ts +34 -34
  216. package/src/styles/animations.scss +30 -30
  217. package/src/styles/index.scss +42 -42
  218. package/src/typings.d.ts +19 -19
  219. package/tsconfig.json +18 -18
  220. package/tsconfig.tsbuildinfo +1 -1
  221. package/dist/components/data/AdvancedRequestDataGrid/helpers/advancedRequests.d.ts +0 -14
  222. package/dist/components/data/AdvancedRequestDataGrid/helpers/advancedRequests.js +0 -77
  223. package/dist/components/data/AdvancedRequestDataGrid/helpers/columns.d.ts +0 -22
  224. package/dist/components/data/AdvancedRequestDataGrid/helpers/columns.js +0 -105
  225. package/dist/components/data/AdvancedRequestDataGrid/helpers/index.d.ts +0 -2
  226. package/dist/components/data/AdvancedRequestDataGrid/helpers/index.js +0 -18
  227. package/dist/components/data/AdvancedRequestDataGrid/index.d.ts +0 -2
  228. package/dist/components/data/AdvancedRequestDataGrid/index.js +0 -215
  229. package/dist/components/data/AdvancedRequestDataGrid/types.d.ts +0 -21
  230. package/dist/components/data/AdvancedRequestDataGrid/types.js +0 -2
  231. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.d.ts +0 -10
  232. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.js +0 -173
  233. package/dist/components/data/DataGrid/DataGridEditableCell.d.ts +0 -2
  234. package/dist/components/data/DataGrid/DataGridEditableCell.js +0 -27
  235. package/dist/components/data/DataGrid/FilterValuesScroller.d.ts +0 -13
  236. package/dist/components/data/DataGrid/FilterValuesScroller.js +0 -73
  237. package/dist/components/data/DataGrid/VirtualScroller.d.ts +0 -11
  238. package/dist/components/data/DataGrid/VirtualScroller.js +0 -41
  239. package/dist/components/data/DataGrid/helpers/advancedRequests.d.ts +0 -12
  240. package/dist/components/data/DataGrid/helpers/advancedRequests.js +0 -53
  241. package/dist/components/data/DataGrid/helpers.d.ts +0 -28
  242. package/dist/components/data/DataGrid/helpers.js +0 -436
  243. package/dist/components/data/SmartQueryDataGrid/helpers/columns.d.ts +0 -20
  244. package/dist/components/data/SmartQueryDataGrid/helpers/columns.js +0 -160
  245. package/dist/components/data/SmartQueryDataGrid/helpers/hooks.d.ts +0 -5
  246. package/dist/components/data/SmartQueryDataGrid/helpers/hooks.js +0 -41
  247. package/dist/components/data/SmartQueryDataGrid/helpers/index.d.ts +0 -2
  248. package/dist/components/data/SmartQueryDataGrid/helpers/index.js +0 -18
  249. package/dist/components/data/SmartQueryDataGrid/helpers/smartQueries.d.ts +0 -3
  250. package/dist/components/data/SmartQueryDataGrid/helpers/smartQueries.js +0 -15
  251. package/dist/components/data/SmartQueryDataGrid/hooks.d.ts +0 -9
  252. package/dist/components/data/SmartQueryDataGrid/hooks.js +0 -63
  253. package/dist/components/data/SmartQueryDataGrid/index.d.ts +0 -12
  254. package/dist/components/data/SmartQueryDataGrid/index.js +0 -242
  255. package/dist/components/data/SmartQueryDataGrid/types.d.ts +0 -22
  256. package/dist/components/data/SmartQueryDataGrid/types.js +0 -2
  257. package/dist/components/forms/Form/InputWithLabel.d.ts +0 -2
  258. package/dist/components/forms/Form/InputWithLabel.js +0 -10
  259. package/dist/components/forms/Form/types.d.ts +0 -7
  260. package/dist/components/forms/Form/types.js +0 -2
  261. package/dist/config/types.d.ts +0 -11
  262. package/dist/config/types.js +0 -2
  263. package/dist/services/requests/userPermissions.d.ts +0 -4
  264. package/dist/services/requests/userPermissions.js +0 -20
@@ -1,403 +1,403 @@
1
- import {
2
- ConditionDTO,
3
- OrderByDTO,
4
- SqlRequestFooterFunction,
5
- SqlRequestRow,
6
- useSqlRequestHandler,
7
- } from '../../../services/sqlRequests';
8
- import {
9
- DataGridContextProps,
10
- DataGridFilters,
11
- DataGridFooterFunction,
12
- DataGridRefProps,
13
- DataGridSort,
14
- } from '../DataGrid/types';
15
- import {
16
- Dispatch,
17
- ForwardedRef,
18
- SetStateAction,
19
- forwardRef,
20
- useCallback,
21
- useEffect,
22
- useImperativeHandle,
23
- useMemo,
24
- useRef,
25
- useState,
26
- } from 'react';
27
- import _, { debounce } from 'lodash';
28
-
29
- import { DataGrid } from '../DataGrid';
30
- import { SqlRequestDataGridProps } from './types';
31
- import { convertSqlFiltersToConditions } from './helpers';
32
- import { isColumnVisible } from '../DataGrid/helpers';
33
-
34
- export const SqlRequestDataGridInner = <R,>(
35
- {
36
- onSelectionChange: onSelectionChangeFromProps,
37
- ...props
38
- }: SqlRequestDataGridProps<R>,
39
- ref: ForwardedRef<DataGridRefProps<R>>
40
- ) => {
41
- const currentRows = useRef<R[]>([]);
42
- const [rows, setRows] = useState<R[]>([]);
43
- const setRowsRef = useCallback<Dispatch<SetStateAction<R[]>>>((value) => {
44
- const newRows =
45
- typeof value === 'function' ? value(currentRows.current) : value;
46
- currentRows.current = newRows;
47
- setRows(newRows);
48
- }, []);
49
-
50
- const [start, setStart] = useState(0);
51
- const [length, setLength] = useState(-1);
52
- const [count, setCount] = useState(-1);
53
- const [sqlRequest, sqlIdRequest, sqlPartialRequest] = useSqlRequestHandler<R>(
54
- props.type
55
- );
56
- const gridRef = useRef<DataGridRefProps<R>>(null);
57
-
58
- const [conditions, setConditions] = useState<Record<string, ConditionDTO>>(
59
- {}
60
- );
61
- const [orderBy, setOrderBy] = useState<OrderByDTO[]>(
62
- Object.entries(props.initialSorts ?? {}).map(
63
- ([columnKey, direction]): OrderByDTO => {
64
- const column = props.columns.find((col) => col.key === columnKey);
65
- return {
66
- field: column?.field?.fieldAlias ?? columnKey,
67
- type: column?.type ?? 'text',
68
- direction: direction.toUpperCase() as 'ASC' | 'DESC',
69
- };
70
- }
71
- )
72
- );
73
-
74
- const [columns, visibleColumnsKeys] = useMemo(
75
- () => [
76
- [
77
- ...props.columns.filter((col) => !col.ignore).map((col) => col.key),
78
- ...(props.hiddenColumns ?? []),
79
- ],
80
- [
81
- ...props.columns
82
- .filter((col) => !col.ignore)
83
- .filter(isColumnVisible)
84
- .map((col) => col.key),
85
- ...(props.hiddenColumns ?? []),
86
- ],
87
- ],
88
- [props.columns, props.hiddenColumns]
89
- );
90
- const columnTypes = useMemo(
91
- () =>
92
- visibleColumnsKeys.map((key) =>
93
- String(props.columns.find((col) => col.key === key)?.type ?? 'text')
94
- ),
95
- [visibleColumnsKeys, props.columns]
96
- );
97
-
98
- const refresh = useCallback(() => {
99
- setRowsRef([]);
100
- setStart(0);
101
- setCount(-1);
102
- }, [setRowsRef]);
103
-
104
- const onFiltersChanged = useCallback(
105
- (filters: DataGridFilters) => {
106
- const newConditions = convertSqlFiltersToConditions(filters);
107
- setConditions(newConditions);
108
- refresh();
109
- },
110
- [refresh]
111
- );
112
-
113
- const onSortsChanged = useCallback(
114
- (sorts: Record<string, DataGridSort>) => {
115
- refresh();
116
- const newOrderBy = Object.entries(sorts).map(
117
- ([columnKey, direction]) =>
118
- ({
119
- field: columnKey,
120
- direction: direction.toUpperCase(),
121
- } as OrderByDTO)
122
- );
123
- setOrderBy(newOrderBy);
124
- },
125
- [refresh]
126
- );
127
-
128
- const loadRows = useRef(
129
- debounce(
130
- (
131
- columns: string[],
132
- returnColumns: string[],
133
- conditions: ConditionDTO[] = [],
134
- orderBy: OrderByDTO[] = [],
135
- start = 0,
136
- length = 0,
137
- getCount = false
138
- ) => {
139
- if (length > 0) {
140
- sqlRequest({
141
- columns: columns.includes('Id') ? columns : [...columns, 'Id'],
142
- returnColumns: returnColumns.includes('Id')
143
- ? returnColumns
144
- : [...returnColumns, 'Id'],
145
- columnTypes: columnTypes.includes('Id')
146
- ? columnTypes
147
- : [...columnTypes, 'Id'],
148
- conditions,
149
- orderBy,
150
- start,
151
- length,
152
- getCount,
153
- }).then((response) => {
154
- if (getCount) {
155
- currentRows.current = Array(response.count).fill(null);
156
- if (getCount) setCount(response.count ?? 0);
157
- }
158
- const data = response.data ?? [];
159
- const rowsFromData = props.parser
160
- ? data.map(props.parser)
161
- : (data as R[]);
162
- const rowsToReplace = currentRows.current
163
- .slice(start, start + length)
164
- .map((row, i) => (!row ? rowsFromData[i] : row));
165
- currentRows.current.splice(start, length, ...rowsToReplace);
166
- setRowsRef([...currentRows.current]);
167
- });
168
- }
169
- },
170
- 100,
171
- {
172
- leading: true,
173
- trailing: true,
174
- }
175
- )
176
- );
177
-
178
- const loadFilterValues = useCallback(
179
- (columnKey: string) => {
180
- const column = props.columns.find((col) => col.key === columnKey);
181
- if (!column) {
182
- return Promise.resolve([]);
183
- }
184
- return sqlRequest({
185
- columns: columns,
186
- returnColumns: [columnKey],
187
- columnTypes: [column.type ?? 'text'],
188
- conditions: [
189
- ...(props.conditions ?? []),
190
- ...Object.values(_.pickBy(conditions, (_, key) => key !== columnKey)),
191
- ].filter((condition) => condition.field !== columnKey),
192
- orderBy: [
193
- {
194
- field:
195
- column.filterField ??
196
- column.field?.fieldAlias ??
197
- column.field?.fieldName ??
198
- columnKey,
199
- type: column.type ?? 'text',
200
- direction: 'ASC',
201
- },
202
- ],
203
- getCount: false,
204
- unique: true,
205
- }).then((response) =>
206
- response.data.map((row) => column.filter?.getter?.(row) ?? null)
207
- );
208
- },
209
- [columns, conditions, props.columns, props.conditions, sqlRequest]
210
- );
211
-
212
- useEffect(() => {
213
- if (
214
- (!rows.length && count < 0) ||
215
- rows.slice(start, start + length).some((row) => row === null)
216
- ) {
217
- loadRows.current(
218
- columns,
219
- visibleColumnsKeys,
220
- [...(props.conditions ?? []), ...(Object.values(conditions) ?? [])],
221
- orderBy,
222
- start,
223
- length,
224
- count < 0
225
- );
226
- }
227
- }, [
228
- props.columns,
229
- conditions,
230
- orderBy,
231
- start,
232
- length,
233
- count,
234
- props.conditions,
235
- columns,
236
- visibleColumnsKeys,
237
- rows,
238
- ]);
239
-
240
- const loadCopyRows = useCallback(
241
- () =>
242
- sqlRequest({
243
- columns: columns,
244
- returnColumns: visibleColumnsKeys,
245
- columnTypes,
246
- conditions: [
247
- ...(props.conditions ?? []),
248
- ...(Object.values(conditions) ?? []),
249
- ],
250
- orderBy,
251
- start: 0,
252
- length: count,
253
- }).then((response) =>
254
- props.parser ? response.data.map(props.parser) : (response.data as R[])
255
- ),
256
- [
257
- sqlRequest,
258
- columns,
259
- visibleColumnsKeys,
260
- columnTypes,
261
- props.conditions,
262
- props.parser,
263
- conditions,
264
- orderBy,
265
- count,
266
- ]
267
- );
268
-
269
- const loadAllIds = useCallback(
270
- () =>
271
- sqlIdRequest({
272
- columns: columns.includes('Id') ? columns : [...columns, 'Id'],
273
- returnColumns: ['Id'],
274
- columnTypes: ['text'],
275
- conditions: [
276
- ...(props.conditions ?? []),
277
- ...(Object.values(conditions) ?? []),
278
- ],
279
- orderBy,
280
- start: 0,
281
- length: count,
282
- }).then((response) => response.data.map((row) => row['Id'])),
283
- [columns, conditions, orderBy, props.conditions, sqlIdRequest, count]
284
- );
285
-
286
- const onVisibleRowsChanged = useCallback(
287
- (newStart: number, newLength: number) => {
288
- if (newStart !== start || newLength !== length) {
289
- setStart(newStart);
290
- setLength(newLength);
291
- }
292
- },
293
- [length, start]
294
- );
295
-
296
- const onSelectionChange = useCallback(
297
- (selectedKeys: string[]) => {
298
- onSelectionChangeFromProps?.(selectedKeys);
299
- },
300
- [onSelectionChangeFromProps]
301
- );
302
-
303
- /** FOOTERS */
304
- const [footers, setFooters] = useState<Record<string, string>>(
305
- props.initialFooters ?? {}
306
- );
307
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
308
- const [footerData, setFooterData] = useState<SqlRequestRow<any>>({});
309
-
310
- const loadFooters = useCallback(() => {
311
- if (Object.keys(footers).length === 0) {
312
- setFooterData({});
313
- } else {
314
- sqlPartialRequest({
315
- columns: columns.includes('Id') ? columns : [...columns, 'Id'],
316
- returnColumns: [],
317
- columnTypes: [],
318
- totalColumns: footers as Record<string, SqlRequestFooterFunction>,
319
- conditions: [
320
- ...(props.conditions ?? []),
321
- ...(Object.values(conditions) ?? []),
322
- ],
323
- orderBy,
324
- }).then((response) => setFooterData(response.totals ?? {}));
325
- }
326
- }, [
327
- columns,
328
- conditions,
329
- footers,
330
- orderBy,
331
- props.conditions,
332
- sqlPartialRequest,
333
- ]);
334
-
335
- useEffect(() => {
336
- loadFooters();
337
- }, [loadFooters]);
338
-
339
- const footerFunctions = useMemo(
340
- () =>
341
- !footerData
342
- ? {}
343
- : Object.entries(footers).reduce((acc, [columnKey, footerKey]) => {
344
- const column = props.columns.find((col) => col.key === columnKey);
345
- const footerFunc =
346
- typeof column?.footer === 'function'
347
- ? column.footer
348
- : column?.footer?.[footerKey];
349
- const render = footerFunc
350
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
351
- (data: any) => footerFunc([data], [], [])
352
- : column?.render ?? _.identity;
353
- if (!column) {
354
- return acc;
355
- }
356
- acc[columnKey] = () => render(footerData, column);
357
- return acc;
358
- }, {} as Record<string, DataGridFooterFunction<R>>),
359
- [footerData, footers, props.columns]
360
- );
361
-
362
- const contextOverride = useMemo<Partial<DataGridContextProps<R>>>(
363
- () => ({
364
- footers,
365
- setFooters,
366
- footerFunctions,
367
- rows,
368
- setRows: setRowsRef,
369
- }),
370
- [footerFunctions, footers, rows, setRowsRef]
371
- );
372
-
373
- useImperativeHandle(
374
- ref,
375
- () => ({
376
- refresh,
377
- setSelectedKeys: gridRef.current?.setSelectedKeys ?? (() => {}),
378
- addRows: gridRef.current?.addRows ?? (() => {}),
379
- }),
380
- [refresh]
381
- );
382
-
383
- return (
384
- <DataGrid
385
- ref={gridRef}
386
- onVisibleRowsChange={onVisibleRowsChanged}
387
- filter={false}
388
- sort={false}
389
- onFiltersChanged={onFiltersChanged}
390
- onSortsChanged={onSortsChanged}
391
- filterValuesLoader={loadFilterValues}
392
- rows={rows}
393
- loadCopyRows={loadCopyRows}
394
- refresh={refresh}
395
- onSelectionChange={onSelectionChange}
396
- getAllIds={loadAllIds}
397
- contextOverride={contextOverride}
398
- {...props}
399
- />
400
- );
401
- };
402
-
403
- export const SqlRequestDataGrid = forwardRef(SqlRequestDataGridInner);
1
+ import {
2
+ ConditionDTO,
3
+ OrderByDTO,
4
+ SqlRequestFooterFunction,
5
+ SqlRequestRow,
6
+ useSqlRequestHandler,
7
+ } from '../../../services/sqlRequests';
8
+ import {
9
+ DataGridContextProps,
10
+ DataGridFilters,
11
+ DataGridFooterFunction,
12
+ DataGridRefProps,
13
+ DataGridSort,
14
+ } from '../DataGrid/types';
15
+ import {
16
+ Dispatch,
17
+ ForwardedRef,
18
+ SetStateAction,
19
+ forwardRef,
20
+ useCallback,
21
+ useEffect,
22
+ useImperativeHandle,
23
+ useMemo,
24
+ useRef,
25
+ useState,
26
+ } from 'react';
27
+ import _, { debounce } from 'lodash';
28
+
29
+ import { DataGrid } from '../DataGrid';
30
+ import { SqlRequestDataGridProps } from './types';
31
+ import { convertSqlFiltersToConditions } from './helpers';
32
+ import { isColumnVisible } from '../DataGrid/helpers';
33
+
34
+ export const SqlRequestDataGridInner = <R,>(
35
+ {
36
+ onSelectionChange: onSelectionChangeFromProps,
37
+ ...props
38
+ }: SqlRequestDataGridProps<R>,
39
+ ref: ForwardedRef<DataGridRefProps<R>>
40
+ ) => {
41
+ const currentRows = useRef<R[]>([]);
42
+ const [rows, setRows] = useState<R[]>([]);
43
+ const setRowsRef = useCallback<Dispatch<SetStateAction<R[]>>>((value) => {
44
+ const newRows =
45
+ typeof value === 'function' ? value(currentRows.current) : value;
46
+ currentRows.current = newRows;
47
+ setRows(newRows);
48
+ }, []);
49
+
50
+ const [start, setStart] = useState(0);
51
+ const [length, setLength] = useState(-1);
52
+ const [count, setCount] = useState(-1);
53
+ const [sqlRequest, sqlIdRequest, sqlPartialRequest] = useSqlRequestHandler<R>(
54
+ props.type
55
+ );
56
+ const gridRef = useRef<DataGridRefProps<R>>(null);
57
+
58
+ const [conditions, setConditions] = useState<Record<string, ConditionDTO>>(
59
+ {}
60
+ );
61
+ const [orderBy, setOrderBy] = useState<OrderByDTO[]>(
62
+ Object.entries(props.initialSorts ?? {}).map(
63
+ ([columnKey, direction]): OrderByDTO => {
64
+ const column = props.columns.find((col) => col.key === columnKey);
65
+ return {
66
+ field: column?.field?.fieldAlias ?? columnKey,
67
+ type: column?.type ?? 'text',
68
+ direction: direction.toUpperCase() as 'ASC' | 'DESC',
69
+ };
70
+ }
71
+ )
72
+ );
73
+
74
+ const [columns, visibleColumnsKeys] = useMemo(
75
+ () => [
76
+ [
77
+ ...props.columns.filter((col) => !col.ignore).map((col) => col.key),
78
+ ...(props.hiddenColumns ?? []),
79
+ ],
80
+ [
81
+ ...props.columns
82
+ .filter((col) => !col.ignore)
83
+ .filter(isColumnVisible)
84
+ .map((col) => col.key),
85
+ ...(props.hiddenColumns ?? []),
86
+ ],
87
+ ],
88
+ [props.columns, props.hiddenColumns]
89
+ );
90
+ const columnTypes = useMemo(
91
+ () =>
92
+ visibleColumnsKeys.map((key) =>
93
+ String(props.columns.find((col) => col.key === key)?.type ?? 'text')
94
+ ),
95
+ [visibleColumnsKeys, props.columns]
96
+ );
97
+
98
+ const refresh = useCallback(() => {
99
+ setRowsRef([]);
100
+ setStart(0);
101
+ setCount(-1);
102
+ }, [setRowsRef]);
103
+
104
+ const onFiltersChanged = useCallback(
105
+ (filters: DataGridFilters) => {
106
+ const newConditions = convertSqlFiltersToConditions(filters);
107
+ setConditions(newConditions);
108
+ refresh();
109
+ },
110
+ [refresh]
111
+ );
112
+
113
+ const onSortsChanged = useCallback(
114
+ (sorts: Record<string, DataGridSort>) => {
115
+ refresh();
116
+ const newOrderBy = Object.entries(sorts).map(
117
+ ([columnKey, direction]) =>
118
+ ({
119
+ field: columnKey,
120
+ direction: direction.toUpperCase(),
121
+ } as OrderByDTO)
122
+ );
123
+ setOrderBy(newOrderBy);
124
+ },
125
+ [refresh]
126
+ );
127
+
128
+ const loadRows = useRef(
129
+ debounce(
130
+ (
131
+ columns: string[],
132
+ returnColumns: string[],
133
+ conditions: ConditionDTO[] = [],
134
+ orderBy: OrderByDTO[] = [],
135
+ start = 0,
136
+ length = 0,
137
+ getCount = false
138
+ ) => {
139
+ if (length > 0) {
140
+ sqlRequest({
141
+ columns: columns.includes('Id') ? columns : [...columns, 'Id'],
142
+ returnColumns: returnColumns.includes('Id')
143
+ ? returnColumns
144
+ : [...returnColumns, 'Id'],
145
+ columnTypes: columnTypes.includes('Id')
146
+ ? columnTypes
147
+ : [...columnTypes, 'Id'],
148
+ conditions,
149
+ orderBy,
150
+ start,
151
+ length,
152
+ getCount,
153
+ }).then((response) => {
154
+ if (getCount) {
155
+ currentRows.current = Array(response.count).fill(null);
156
+ if (getCount) setCount(response.count ?? 0);
157
+ }
158
+ const data = response.data ?? [];
159
+ const rowsFromData = props.parser
160
+ ? data.map(props.parser)
161
+ : (data as R[]);
162
+ const rowsToReplace = currentRows.current
163
+ .slice(start, start + length)
164
+ .map((row, i) => (!row ? rowsFromData[i] : row));
165
+ currentRows.current.splice(start, length, ...rowsToReplace);
166
+ setRowsRef([...currentRows.current]);
167
+ });
168
+ }
169
+ },
170
+ 100,
171
+ {
172
+ leading: true,
173
+ trailing: true,
174
+ }
175
+ )
176
+ );
177
+
178
+ const loadFilterValues = useCallback(
179
+ (columnKey: string) => {
180
+ const column = props.columns.find((col) => col.key === columnKey);
181
+ if (!column) {
182
+ return Promise.resolve([]);
183
+ }
184
+ return sqlRequest({
185
+ columns: columns,
186
+ returnColumns: [columnKey],
187
+ columnTypes: [column.type ?? 'text'],
188
+ conditions: [
189
+ ...(props.conditions ?? []),
190
+ ...Object.values(_.pickBy(conditions, (_, key) => key !== columnKey)),
191
+ ].filter((condition) => condition.field !== columnKey),
192
+ orderBy: [
193
+ {
194
+ field:
195
+ column.filterField ??
196
+ column.field?.fieldAlias ??
197
+ column.field?.fieldName ??
198
+ columnKey,
199
+ type: column.type ?? 'text',
200
+ direction: 'ASC',
201
+ },
202
+ ],
203
+ getCount: false,
204
+ unique: true,
205
+ }).then((response) =>
206
+ response.data.map((row) => column.filter?.getter?.(row) ?? null)
207
+ );
208
+ },
209
+ [columns, conditions, props.columns, props.conditions, sqlRequest]
210
+ );
211
+
212
+ useEffect(() => {
213
+ if (
214
+ (!rows.length && count < 0) ||
215
+ rows.slice(start, start + length).some((row) => row === null)
216
+ ) {
217
+ loadRows.current(
218
+ columns,
219
+ visibleColumnsKeys,
220
+ [...(props.conditions ?? []), ...(Object.values(conditions) ?? [])],
221
+ orderBy,
222
+ start,
223
+ length,
224
+ count < 0
225
+ );
226
+ }
227
+ }, [
228
+ props.columns,
229
+ conditions,
230
+ orderBy,
231
+ start,
232
+ length,
233
+ count,
234
+ props.conditions,
235
+ columns,
236
+ visibleColumnsKeys,
237
+ rows,
238
+ ]);
239
+
240
+ const loadCopyRows = useCallback(
241
+ () =>
242
+ sqlRequest({
243
+ columns: columns,
244
+ returnColumns: visibleColumnsKeys,
245
+ columnTypes,
246
+ conditions: [
247
+ ...(props.conditions ?? []),
248
+ ...(Object.values(conditions) ?? []),
249
+ ],
250
+ orderBy,
251
+ start: 0,
252
+ length: count,
253
+ }).then((response) =>
254
+ props.parser ? response.data.map(props.parser) : (response.data as R[])
255
+ ),
256
+ [
257
+ sqlRequest,
258
+ columns,
259
+ visibleColumnsKeys,
260
+ columnTypes,
261
+ props.conditions,
262
+ props.parser,
263
+ conditions,
264
+ orderBy,
265
+ count,
266
+ ]
267
+ );
268
+
269
+ const loadAllIds = useCallback(
270
+ () =>
271
+ sqlIdRequest({
272
+ columns: columns.includes('Id') ? columns : [...columns, 'Id'],
273
+ returnColumns: ['Id'],
274
+ columnTypes: ['text'],
275
+ conditions: [
276
+ ...(props.conditions ?? []),
277
+ ...(Object.values(conditions) ?? []),
278
+ ],
279
+ orderBy,
280
+ start: 0,
281
+ length: count,
282
+ }).then((response) => response.data.map((row) => row['Id'])),
283
+ [columns, conditions, orderBy, props.conditions, sqlIdRequest, count]
284
+ );
285
+
286
+ const onVisibleRowsChanged = useCallback(
287
+ (newStart: number, newLength: number) => {
288
+ if (newStart !== start || newLength !== length) {
289
+ setStart(newStart);
290
+ setLength(newLength);
291
+ }
292
+ },
293
+ [length, start]
294
+ );
295
+
296
+ const onSelectionChange = useCallback(
297
+ (selectedKeys: string[]) => {
298
+ onSelectionChangeFromProps?.(selectedKeys);
299
+ },
300
+ [onSelectionChangeFromProps]
301
+ );
302
+
303
+ /** FOOTERS */
304
+ const [footers, setFooters] = useState<Record<string, string>>(
305
+ props.initialFooters ?? {}
306
+ );
307
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
308
+ const [footerData, setFooterData] = useState<SqlRequestRow<any>>({});
309
+
310
+ const loadFooters = useCallback(() => {
311
+ if (Object.keys(footers).length === 0) {
312
+ setFooterData({});
313
+ } else {
314
+ sqlPartialRequest({
315
+ columns: columns.includes('Id') ? columns : [...columns, 'Id'],
316
+ returnColumns: [],
317
+ columnTypes: [],
318
+ totalColumns: footers as Record<string, SqlRequestFooterFunction>,
319
+ conditions: [
320
+ ...(props.conditions ?? []),
321
+ ...(Object.values(conditions) ?? []),
322
+ ],
323
+ orderBy,
324
+ }).then((response) => setFooterData(response.totals ?? {}));
325
+ }
326
+ }, [
327
+ columns,
328
+ conditions,
329
+ footers,
330
+ orderBy,
331
+ props.conditions,
332
+ sqlPartialRequest,
333
+ ]);
334
+
335
+ useEffect(() => {
336
+ loadFooters();
337
+ }, [loadFooters]);
338
+
339
+ const footerFunctions = useMemo(
340
+ () =>
341
+ !footerData
342
+ ? {}
343
+ : Object.entries(footers).reduce((acc, [columnKey, footerKey]) => {
344
+ const column = props.columns.find((col) => col.key === columnKey);
345
+ const footerFunc =
346
+ typeof column?.footer === 'function'
347
+ ? column.footer
348
+ : column?.footer?.[footerKey];
349
+ const render = footerFunc
350
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
351
+ (data: any) => footerFunc([data], [], [])
352
+ : column?.render ?? _.identity;
353
+ if (!column) {
354
+ return acc;
355
+ }
356
+ acc[columnKey] = () => render(footerData, column);
357
+ return acc;
358
+ }, {} as Record<string, DataGridFooterFunction<R>>),
359
+ [footerData, footers, props.columns]
360
+ );
361
+
362
+ const contextOverride = useMemo<Partial<DataGridContextProps<R>>>(
363
+ () => ({
364
+ footers,
365
+ setFooters,
366
+ footerFunctions,
367
+ rows,
368
+ setRows: setRowsRef,
369
+ }),
370
+ [footerFunctions, footers, rows, setRowsRef]
371
+ );
372
+
373
+ useImperativeHandle(
374
+ ref,
375
+ () => ({
376
+ refresh,
377
+ setSelectedKeys: gridRef.current?.setSelectedKeys ?? (() => {}),
378
+ addRows: gridRef.current?.addRows ?? (() => {}),
379
+ }),
380
+ [refresh]
381
+ );
382
+
383
+ return (
384
+ <DataGrid
385
+ ref={gridRef}
386
+ onVisibleRowsChange={onVisibleRowsChanged}
387
+ filter={false}
388
+ sort={false}
389
+ onFiltersChanged={onFiltersChanged}
390
+ onSortsChanged={onSortsChanged}
391
+ filterValuesLoader={loadFilterValues}
392
+ rows={rows}
393
+ loadCopyRows={loadCopyRows}
394
+ refresh={refresh}
395
+ onSelectionChange={onSelectionChange}
396
+ getAllIds={loadAllIds}
397
+ contextOverride={contextOverride}
398
+ {...props}
399
+ />
400
+ );
401
+ };
402
+
403
+ export const SqlRequestDataGrid = forwardRef(SqlRequestDataGridInner);