@react-pakistan/util-functions 1.23.91 → 1.23.93

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.
@@ -17,4 +17,4 @@ export declare enum API_RESULT {
17
17
  SUCCESS = "SUCCESS",
18
18
  ERROR = "ERROR"
19
19
  }
20
- export declare const useFetch: (url: string, config?: FetchConfig) => Return;
20
+ export declare const useFetch: (url: string, config?: FetchConfig, deps?: Array<string> | Array<any>) => Return;
@@ -58,6 +58,15 @@ var __rest = (this && this.__rest) || function (s, e) {
58
58
  }
59
59
  return t;
60
60
  };
61
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
62
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
63
+ if (ar || !(i in from)) {
64
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
65
+ ar[i] = from[i];
66
+ }
67
+ }
68
+ return to.concat(ar || Array.prototype.slice.call(from));
69
+ };
61
70
  Object.defineProperty(exports, "__esModule", { value: true });
62
71
  exports.useFetch = exports.API_RESULT = void 0;
63
72
  var react_1 = require("react");
@@ -66,8 +75,9 @@ var API_RESULT;
66
75
  API_RESULT["SUCCESS"] = "SUCCESS";
67
76
  API_RESULT["ERROR"] = "ERROR";
68
77
  })(API_RESULT || (exports.API_RESULT = API_RESULT = {}));
69
- var useFetch = function (url, config) {
78
+ var useFetch = function (url, config, deps) {
70
79
  if (config === void 0) { config = {}; }
80
+ if (deps === void 0) { deps = []; }
71
81
  var callback = config.callback, _a = config.autoFetch, autoFetch = _a === void 0 ? false : _a, defaultOptions = __rest(config, ["callback", "autoFetch"]);
72
82
  var _b = (0, react_1.useState)(null), data = _b[0], setData = _b[1];
73
83
  var _c = (0, react_1.useState)(null), error = _c[0], setError = _c[1];
@@ -132,7 +142,11 @@ var useFetch = function (url, config) {
132
142
  case 6: return [2 /*return*/];
133
143
  }
134
144
  });
135
- }); }, [url, defaultOptions, callback]);
145
+ }); }, __spreadArray([
146
+ url,
147
+ defaultOptions,
148
+ callback
149
+ ], deps, true));
136
150
  (0, react_1.useEffect)(function () {
137
151
  if (autoFetch) {
138
152
  fetchNow();
@@ -0,0 +1,35 @@
1
+ import { FetchConfig } from './use-fetch';
2
+ export interface CallbackParams {
3
+ data: any;
4
+ error: any;
5
+ }
6
+ interface Params {
7
+ byIdCallback: (d: CallbackParams) => void;
8
+ byIdParams: object;
9
+ deleteCallback: (d: CallbackParams) => void;
10
+ deleteParams: object;
11
+ listCallback: (d: CallbackParams) => void;
12
+ listParams: object;
13
+ listUrl: string;
14
+ unitByIdUrl: string;
15
+ unitUrl: string;
16
+ updateCallback: (d: CallbackParams) => void;
17
+ updateParams: object;
18
+ searchQuery: string;
19
+ }
20
+ interface Return {
21
+ byIdError: Error;
22
+ byIdFetchNow: (url?: string, config?: FetchConfig) => void;
23
+ byIdLoading: boolean;
24
+ deleteError: Error;
25
+ deleteFetchNow: (url?: string, config?: FetchConfig) => void;
26
+ deleteLoading: boolean;
27
+ listError: Error;
28
+ listFetchNow: () => void;
29
+ listLoading: boolean;
30
+ updateError: Error;
31
+ updateFetchNow: () => void;
32
+ updateLoading: boolean;
33
+ }
34
+ export declare const useModuleEntity: ({ byIdCallback, byIdParams, deleteCallback, deleteParams, listCallback, listParams, listUrl, unitByIdUrl, unitUrl, updateCallback, updateParams, searchQuery, }: Params) => Return;
35
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useModuleEntity = void 0;
5
+ var constants_1 = require("../constants");
6
+ var use_fetch_1 = require("./use-fetch");
7
+ var use_debounce_1 = require("./use-debounce");
8
+ 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;
10
+ var debouncedQuery = (0, use_debounce_1.useDebounce)(searchQuery, 800);
11
+ // list
12
+ var _b = (0, use_fetch_1.useFetch)(listUrl, {
13
+ method: constants_1.API_METHODS.POST,
14
+ body: JSON.stringify(listParams),
15
+ callback: listCallback,
16
+ }, [debouncedQuery]), listError = _b.error, listFetchNow = _b.fetchNow, listLoading = _b.loading;
17
+ // create / edit
18
+ var _c = (0, use_fetch_1.useFetch)(unitUrl, {
19
+ method: constants_1.API_METHODS.POST,
20
+ body: JSON.stringify(updateParams),
21
+ callback: updateCallback,
22
+ }), updateError = _c.error, updateLoading = _c.loading, updateFetchNow = _c.fetchNow;
23
+ // by id
24
+ var _d = (0, use_fetch_1.useFetch)(unitByIdUrl, {
25
+ method: constants_1.API_METHODS.POST,
26
+ body: JSON.stringify(byIdParams),
27
+ callback: byIdCallback,
28
+ }), byIdError = _d.error, byIdLoading = _d.loading, byIdFetchNow = _d.fetchNow;
29
+ // delete
30
+ var _e = (0, use_fetch_1.useFetch)(unitUrl, {
31
+ method: constants_1.API_METHODS.DELETE,
32
+ body: JSON.stringify(deleteParams),
33
+ callback: deleteCallback,
34
+ }), deleteError = _e.error, deleteLoading = _e.loading, deleteFetchNow = _e.fetchNow;
35
+ return {
36
+ byIdError: byIdError,
37
+ byIdFetchNow: byIdFetchNow,
38
+ byIdLoading: byIdLoading,
39
+ deleteError: deleteError,
40
+ deleteFetchNow: deleteFetchNow,
41
+ deleteLoading: deleteLoading,
42
+ listError: listError,
43
+ listFetchNow: listFetchNow,
44
+ listLoading: listLoading,
45
+ updateError: updateError,
46
+ updateFetchNow: updateFetchNow,
47
+ updateLoading: updateLoading,
48
+ };
49
+ };
50
+ exports.useModuleEntity = useModuleEntity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.23.91",
3
+ "version": "1.23.93",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {