@m4l/core 0.0.26 → 0.0.27

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.
@@ -1,8 +1,8 @@
1
1
  import { createContext, useState, useLayoutEffect, useCallback } from "react";
2
+ import { u as useFlags } from "../../hooks/useFlags/index.js";
2
3
  import { u as useHostTools } from "../../hooks/useHostTools/index.js";
3
4
  import { u as useNetwork } from "../../hooks/useNetwork/index.js";
4
5
  import { u as useEnvironment } from "../../hooks/useEnvironment/index.js";
5
- import { u as useFlags } from "../../hooks/useFlags/index.js";
6
6
  import { jsx } from "react/jsx-runtime";
7
7
  const initialState = {
8
8
  getLabel: () => "..",
@@ -1,6 +1,9 @@
1
- export { useLocalStorage } from './useLocalStorage';
1
+ export { useFlags, useFlagsPresent } from './useFlags';
2
2
  export { useHostTools } from './useHostTools';
3
+ export { useLocalStorage } from './useLocalStorage';
4
+ export { useModuleDictionary } from './useModuleDictionary';
3
5
  export { useNetwork } from './useNetwork';
4
6
  export { useEnvironment } from './useEnvironment';
5
- export { useFlags, useFlagsPresent } from './useFlags';
6
- export { useModuleDictionary } from './useModuleDictionary';
7
+ export { usePaginate } from './usePaginate';
8
+ export type { PagerState } from './usePaginate/types';
9
+ export { initialPagerState } from './usePaginate/types';
@@ -0,0 +1,15 @@
1
+ import type { PagerState } from './types';
2
+ export interface propsUsePaginate {
3
+ endPoint: string;
4
+ timeout?: number;
5
+ fireOnEnter?: boolean;
6
+ queryParams?: Record<string, unknown>;
7
+ }
8
+ export declare const usePaginate: <TRow>(props: propsUsePaginate) => {
9
+ onPageChange: (newPage: number) => void;
10
+ onRowsPerPageChange: (newRowsPerPage: number) => void;
11
+ pagerState: PagerState;
12
+ rows: TRow[];
13
+ clearRows: () => void;
14
+ Refresh: () => void;
15
+ };
@@ -0,0 +1,72 @@
1
+ import { useState, useRef, useCallback, useEffect } from "react";
2
+ import { u as useHostTools } from "../useHostTools/index.js";
3
+ import { u as useNetwork } from "../useNetwork/index.js";
4
+ const initialPagerState = {
5
+ page: 0,
6
+ rowsPerPage: 25,
7
+ totalRecords: 0
8
+ };
9
+ const usePaginate = (props) => {
10
+ const { endPoint, timeout = 5e3, queryParams = {}, fireOnEnter = true } = props;
11
+ const [refresh, setRefresh] = useState(0);
12
+ const [rows, setRows] = useState([]);
13
+ const [pagerState, setPagerState] = useState(initialPagerState);
14
+ const refPagerState = useRef(initialPagerState);
15
+ const [fire, setFire] = useState(fireOnEnter);
16
+ const { startProgress, stopProgress } = useHostTools();
17
+ const { networkOperation } = useNetwork();
18
+ const Refresh = useCallback(() => {
19
+ setRefresh((oldValue) => oldValue + 1);
20
+ }, []);
21
+ useEffect(() => {
22
+ let mounted = true;
23
+ if (!fire) {
24
+ setFire(true);
25
+ return;
26
+ }
27
+ startProgress();
28
+ networkOperation({
29
+ method: "GET",
30
+ endPoint,
31
+ timeout,
32
+ parms: {
33
+ ...queryParams,
34
+ page: refPagerState.current.page,
35
+ limit: refPagerState.current.rowsPerPage
36
+ }
37
+ }).then((response) => {
38
+ if (mounted) {
39
+ setRows(response.data);
40
+ refPagerState.current.page = response.pager.page;
41
+ setPagerState((oldPagerState) => ({
42
+ ...oldPagerState,
43
+ page: response.pager.page,
44
+ totalRecords: response.pager.total
45
+ }));
46
+ }
47
+ }).finally(() => {
48
+ if (mounted) {
49
+ stopProgress();
50
+ }
51
+ });
52
+ return function cleanUp() {
53
+ stopProgress();
54
+ mounted = false;
55
+ };
56
+ }, [refresh, queryParams]);
57
+ const onPageChange = (newPage) => {
58
+ refPagerState.current.page = newPage;
59
+ setRefresh((oldValue) => oldValue + 1);
60
+ };
61
+ const onRowsPerPageChange = (newRowsPerPage) => {
62
+ refPagerState.current.rowsPerPage = newRowsPerPage;
63
+ setPagerState((oldPagerState) => ({ ...oldPagerState, rowsPerPage: newRowsPerPage }));
64
+ setRefresh((oldValue) => oldValue + 1);
65
+ };
66
+ const clearRows = () => {
67
+ setRows([]);
68
+ setPagerState(initialPagerState);
69
+ };
70
+ return { onPageChange, onRowsPerPageChange, pagerState, rows, clearRows, Refresh };
71
+ };
72
+ export { initialPagerState as i, usePaginate as u };
@@ -0,0 +1,6 @@
1
+ export declare interface PagerState {
2
+ page: number;
3
+ rowsPerPage: number;
4
+ totalRecords: number;
5
+ }
6
+ export declare const initialPagerState: PagerState;
package/dist/index.js CHANGED
@@ -3,12 +3,13 @@ export { H as HostToolsContext, a as HostToolsProvider } from "./contexts/HostTo
3
3
  export { N as NetworkContext, a as NetworkProvider } from "./contexts/NetworkContext/index.js";
4
4
  export { F as FlagsContext, a as FlagsProvider } from "./contexts/FlagsContext/index.js";
5
5
  export { M as ModuleDictionaryContext, a as ModuleDictionaryProvider } from "./contexts/ModuleDictionaryContext/index.js";
6
- export { u as useLocalStorage } from "./hooks/useLocalStorage/index.js";
6
+ export { u as useFlags, a as useFlagsPresent } from "./hooks/useFlags/index.js";
7
7
  export { u as useHostTools } from "./hooks/useHostTools/index.js";
8
+ export { u as useLocalStorage } from "./hooks/useLocalStorage/index.js";
9
+ export { u as useModuleDictionary } from "./hooks/useModuleDictionary/index.js";
8
10
  export { u as useNetwork } from "./hooks/useNetwork/index.js";
9
11
  export { u as useEnvironment } from "./hooks/useEnvironment/index.js";
10
- export { u as useFlags, a as useFlagsPresent } from "./hooks/useFlags/index.js";
11
- export { u as useModuleDictionary } from "./hooks/useModuleDictionary/index.js";
12
+ export { i as initialPagerState, u as usePaginate } from "./hooks/usePaginate/index.js";
12
13
  export { E as EmitEvents } from "./types/index.js";
13
14
  export { a as getLocalStorage, g as getPropertyByString, s as setLocalStorage, v as voidFunction } from "./utils/index.js";
14
15
  export { a as axiosOperation } from "./external/axios.js";
package/dist/vendor.js CHANGED
@@ -1,15 +1,16 @@
1
1
  import "react";
2
+ import "./contexts/FlagsContext/index.js";
2
3
  import "./contexts/HostToolsContext/index.js";
4
+ import "./contexts/ModuleDictionaryContext/index.js";
3
5
  import "./contexts/NetworkContext/index.js";
4
6
  import "./contexts/EnvironmentContext/index.js";
5
- import "./contexts/FlagsContext/index.js";
6
- import "./contexts/ModuleDictionaryContext/index.js";
7
- import "./hooks/useLocalStorage/index.js";
7
+ import "./hooks/useFlags/index.js";
8
8
  import "./hooks/useHostTools/index.js";
9
+ import "./hooks/useLocalStorage/index.js";
10
+ import "./hooks/useModuleDictionary/index.js";
9
11
  import "./hooks/useNetwork/index.js";
10
12
  import "./hooks/useEnvironment/index.js";
11
- import "./hooks/useFlags/index.js";
12
- import "./hooks/useModuleDictionary/index.js";
13
+ import "./hooks/usePaginate/index.js";
13
14
  import "./types/index.js";
14
15
  import "./utils/index.js";
15
16
  import "./external/axios.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.26",
4
+ "version": "0.0.27",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {