@addsign/moje-agenda-shared-lib 2.0.60 → 2.0.61

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.
@@ -128,7 +128,7 @@ function DataTableServer<T extends DataTableInternalItems>({
128
128
  const tableRef = useRef<HTMLTableElement | null>(null);
129
129
  const syncWidthRef = useRef<HTMLDivElement | null>(null);
130
130
 
131
- const [itemsPerPageLocal, setItemsPerPageLocal] = useState<number>();
131
+ const [itemsPerPageLocal, setItemsPerPageLocal] = useState<number>(10);
132
132
  const federationContext = useFederationContext();
133
133
  const [data, setData] = useState<IPageable<T>>();
134
134
  const [isLoading, setIsLoading] = useState(false);
@@ -137,15 +137,17 @@ function DataTableServer<T extends DataTableInternalItems>({
137
137
 
138
138
  const [hasMounted, setHasMounted] = useState(false);
139
139
 
140
- const [currentPage, setCurrentPage] = useState<number>();
140
+ const [currentPage, setCurrentPage] = useState<number>(0);
141
141
  const [selectedItems, setSelectedItems] = useState<T[]>([]);
142
142
  const [fulltextSearch, setFulltextSearch] = useState("");
143
143
  const [filterOptions, setFilterOptions] = useState<Record<string, any[]>>({});
144
144
  const [columnFilters, setColumnFilters] = useState<{ [key: string]: any }>(
145
145
  {}
146
146
  );
147
- const [showColFilters, setShowColFilters] = useState<boolean>();
147
+ const [showColFilters, setShowColFilters] = useState<boolean>(false);
148
148
  const [sortConfig, setSortConfig] = useState<ISortConfig | null>(null);
149
+ const prevDepsRef = useRef<any[]>([]);
150
+ const prevFilterDepsRef = useRef<any[]>([]);
149
151
 
150
152
  const createDataPageable = (
151
153
  response: any,
@@ -171,24 +173,53 @@ function DataTableServer<T extends DataTableInternalItems>({
171
173
  const mergedFilters: { [key: string]: any } = useMemo(() => {
172
174
  return showColFilters ? { ...columnFilters, ...filters } : filters || {};
173
175
  }, [columnFilters, filters, showColFilters]);
174
-
175
176
  useEffect(() => {
176
- setReloadData(true);
177
+ const currentDeps = [
178
+ url,
179
+ showColFilters,
180
+ columnFilters,
181
+ itemsPerPageLocal,
182
+ currentPage,
183
+ sortConfig,
184
+ filters,
185
+ tableKey,
186
+ ];
187
+
188
+ // Kontrola, jestli se skutečně něco změnilo
189
+ const hasChanged = currentDeps.some((dep, index) => {
190
+ return JSON.stringify(dep) !== JSON.stringify(prevDepsRef.current[index]);
191
+ });
192
+
193
+ if (hasChanged) {
194
+ console.log(
195
+ "setting reloadData - actual change detected",
196
+ url,
197
+ showColFilters,
198
+ columnFilters,
199
+ itemsPerPageLocal,
200
+ currentPage,
201
+ sortConfig,
202
+ filters,
203
+ tableKey,
204
+ reloadData
205
+ );
206
+ setReloadData(true);
207
+ }
208
+
209
+ prevDepsRef.current = currentDeps;
177
210
  }, [
178
211
  url,
179
212
  showColFilters,
180
- // id,
181
213
  columnFilters,
182
214
  itemsPerPageLocal,
183
215
  currentPage,
184
216
  sortConfig,
185
- federationContext.apiClient,
186
- federationContext.emitter,
187
217
  filters,
188
218
  tableKey,
189
219
  ]);
190
220
 
191
221
  useEffect(() => {
222
+ console.log("reloadData", reloadData);
192
223
  if (reloadData) {
193
224
  if (abortControllerRef.current) {
194
225
  abortControllerRef.current.abort();
@@ -307,66 +338,92 @@ function DataTableServer<T extends DataTableInternalItems>({
307
338
  }, [id]);
308
339
 
309
340
  useEffect(() => {
310
- const fetchFilterOptions = async (column: DataTableColumn<T>) => {
311
- if (column.filterOptions) {
312
- return column.filterOptions;
313
- } else if (column.filterSource) {
314
- try {
315
- const response = await federationContext.apiClient.get(
316
- column.filterSource
317
- );
318
-
319
- const options = response.data.map((item: any) => ({
320
- value: item[column.filterValueKey as keyof typeof item]?.toString(),
321
- label: item[column.filterLabelKey as keyof typeof item]?.toString(),
322
- }));
323
- return options;
324
- } catch (error) {
325
- console.error("Error fetching filter options:", error);
326
- return [];
327
- }
341
+ const currentFilterDeps = [columns, federationContext.apiClient];
342
+
343
+ // Kontrola, jestli se skutečně něco změnilo
344
+ const hasFilterChanged = currentFilterDeps.some((dep, index) => {
345
+ const prev = prevFilterDepsRef.current[index];
346
+ if (index === 1) {
347
+ // federationContext.apiClient (index 1) - referenční porovnání
348
+ return dep !== prev;
328
349
  } else {
329
- return [];
350
+ // columns (index 0) - deep porovnání
351
+ return JSON.stringify(dep) !== JSON.stringify(prev);
330
352
  }
331
- };
353
+ });
332
354
 
333
- const updateFilterOptions = async () => {
334
- const newFilterOptions: Record<string, any[]> = {};
335
-
336
- for (const column of columns) {
337
- if (
338
- (column.filterType === "select" ||
339
- column.filterType === "multi-select") &&
340
- (column.filterSource || column.filterOptions) &&
341
- column.filterValueKey &&
342
- column.filterLabelKey &&
343
- column.filterParam
344
- ) {
345
- const options = await fetchFilterOptions(column);
346
-
347
- if (options && column.filterType === "select") {
348
- // Filter out empty values and add a "clear" option
349
- const filteredOptions = options.filter(
350
- (option: IOptionItem) =>
351
- option.value !== null &&
352
- option.value !== undefined &&
353
- option.value !== ""
355
+ if (hasFilterChanged) {
356
+ const fetchFilterOptions = async (column: DataTableColumn<T>) => {
357
+ if (column.filterOptions) {
358
+ return column.filterOptions;
359
+ } else if (column.filterSource) {
360
+ try {
361
+ const response = await federationContext.apiClient.get(
362
+ column.filterSource
354
363
  );
355
- newFilterOptions[column.filterParam as string] = [
356
- { value: "__clear__", label: "Všechny" },
357
- ...filteredOptions,
358
- ];
359
- } else if (options && column.filterType === "multi-select") {
360
- newFilterOptions[column.filterParam as string] = options;
364
+
365
+ const options = response.data.map((item: any) => ({
366
+ value:
367
+ item[column.filterValueKey as keyof typeof item]?.toString(),
368
+ label:
369
+ item[column.filterLabelKey as keyof typeof item]?.toString(),
370
+ }));
371
+ return options;
372
+ } catch (error) {
373
+ console.error("Error fetching filter options:", error);
374
+ return [];
375
+ }
376
+ } else {
377
+ return [];
378
+ }
379
+ };
380
+
381
+ const updateFilterOptions = async () => {
382
+ const newFilterOptions: Record<string, any[]> = {};
383
+
384
+ for (const column of columns) {
385
+ if (
386
+ (column.filterType === "select" ||
387
+ column.filterType === "multi-select") &&
388
+ (column.filterSource || column.filterOptions) &&
389
+ column.filterValueKey &&
390
+ column.filterLabelKey &&
391
+ column.filterParam
392
+ ) {
393
+ const options = await fetchFilterOptions(column);
394
+
395
+ if (options && column.filterType === "select") {
396
+ // Filter out empty values and add a "clear" option
397
+ const filteredOptions = options.filter(
398
+ (option: IOptionItem) =>
399
+ option.value !== null &&
400
+ option.value !== undefined &&
401
+ option.value !== ""
402
+ );
403
+ newFilterOptions[column.filterParam as string] = [
404
+ { value: "__clear__", label: "Všechny" },
405
+ ...filteredOptions,
406
+ ];
407
+ } else if (options && column.filterType === "multi-select") {
408
+ newFilterOptions[column.filterParam as string] = options;
409
+ }
361
410
  }
362
411
  }
363
- }
364
412
 
365
- setFilterOptions(newFilterOptions);
366
- };
413
+ setFilterOptions(newFilterOptions);
414
+ };
415
+
416
+ console.log(
417
+ "updateFilterOptions - actual change detected",
418
+ columns,
419
+ federationContext.apiClient
420
+ );
421
+ updateFilterOptions();
422
+ }
367
423
 
368
- updateFilterOptions();
424
+ prevFilterDepsRef.current = currentFilterDeps;
369
425
  }, [columns, federationContext.apiClient]);
426
+
370
427
  const hasSomeColFilters = useMemo(() => {
371
428
  return columns.some((column) => !!column.filterParam);
372
429
  }, [columns]);
@@ -848,7 +905,12 @@ function DataTableServer<T extends DataTableInternalItems>({
848
905
  <div
849
906
  className="p-0 m-0 pb-2"
850
907
  onClick={(e) => e.stopPropagation()}
851
- data-cy={"datatable-filter-container-" + id + "-" + String(key)}
908
+ data-cy={
909
+ "datatable-filter-container-" +
910
+ id +
911
+ "-" +
912
+ String(key)
913
+ }
852
914
  >
853
915
  {filterType === "select" ? (
854
916
  <Select
@@ -867,15 +929,11 @@ function DataTableServer<T extends DataTableInternalItems>({
867
929
  String(filterParam)
868
930
  ]?.toString() || "__clear__"
869
931
  }
870
-
871
932
  disabled={Object.keys(
872
933
  (filters as object) || {}
873
934
  ).includes(String(filterParam))}
874
935
  >
875
- <SelectTrigger
876
-
877
- className="flex-1 w-full px-2 font-normal placeholder-muted-foreground"
878
- >
936
+ <SelectTrigger className="flex-1 w-full px-2 font-normal placeholder-muted-foreground">
879
937
  <SelectValue placeholder="Zadejte filtr" />
880
938
  </SelectTrigger>
881
939
  <SelectContent>
@@ -921,7 +979,7 @@ function DataTableServer<T extends DataTableInternalItems>({
921
979
  ).includes(String(filterParam))}
922
980
  variant="secondary"
923
981
  maxCount={0}
924
- />
982
+ />
925
983
  ) : filterType === "dateRange" ? (
926
984
  <DateRangeField
927
985
  // key={JSON.stringify(mergedFilters)}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@addsign/moje-agenda-shared-lib",
3
3
  "private": false,
4
- "version": "2.0.60",
4
+ "version": "2.0.61",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
7
7
  "types": "dist/main.d.ts",