@addev-be/ui 0.18.2 → 0.19.0

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