@visns-studio/visns-components 5.3.14 → 5.4.1

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.
package/package.json CHANGED
@@ -78,7 +78,7 @@
78
78
  "react-dom": "^17.0.0 || ^18.0.0"
79
79
  },
80
80
  "name": "@visns-studio/visns-components",
81
- "version": "5.3.14",
81
+ "version": "5.4.1",
82
82
  "description": "Various packages to assist in the development of our Custom Applications.",
83
83
  "main": "src/index.js",
84
84
  "files": [
@@ -296,6 +296,29 @@ function GenericDetail({
296
296
  window.open(exportUrl);
297
297
  };
298
298
 
299
+ const handleCustomAction = async (action) => {
300
+ try {
301
+ if (action.url) {
302
+ const res = await CustomFetch(
303
+ action.url,
304
+ action.method || 'POST',
305
+ {
306
+ id: routeParams[urlParam],
307
+ ...action.payload,
308
+ }
309
+ );
310
+
311
+ if (res.data.error === '') {
312
+ toast.success(action.message);
313
+ } else {
314
+ toast.error(res.data.error);
315
+ }
316
+ }
317
+ } catch (error) {
318
+ console.error(error);
319
+ }
320
+ };
321
+
299
322
  const handleCheckboxUpdate = async () => {
300
323
  try {
301
324
  if (currentConfigRef.current.functions.checkboxUpdate) {
@@ -321,6 +344,31 @@ function GenericDetail({
321
344
  }
322
345
  };
323
346
 
347
+ const handleCheckboxDelete = async () => {
348
+ try {
349
+ if (currentConfigRef.current.functions.checkboxDelete) {
350
+ const { data, method, message, url } =
351
+ currentConfigRef.current.functions.checkboxDelete;
352
+ if (!Object.keys(rowsSelected).length) {
353
+ toast.warning(message.warning);
354
+ return;
355
+ }
356
+ const res = await CustomFetch(url, method, {
357
+ ...data,
358
+ rows: rowsSelected,
359
+ });
360
+ if (res.data.error === '') {
361
+ toast.success(message.success);
362
+ gridRef.current.reload();
363
+ } else {
364
+ toast.error(message.error);
365
+ }
366
+ }
367
+ } catch (error) {
368
+ console.error(error);
369
+ }
370
+ };
371
+
324
372
  const handleReload = () => {
325
373
  const min = 0;
326
374
  const max = 999999999;
@@ -877,10 +925,6 @@ function GenericDetail({
877
925
  }
878
926
  };
879
927
 
880
- useEffect(() => {
881
- console.info(activeTabConfig);
882
- }, [activeTabConfig]);
883
-
884
928
  const renderContent = () => {
885
929
  if (subnav && subnav.length > 0) {
886
930
  const activeTab = subnav.find((s) => s.show === true);
@@ -1686,6 +1730,22 @@ function GenericDetail({
1686
1730
  Export
1687
1731
  </button>
1688
1732
  )}
1733
+
1734
+ {activeTabConfig.customAction && (
1735
+ <button
1736
+ className={styles.btn}
1737
+ onClick={() =>
1738
+ handleCustomAction(
1739
+ activeTabConfig.customAction
1740
+ )
1741
+ }
1742
+ >
1743
+ {
1744
+ activeTabConfig
1745
+ .customAction.label
1746
+ }
1747
+ </button>
1748
+ )}
1689
1749
  </div>
1690
1750
  )}
1691
1751
  </>
@@ -1913,6 +1973,22 @@ function GenericDetail({
1913
1973
  </button>
1914
1974
  </div>
1915
1975
  )}
1976
+
1977
+ {activeTabConfig.functions
1978
+ ?.checkboxDelete && (
1979
+ <div className={styles.polActions}>
1980
+ <button
1981
+ className={styles.btn}
1982
+ onClick={
1983
+ handleCheckboxDelete
1984
+ }
1985
+ >
1986
+ {activeTabConfig.functions
1987
+ .checkboxDelete.label ||
1988
+ 'Delete'}
1989
+ </button>
1990
+ </div>
1991
+ )}
1916
1992
  </>
1917
1993
  );
1918
1994
  } else {
@@ -249,6 +249,52 @@ function GenericIndex({
249
249
  tableData,
250
250
  ]);
251
251
 
252
+ const handleCheckboxDelete = async () => {
253
+ try {
254
+ const { data, method, message, url } = setting?.functions
255
+ ?.checkboxDelete
256
+ ? setting.functions.checkboxDelete
257
+ : config.form?.functions?.checkboxDelete;
258
+
259
+ if (!Object.keys(rowsSelectedRef.current).length) {
260
+ toast.warning(message.warning);
261
+ return;
262
+ }
263
+
264
+ // Create a map of selected rows with full data from tableData
265
+ const selectedRowsData = {};
266
+ Object.entries(rowsSelectedRef.current).forEach(([id, value]) => {
267
+ if (value) {
268
+ const fullRowData = tableData.find(
269
+ (row) => row.id === parseInt(id)
270
+ );
271
+ if (fullRowData) {
272
+ selectedRowsData[id] = fullRowData;
273
+ }
274
+ }
275
+ });
276
+
277
+ const res = await CustomFetch(url, method, {
278
+ ...data,
279
+ rows: selectedRowsData,
280
+ });
281
+
282
+ if (res.data.error === '') {
283
+ toast.success(message.success);
284
+ gridRef.current?.reload();
285
+ // Clear selections after successful delete
286
+ rowsSelectedRef.current = {};
287
+ } else {
288
+ toast.error(message.error);
289
+ }
290
+ } catch (err) {
291
+ console.error(err);
292
+ toast.error(
293
+ 'An error occurred while processing the delete request'
294
+ );
295
+ }
296
+ };
297
+
252
298
  const handleExport = async (e) => {
253
299
  try {
254
300
  if (e) {
@@ -464,34 +510,48 @@ function GenericIndex({
464
510
 
465
511
  <div className={styles.grid}>{renderContent()}</div>
466
512
  <Outlet />
467
- {setting.functions?.checkboxUpdate && (
468
- <div className={styles.polActions}>
513
+
514
+ <div className={styles.polActions}>
515
+ {setting.functions?.checkboxUpdate && (
469
516
  <button
470
517
  className={styles.btn}
471
518
  onClick={handleCheckboxUpdate}
472
519
  >
473
520
  {setting.functions.checkboxUpdate.label || 'Update'}
474
521
  </button>
475
- </div>
476
- )}
477
- {config?.form?.functions?.checkboxUpdate && (
478
- <div className={styles.polActions}>
522
+ )}
523
+ {config?.form?.functions?.checkboxUpdate && (
479
524
  <button
480
525
  className={styles.btn}
481
526
  onClick={handleCheckboxUpdate}
482
527
  >
483
528
  {config.form.functions.checkboxUpdate.label || 'Update'}
484
529
  </button>
485
- </div>
486
- )}
530
+ )}
531
+
532
+ {setting.functions?.checkboxDelete && (
533
+ <button
534
+ className={styles.btn}
535
+ onClick={handleCheckboxDelete}
536
+ >
537
+ {setting.functions.checkboxDelete.label || 'Delete'}
538
+ </button>
539
+ )}
540
+ {config?.form?.functions?.checkboxDelete && (
541
+ <button
542
+ className={styles.btn}
543
+ onClick={handleCheckboxDelete}
544
+ >
545
+ {config.form.functions.checkboxDelete.label || 'Delete'}
546
+ </button>
547
+ )}
487
548
 
488
- {setting.export?.label && (
489
- <div className={styles.polActions}>
549
+ {setting.export?.label && (
490
550
  <button className={styles.btn} onClick={handleExport}>
491
551
  {setting.export.label}
492
552
  </button>
493
- </div>
494
- )}
553
+ )}
554
+ </div>
495
555
  </>
496
556
  );
497
557
  }