@react-pakistan/util-functions 1.24.1 → 1.24.3

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.
@@ -5,17 +5,21 @@ export interface CallbackParams {
5
5
  }
6
6
  interface Params {
7
7
  byIdCallback: (d: CallbackParams) => void;
8
+ byIdDeps?: Array<any> | Array<string>;
8
9
  byIdParams: object;
9
10
  deleteCallback?: (d: CallbackParams) => void;
11
+ deleteDeps?: Array<any> | Array<string>;
10
12
  deleteParams?: object;
11
13
  listCallback: (d: CallbackParams) => void;
14
+ listDeps?: Array<any> | Array<string>;
12
15
  listParams: object;
13
16
  listUrl: string;
17
+ searchQuery: string;
14
18
  unitByIdUrl: string;
15
19
  unitUrl: string;
16
20
  updateCallback: (d: CallbackParams) => void;
21
+ updateDeps?: Array<any> | Array<string>;
17
22
  updateParams: object;
18
- searchQuery: string;
19
23
  }
20
24
  interface Return {
21
25
  byIdError: Error;
@@ -31,5 +35,5 @@ interface Return {
31
35
  updateFetchNow: (url?: string, config?: FetchConfig) => void;
32
36
  updateLoading: boolean;
33
37
  }
34
- export declare const useModuleEntity: ({ byIdCallback, byIdParams, deleteCallback, deleteParams, listCallback, listParams, listUrl, unitByIdUrl, unitUrl, updateCallback, updateParams, searchQuery, }: Params) => Return;
38
+ export declare const useModuleEntity: ({ byIdCallback, byIdDeps, byIdParams, deleteCallback, deleteDeps, deleteParams, listCallback, listDeps, listParams, listUrl, searchQuery, unitByIdUrl, unitUrl, updateCallback, updateDeps, updateParams, }: Params) => Return;
35
39
  export {};
@@ -1,37 +1,46 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
4
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
5
+ if (ar || !(i in from)) {
6
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
7
+ ar[i] = from[i];
8
+ }
9
+ }
10
+ return to.concat(ar || Array.prototype.slice.call(from));
11
+ };
3
12
  Object.defineProperty(exports, "__esModule", { value: true });
4
13
  exports.useModuleEntity = void 0;
5
14
  var constants_1 = require("../constants");
6
15
  var use_fetch_1 = require("./use-fetch");
7
16
  var use_debounce_1 = require("./use-debounce");
8
17
  var useModuleEntity = function (_a) {
9
- var byIdCallback = _a.byIdCallback, byIdParams = _a.byIdParams, deleteCallback = _a.deleteCallback, deleteParams = _a.deleteParams, listCallback = _a.listCallback, listParams = _a.listParams, listUrl = _a.listUrl, unitByIdUrl = _a.unitByIdUrl, unitUrl = _a.unitUrl, updateCallback = _a.updateCallback, updateParams = _a.updateParams, searchQuery = _a.searchQuery;
18
+ var byIdCallback = _a.byIdCallback, _b = _a.byIdDeps, byIdDeps = _b === void 0 ? [] : _b, byIdParams = _a.byIdParams, deleteCallback = _a.deleteCallback, _c = _a.deleteDeps, deleteDeps = _c === void 0 ? [] : _c, deleteParams = _a.deleteParams, listCallback = _a.listCallback, _d = _a.listDeps, listDeps = _d === void 0 ? [] : _d, listParams = _a.listParams, listUrl = _a.listUrl, searchQuery = _a.searchQuery, unitByIdUrl = _a.unitByIdUrl, unitUrl = _a.unitUrl, updateCallback = _a.updateCallback, _e = _a.updateDeps, updateDeps = _e === void 0 ? [] : _e, updateParams = _a.updateParams;
10
19
  var debouncedQuery = (0, use_debounce_1.useDebounce)(searchQuery, 800);
11
20
  // list
12
- var _b = (0, use_fetch_1.useFetch)(listUrl, {
21
+ var _f = (0, use_fetch_1.useFetch)(listUrl, {
13
22
  method: constants_1.API_METHODS.POST,
14
23
  body: JSON.stringify(listParams),
15
24
  callback: listCallback,
16
- }, [debouncedQuery]), listError = _b.error, listFetchNow = _b.fetchNow, listLoading = _b.loading;
25
+ }, __spreadArray([debouncedQuery], listDeps, true)), listError = _f.error, listFetchNow = _f.fetchNow, listLoading = _f.loading;
17
26
  // create / edit
18
- var _c = (0, use_fetch_1.useFetch)(unitUrl, {
27
+ var _g = (0, use_fetch_1.useFetch)(unitUrl, {
19
28
  method: constants_1.API_METHODS.POST,
20
29
  body: JSON.stringify(updateParams),
21
30
  callback: updateCallback,
22
- }), updateError = _c.error, updateLoading = _c.loading, updateFetchNow = _c.fetchNow;
31
+ }, __spreadArray([], updateDeps, true)), updateError = _g.error, updateLoading = _g.loading, updateFetchNow = _g.fetchNow;
23
32
  // by id
24
- var _d = (0, use_fetch_1.useFetch)(unitByIdUrl, {
33
+ var _h = (0, use_fetch_1.useFetch)(unitByIdUrl, {
25
34
  method: constants_1.API_METHODS.POST,
26
35
  body: JSON.stringify(byIdParams),
27
36
  callback: byIdCallback,
28
- }), byIdError = _d.error, byIdLoading = _d.loading, byIdFetchNow = _d.fetchNow;
37
+ }, __spreadArray([], byIdDeps, true)), byIdError = _h.error, byIdLoading = _h.loading, byIdFetchNow = _h.fetchNow;
29
38
  // delete
30
- var _e = (0, use_fetch_1.useFetch)(unitUrl, {
39
+ var _j = (0, use_fetch_1.useFetch)(unitUrl, {
31
40
  method: constants_1.API_METHODS.DELETE,
32
41
  body: JSON.stringify(deleteParams),
33
42
  callback: deleteCallback,
34
- }), deleteError = _e.error, deleteLoading = _e.loading, deleteFetchNow = _e.fetchNow;
43
+ }, __spreadArray([], deleteDeps, true)), deleteError = _j.error, deleteLoading = _j.loading, deleteFetchNow = _j.fetchNow;
35
44
  return {
36
45
  byIdError: byIdError,
37
46
  byIdFetchNow: byIdFetchNow,
package/index.d.ts CHANGED
@@ -25,3 +25,4 @@ export * from './general';
25
25
  export * from './hooks';
26
26
  export * from './local-storage';
27
27
  export * from './storybook';
28
+ export * from './module/my-module';
package/index.js CHANGED
@@ -41,3 +41,4 @@ __exportStar(require("./general"), exports);
41
41
  __exportStar(require("./hooks"), exports);
42
42
  __exportStar(require("./local-storage"), exports);
43
43
  __exportStar(require("./storybook"), exports);
44
+ __exportStar(require("./module/my-module"), exports);
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ type State = {
3
+ count: number;
4
+ isLoading: boolean;
5
+ };
6
+ type Action = {
7
+ type: 'INCREMENT';
8
+ } | {
9
+ type: 'DECREMENT';
10
+ } | {
11
+ type: 'SET_LOADING';
12
+ payload: boolean;
13
+ };
14
+ type MyModuleProviderProps = {
15
+ children: React.ReactNode;
16
+ initialState?: Partial<State>;
17
+ };
18
+ export declare const MyModuleProvider: React.FC<MyModuleProviderProps>;
19
+ export declare const useMyModule: () => {
20
+ state: State;
21
+ dispatch: React.Dispatch<Action>;
22
+ };
23
+ export declare const Counter: React.FC;
24
+ export {};
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ /* eslint-disable */
3
+ var __assign = (this && this.__assign) || function () {
4
+ __assign = Object.assign || function(t) {
5
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
6
+ s = arguments[i];
7
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
+ t[p] = s[p];
9
+ }
10
+ return t;
11
+ };
12
+ return __assign.apply(this, arguments);
13
+ };
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
26
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
27
+ }) : function(o, v) {
28
+ o["default"] = v;
29
+ });
30
+ var __importStar = (this && this.__importStar) || (function () {
31
+ var ownKeys = function(o) {
32
+ ownKeys = Object.getOwnPropertyNames || function (o) {
33
+ var ar = [];
34
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
35
+ return ar;
36
+ };
37
+ return ownKeys(o);
38
+ };
39
+ return function (mod) {
40
+ if (mod && mod.__esModule) return mod;
41
+ var result = {};
42
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ })();
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.Counter = exports.useMyModule = exports.MyModuleProvider = void 0;
49
+ var react_1 = __importStar(require("react"));
50
+ // 2. Create a context for your module
51
+ var MyModuleContext = (0, react_1.createContext)(undefined);
52
+ // 3. Reducer function
53
+ var reducer = function (state, action) {
54
+ switch (action.type) {
55
+ case 'INCREMENT':
56
+ return __assign(__assign({}, state), { count: state.count + 1 });
57
+ case 'DECREMENT':
58
+ return __assign(__assign({}, state), { count: state.count - 1 });
59
+ case 'SET_LOADING':
60
+ return __assign(__assign({}, state), { isLoading: action.payload });
61
+ default:
62
+ return state;
63
+ }
64
+ };
65
+ var MyModuleProvider = function (_a) {
66
+ var children = _a.children, initialState = _a.initialState;
67
+ var _b = (0, react_1.useReducer)(reducer, __assign({ count: 0, isLoading: false }, initialState)), state = _b[0], dispatch = _b[1];
68
+ return (react_1.default.createElement(MyModuleContext.Provider, { value: { state: state, dispatch: dispatch } }, children));
69
+ };
70
+ exports.MyModuleProvider = MyModuleProvider;
71
+ // 5. Custom hook to access the context
72
+ var useMyModule = function () {
73
+ var context = (0, react_1.useContext)(MyModuleContext);
74
+ if (context === undefined) {
75
+ throw new Error('useMyModule must be used within a MyModuleProvider');
76
+ }
77
+ return context;
78
+ };
79
+ exports.useMyModule = useMyModule;
80
+ // 6. Your actual module components
81
+ var Counter = function () {
82
+ var _a = (0, exports.useMyModule)(), state = _a.state, dispatch = _a.dispatch;
83
+ return (react_1.default.createElement("div", null,
84
+ react_1.default.createElement("button", { onClick: function () { return dispatch({ type: 'DECREMENT' }); } }, "-"),
85
+ react_1.default.createElement("span", null, state.count),
86
+ react_1.default.createElement("button", { onClick: function () { return dispatch({ type: 'INCREMENT' }); } }, "+")));
87
+ };
88
+ exports.Counter = Counter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.24.1",
3
+ "version": "1.24.3",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {