@m4l/layouts 9.3.18 → 9.4.0-AQ20260114.beta.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/hooks/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './useDynamicAccordions';
4
4
  export * from './useDynamicPaperForm';
5
5
  export * from './useNetworkActionConfirm';
6
6
  export * from './useFormAddEdit';
7
+ export * from './useRefreshData';
@@ -0,0 +1,4 @@
1
+ export declare const USE_REFRESH_DATA_DICTIONARY: {
2
+ readonly TOAST_NEW_ROW_BUT_NOT_VISIBLE_TITLE: "refresh.toast_new_row_but_not_visible_title";
3
+ readonly TOAST_NEW_ROW_BUT_NOT_VISIBLE_DESCRIPTION: "refresh.toast_new_row_but_not_visible_description";
4
+ };
@@ -0,0 +1,7 @@
1
+ const USE_REFRESH_DATA_DICTIONARY = {
2
+ TOAST_NEW_ROW_BUT_NOT_VISIBLE_TITLE: "refresh.toast_new_row_but_not_visible_title",
3
+ TOAST_NEW_ROW_BUT_NOT_VISIBLE_DESCRIPTION: "refresh.toast_new_row_but_not_visible_description"
4
+ };
5
+ export {
6
+ USE_REFRESH_DATA_DICTIONARY as U
7
+ };
@@ -0,0 +1,2 @@
1
+ export * from './useRefreshData';
2
+ export type { UseRefreshData } from './types';
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,35 @@
1
+ import { GetLabelType } from '@m4l/core';
2
+ export interface UseRefreshData<TRow> {
3
+ /**
4
+ * Function to refresh the data
5
+ */
6
+ refreshPaginate: () => void;
7
+ /**
8
+ * Rows to refresh
9
+ */
10
+ rows: TRow[];
11
+ /**
12
+ * Function to get the label
13
+ */
14
+ getLabel: GetLabelType;
15
+ }
16
+ /**
17
+ * fieldValue with generic that allow to use nested attributes from a interface or object
18
+ * example: 'fieldValue' = 'object.field1.field2'
19
+ */
20
+ export type DeepKeyOf<TRow> = TRow extends object ? {
21
+ [Key in keyof TRow & string]: NonNullable<TRow[Key]> extends object ? Key | `${Key}.${DeepKeyOf<NonNullable<TRow[Key]>>}` : Key;
22
+ }[keyof TRow & string] : never;
23
+ /**
24
+ * Interface to trigger the refresh when a specific attribute is equal to a specific value
25
+ */
26
+ export interface TriggerRefreshWhen<TRow> {
27
+ /**
28
+ * Attribute to check if it is equal to the value
29
+ */
30
+ attribute: DeepKeyOf<TRow>;
31
+ /**
32
+ * Value to compare with the attribute
33
+ */
34
+ equals: string | number | boolean;
35
+ }
@@ -0,0 +1,12 @@
1
+ import { RowKey } from '@m4l/components';
2
+ import { TriggerRefreshWhen, UseRefreshData } from './types';
3
+ /**
4
+ * Hook that manages the refresh of the data and the selected rows.
5
+ * Note: This hook could be use in every module that needs to refresh the data and the selected rows.
6
+ */
7
+ export declare const useRefreshData: <TRow>(useRefreshDataProps: UseRefreshData<TRow>) => {
8
+ refresh: (rowParam?: TRow | RowKey, triggerSelectedRowWhen?: TriggerRefreshWhen<TRow>) => void;
9
+ onSelectedRowsChange: (newRowsSelectSet: ReadonlySet<RowKey>) => void;
10
+ selectedRows: ReadonlySet<RowKey>;
11
+ focusOnRowKey: RowKey | undefined;
12
+ };
@@ -0,0 +1,56 @@
1
+ import { useState, useEffect, useCallback } from "react";
2
+ import { getDataGridRowsFromSet } from "@m4l/components";
3
+ import { useHostTools } from "@m4l/core";
4
+ import { u as useMasterDetail } from "../useMasterDetail/index.js";
5
+ import { U as USE_REFRESH_DATA_DICTIONARY } from "./dictionary.js";
6
+ const useRefreshData = (useRefreshDataProps) => {
7
+ const { rows, refreshPaginate, getLabel } = useRefreshDataProps;
8
+ const { toast } = useHostTools();
9
+ const { onChangeMasterSelection } = useMasterDetail();
10
+ const [validateShowRowCreate, setValidateShowRowCreate] = useState({ show: false, idRow: void 0 });
11
+ const [selectedRows, setSelectedRows] = useState(/* @__PURE__ */ new Set());
12
+ const [focusOnRowKey, setFocusOnRowKey] = useState(void 0);
13
+ const onSelectedRowsChange = (newRowsSelectSet) => {
14
+ setSelectedRows(newRowsSelectSet);
15
+ onChangeMasterSelection(getDataGridRowsFromSet(newRowsSelectSet, rows, (row) => row?.id));
16
+ };
17
+ useEffect(() => {
18
+ if (selectedRows.size > 0) {
19
+ onChangeMasterSelection(getDataGridRowsFromSet(selectedRows, rows, (row) => row?.id));
20
+ }
21
+ }, [rows]);
22
+ useEffect(() => {
23
+ if (validateShowRowCreate.show) {
24
+ const existRowSelected = rows.some((row) => selectedRows.has(row?.id));
25
+ if (!existRowSelected && validateShowRowCreate.idRow) {
26
+ setSelectedRows?.(/* @__PURE__ */ new Set());
27
+ toast({
28
+ title: getLabel(USE_REFRESH_DATA_DICTIONARY.TOAST_NEW_ROW_BUT_NOT_VISIBLE_TITLE),
29
+ description: getLabel(USE_REFRESH_DATA_DICTIONARY.TOAST_NEW_ROW_BUT_NOT_VISIBLE_DESCRIPTION, validateShowRowCreate.idRow)
30
+ }, { type: "info" });
31
+ }
32
+ }
33
+ }, [rows]);
34
+ const getRowKey = useCallback((rowParam) => {
35
+ if (typeof rowParam === "string" || typeof rowParam === "number") {
36
+ return rowParam;
37
+ }
38
+ if (typeof rowParam === "object" && rowParam !== null && "id" in rowParam && (typeof rowParam?.id === "string" || typeof rowParam?.id === "number")) {
39
+ return rowParam?.id;
40
+ }
41
+ return void 0;
42
+ }, []);
43
+ const refresh = useCallback((rowParam, triggerSelectedRowWhen) => {
44
+ const idRow = getRowKey(rowParam);
45
+ setFocusOnRowKey(idRow);
46
+ setValidateShowRowCreate({ show: idRow !== void 0, idRow });
47
+ if (idRow !== void 0 && triggerSelectedRowWhen && typeof rowParam === "object" && Object.prototype.hasOwnProperty.call(rowParam, triggerSelectedRowWhen.attribute) && rowParam[triggerSelectedRowWhen.attribute] === triggerSelectedRowWhen.equals) {
48
+ setSelectedRows(/* @__PURE__ */ new Set([idRow]));
49
+ }
50
+ refreshPaginate();
51
+ }, [refreshPaginate, setSelectedRows, getRowKey]);
52
+ return { refresh, onSelectedRowsChange, selectedRows, focusOnRowKey };
53
+ };
54
+ export {
55
+ useRefreshData as u
56
+ };
@@ -0,0 +1 @@
1
+ export {};
package/index.js CHANGED
@@ -15,6 +15,7 @@ import { u as u3 } from "./hooks/useDynamicAccordions/useDynamicAccordions.js";
15
15
  import { c, u as u4 } from "./hooks/useDynamicPaperForm/useDynamicPaperForm.js";
16
16
  import { u as u5 } from "./hooks/useNetworkActionConfirm/useNetworkActionConfirm.js";
17
17
  import { u as u6 } from "./hooks/useFormAddEdit/useFormAddEdit.js";
18
+ import { u as u7 } from "./hooks/useRefreshData/useRefreshData.js";
18
19
  import { c as c2 } from "./utils/createAppMF.js";
19
20
  export {
20
21
  D as DynamicTabs,
@@ -36,5 +37,6 @@ export {
36
37
  u6 as useFormAddEdit,
37
38
  u as useMasterDetail,
38
39
  u2 as useModule,
39
- u5 as useNetworkActionConfirm
40
+ u5 as useNetworkActionConfirm,
41
+ u7 as useRefreshData
40
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m4l/layouts",
3
- "version": "9.3.18",
3
+ "version": "9.4.0-AQ20260114.beta.1",
4
4
  "license": "UNLICENSED",
5
5
  "author": "M4L Team",
6
6
  "lint-staged": {