@appcorp/stellar-solutions-invoice-module 0.1.74 → 0.1.75

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.
Files changed (89) hide show
  1. package/base-modules/invoice/add-service-product-section.d.ts +1 -5
  2. package/base-modules/invoice/add-service-product-section.js +3 -3
  3. package/base-modules/invoice/cache.d.ts +15 -0
  4. package/base-modules/invoice/cache.js +50 -0
  5. package/base-modules/invoice/calculate-subtotal.d.ts +6 -1
  6. package/base-modules/invoice/calculate-subtotal.js +2 -2
  7. package/base-modules/invoice/calculate-total.d.ts +1 -1
  8. package/base-modules/invoice/calculate-total.js +8 -8
  9. package/base-modules/invoice/company-form-section.d.ts +1 -5
  10. package/base-modules/invoice/company-form-section.js +25 -22
  11. package/base-modules/invoice/constants.d.ts +2 -3
  12. package/base-modules/invoice/constants.js +2 -3
  13. package/base-modules/invoice/context.d.ts +556 -20
  14. package/base-modules/invoice/context.js +530 -342
  15. package/base-modules/invoice/customer-form-section.d.ts +1 -5
  16. package/base-modules/invoice/customer-form-section.js +25 -9
  17. package/base-modules/invoice/form.d.ts +1 -5
  18. package/base-modules/invoice/form.js +9 -9
  19. package/base-modules/invoice/page.d.ts +8 -0
  20. package/base-modules/invoice/page.js +171 -0
  21. package/base-modules/invoice/pricing-form-section.d.ts +1 -5
  22. package/base-modules/invoice/pricing-form-section.js +16 -11
  23. package/base-modules/invoice/products-form-section.d.ts +1 -5
  24. package/base-modules/invoice/products-form-section.js +49 -40
  25. package/base-modules/invoice/services-form-section.d.ts +1 -5
  26. package/base-modules/invoice/services-form-section.js +10 -9
  27. package/base-modules/invoice/validate.d.ts +83 -25
  28. package/base-modules/invoice/validate.js +4 -4
  29. package/base-modules/payment/cache.d.ts +15 -0
  30. package/base-modules/payment/cache.js +52 -0
  31. package/base-modules/payment/constants.d.ts +1 -14
  32. package/base-modules/payment/constants.js +2 -15
  33. package/base-modules/payment/context.d.ts +214 -48
  34. package/base-modules/payment/context.js +266 -237
  35. package/base-modules/payment/drawer.d.ts +0 -6
  36. package/base-modules/payment/drawer.js +89 -44
  37. package/base-modules/payment/form.js +3 -2
  38. package/base-modules/payment/{payment.js → page.js} +2 -5
  39. package/base-modules/payment/types.d.ts +0 -85
  40. package/base-modules/payment/types.js +74 -21
  41. package/base-modules/payment/validate.d.ts +12 -0
  42. package/base-modules/payment/validate.js +19 -8
  43. package/base-modules/payment/view.d.ts +1 -5
  44. package/base-modules/payment/view.js +4 -3
  45. package/base-modules/quote/add-service-product-section.js +1 -1
  46. package/base-modules/quote/cache.d.ts +15 -0
  47. package/base-modules/quote/cache.js +50 -0
  48. package/base-modules/quote/company-form-section.d.ts +1 -1
  49. package/base-modules/quote/company-form-section.js +14 -12
  50. package/base-modules/quote/constants.js +1 -0
  51. package/base-modules/quote/context.d.ts +656 -46
  52. package/base-modules/quote/context.js +413 -289
  53. package/base-modules/quote/customer-form-section.d.ts +1 -1
  54. package/base-modules/quote/customer-form-section.js +24 -9
  55. package/base-modules/quote/form.d.ts +1 -5
  56. package/base-modules/quote/form.js +8 -9
  57. package/base-modules/quote/page.d.ts +8 -0
  58. package/base-modules/quote/page.js +163 -0
  59. package/base-modules/quote/pricing-form-section.js +9 -8
  60. package/base-modules/quote/products-form-section.js +2 -2
  61. package/base-modules/quote/services-form-section.js +1 -1
  62. package/base-modules/quote/types.d.ts +0 -127
  63. package/base-modules/quote/types.js +92 -35
  64. package/base-modules/quote/validate.d.ts +82 -25
  65. package/base-modules/quote/validate.js +3 -4
  66. package/package.json +8 -8
  67. package/base-modules/invoice/actions.d.ts +0 -215
  68. package/base-modules/invoice/actions.js +0 -38
  69. package/base-modules/invoice/drawer.d.ts +0 -13
  70. package/base-modules/invoice/drawer.js +0 -46
  71. package/base-modules/invoice/invoice.d.ts +0 -8
  72. package/base-modules/invoice/invoice.js +0 -69
  73. package/base-modules/invoice/reducer.d.ts +0 -4
  74. package/base-modules/invoice/reducer.js +0 -180
  75. package/base-modules/invoice/types.d.ts +0 -211
  76. package/base-modules/invoice/types.js +0 -43
  77. package/base-modules/payment/actions.d.ts +0 -203
  78. package/base-modules/payment/actions.js +0 -50
  79. package/base-modules/payment/reducer.d.ts +0 -29
  80. package/base-modules/payment/reducer.js +0 -215
  81. package/base-modules/quote/actions.d.ts +0 -215
  82. package/base-modules/quote/actions.js +0 -38
  83. package/base-modules/quote/drawer.d.ts +0 -13
  84. package/base-modules/quote/drawer.js +0 -46
  85. package/base-modules/quote/quote.d.ts +0 -8
  86. package/base-modules/quote/quote.js +0 -69
  87. package/base-modules/quote/reducer.d.ts +0 -4
  88. package/base-modules/quote/reducer.js +0 -181
  89. /package/base-modules/payment/{payment.d.ts → page.d.ts} +0 -0
@@ -1,35 +1,8 @@
1
1
  /**
2
- * Payment State Context
2
+ * Payment State Context - Using Generic Module Factory
3
3
  *
4
- * This module provides comprehensive state management for the Payment feature including:
5
- * - CRUD operations (create, read, update, delete)
6
- * - Form validation and error handling
7
- * - Search and filtering functionality
8
- * - Pagination controls
9
- * - Payment mode and quote invoice integration
10
- * - Currency management with default selection
11
- * - Internationalization support
12
- * - Theme-aware toast notifications
13
- *
14
- * Organization:
15
- * - Types & Interfaces
16
- * - Main Hook (usePaymentState)
17
- * - State & Core Hooks
18
- * - Debounced Values
19
- * - API Parameters (memoized)
20
- * - API Callbacks (with error handling)
21
- * - Module Entity Hook
22
- * - Effects (list refresh, currency defaults)
23
- * - Drawer & Modal Handlers
24
- * - CRUD Operation Handlers
25
- * - Form Handlers
26
- * - Pagination Handlers
27
- * - Search Handlers
28
- * - Table Actions (memoized)
29
- * - Return State
30
- * - Context Setup
31
- * - Provider Component
32
- * - Custom Hook
4
+ * This module provides comprehensive state management for the Payment feature using
5
+ * the createGenericModule factory pattern.
33
6
  */
34
7
  "use client";
35
8
  "use strict";
@@ -44,88 +17,165 @@ var __assign = (this && this.__assign) || function () {
44
17
  };
45
18
  return __assign.apply(this, arguments);
46
19
  };
47
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
48
- if (k2 === undefined) k2 = k;
49
- var desc = Object.getOwnPropertyDescriptor(m, k);
50
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
51
- desc = { enumerable: true, get: function() { return m[k]; } };
20
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ };
29
+ var __generator = (this && this.__generator) || function (thisArg, body) {
30
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
31
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
32
+ function verb(n) { return function (v) { return step([n, v]); }; }
33
+ function step(op) {
34
+ if (f) throw new TypeError("Generator is already executing.");
35
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
36
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
37
+ if (y = 0, t) op = [op[0] & 2, t.value];
38
+ switch (op[0]) {
39
+ case 0: case 1: t = op; break;
40
+ case 4: _.label++; return { value: op[1], done: false };
41
+ case 5: _.label++; y = op[1]; op = [0]; continue;
42
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
43
+ default:
44
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
45
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
46
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
47
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
48
+ if (t[2]) _.ops.pop();
49
+ _.trys.pop(); continue;
50
+ }
51
+ op = body.call(thisArg, _);
52
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
53
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
52
54
  }
53
- Object.defineProperty(o, k2, desc);
54
- }) : (function(o, m, k, k2) {
55
- if (k2 === undefined) k2 = k;
56
- o[k2] = m[k];
57
- }));
58
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
59
- Object.defineProperty(o, "default", { enumerable: true, value: v });
60
- }) : function(o, v) {
61
- o["default"] = v;
62
- });
63
- var __importStar = (this && this.__importStar) || (function () {
64
- var ownKeys = function(o) {
65
- ownKeys = Object.getOwnPropertyNames || function (o) {
66
- var ar = [];
67
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
68
- return ar;
69
- };
70
- return ownKeys(o);
71
- };
72
- return function (mod) {
73
- if (mod && mod.__esModule) return mod;
74
- var result = {};
75
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
76
- __setModuleDefault(result, mod);
77
- return result;
78
- };
79
- })();
55
+ };
56
+ var _a;
80
57
  Object.defineProperty(exports, "__esModule", { value: true });
81
- exports.usePaymentStateContext = exports.PaymentStateContextProvider = exports.PaymentStateContext = void 0;
82
- var react_1 = __importStar(require("react"));
58
+ exports.usePaymentModule = exports.initialPaymentState = exports.PAYMENT_ACTION_TYPES = exports.paymentModuleConfig = exports.usePaymentCoreContext = exports.PaymentCoreProvider = exports.paymentReducer = void 0;
59
+ var react_1 = require("react");
83
60
  var next_themes_1 = require("next-themes");
84
61
  var next_intl_1 = require("next-intl");
85
62
  var util_functions_1 = require("@react-pakistan/util-functions");
63
+ var generic_module_factory_1 = require("@react-pakistan/util-functions/factory/generic-module-factory");
64
+ var generic_component_factory_1 = require("@react-pakistan/util-functions/factory/generic-component-factory");
86
65
  var context_1 = require("@appcorp/stellar-solutions-modules/global-modules/preferences/context");
87
- var actions_1 = require("./actions");
88
66
  var constants_1 = require("./constants");
89
- var reducer_1 = require("./reducer");
90
67
  var toast_utils_1 = require("@appcorp/shadcn/lib/toast-utils");
91
- var types_1 = require("./types");
68
+ // import { PAYMENT_DRAWER, PAYMENT_TYPE, PaymentTypeBE } from "./types";
69
+ // import { PaymentModeTypeBE } from "@appcorp/stellar-solutions-modules/global-modules/payment-mode/types";
70
+ // import { QuoteInvoiceTypeBE } from "../invoice/types";
92
71
  var validate_1 = require("./validate");
72
+ var cache_1 = require("./cache");
93
73
  var context_2 = require("../invoice/context");
94
74
  // ============================================================================
95
- // MAIN HOOK
75
+ // MODULE CONFIGURATION
76
+ // ============================================================================
77
+ var paymentConfig = {
78
+ name: "Payment",
79
+ displayName: "Payment",
80
+ initialState: {
81
+ // List data
82
+ items: [],
83
+ count: 0,
84
+ // Search & Pagination
85
+ currentPage: 1,
86
+ pageLimit: constants_1.pageLimit,
87
+ searchQuery: "",
88
+ // UI state
89
+ disableSaveButton: true,
90
+ drawer: null,
91
+ modal: null,
92
+ // Errors
93
+ errors: {},
94
+ // Form data
95
+ amount: "",
96
+ attachment: "",
97
+ balance: "",
98
+ currency: "",
99
+ date: new Date().toISOString(),
100
+ description: "",
101
+ id: "",
102
+ mode: "Create",
103
+ paymentModeId: "",
104
+ paymentModeQuery: "",
105
+ paymentType: util_functions_1.PAYMENT_TYPE.FULL_AMOUNT,
106
+ quoteInvoiceId: "",
107
+ quoteInvoiceQuery: "",
108
+ ref: "",
109
+ invoice: null,
110
+ // Related entities
111
+ paymentModes: [],
112
+ quoteInvoices: [],
113
+ paymentMode: undefined,
114
+ quoteInvoice: undefined,
115
+ },
116
+ drawerTypes: __assign({}, generic_component_factory_1.DRAWER_TYPES),
117
+ };
118
+ // ============================================================================
119
+ // CREATE PAYMENT MODULE
120
+ // ============================================================================
121
+ exports.paymentReducer = (_a = (0, generic_module_factory_1.createGenericModule)(paymentConfig), _a.reducer), exports.PaymentCoreProvider = _a.Provider, exports.usePaymentCoreContext = _a.useContext, exports.paymentModuleConfig = _a.config, exports.PAYMENT_ACTION_TYPES = _a.actionTypes, exports.initialPaymentState = _a.initialState;
96
122
  // ============================================================================
97
- var usePaymentState = function () {
123
+ // ENHANCED PAYMENT HOOK WITH API INTEGRATION
124
+ // ============================================================================
125
+ // interface StateProviderProps {
126
+ // children: ReactNode;
127
+ // }
128
+ var usePaymentModule = function () {
129
+ var _a;
98
130
  // ---------------------------------------------------------------------------
99
- // STATE & CORE HOOKS
131
+ // State via factory context
100
132
  // ---------------------------------------------------------------------------
101
- var invoices = (0, context_2.useInvoiceStateContext)().invoices;
102
- var _a = (0, react_1.useReducer)(reducer_1.paymentReducer, reducer_1.initialPaymentState), state = _a[0], dispatch = _a[1];
133
+ var context = (0, exports.usePaymentCoreContext)();
134
+ var state = context.state;
135
+ var invoiceState = (0, context_2.useInvoiceModule)().state;
136
+ var invoices = (_a = invoiceState === null || invoiceState === void 0 ? void 0 : invoiceState.items) !== null && _a !== void 0 ? _a : [];
103
137
  var theme = (0, next_themes_1.useTheme)().theme;
104
138
  var t = (0, next_intl_1.useTranslations)("payment");
105
139
  var currencies = (0, context_1.usePreferenceStateContext)().currencies;
106
- // const tCommon = useTranslations("common");
107
140
  // ---------------------------------------------------------------------------
108
- // DEBOUNCED VALUES
141
+ // Toast Helpers
142
+ // ---------------------------------------------------------------------------
143
+ var showErrorToast = (0, react_1.useCallback)(function (description) {
144
+ (0, toast_utils_1.generateThemeToast)({
145
+ description: description,
146
+ theme: theme,
147
+ variant: toast_utils_1.TOAST_VARIANT.ERROR,
148
+ });
149
+ }, [theme]);
150
+ var showSuccessToast = (0, react_1.useCallback)(function (description) {
151
+ (0, toast_utils_1.generateThemeToast)({
152
+ description: description,
153
+ theme: theme,
154
+ variant: toast_utils_1.TOAST_VARIANT.SUCCESS,
155
+ });
156
+ }, [theme]);
157
+ // ---------------------------------------------------------------------------
158
+ // Debounced Values
109
159
  // ---------------------------------------------------------------------------
110
160
  var debouncedQuery = (0, util_functions_1.useDebounce)(state.searchQuery, 800);
111
161
  // ---------------------------------------------------------------------------
112
- // API PARAMETERS
162
+ // API Parameters
113
163
  // ---------------------------------------------------------------------------
114
164
  var listParams = (0, react_1.useMemo)(function () { return ({
115
165
  currentPage: state.currentPage,
166
+ includePaymentMode: true,
167
+ includeQuoteInvoice: true,
116
168
  pageLimit: state.pageLimit,
117
169
  searchQuery: state.searchQuery,
118
- includeQuoteInvoice: true,
119
- includePaymentMode: true,
120
170
  }); }, [state.currentPage, state.pageLimit, state.searchQuery]);
121
171
  var updateParams = (0, react_1.useMemo)(function () {
122
172
  var _a, _b;
123
173
  return ({
124
- amount: state.amount,
174
+ amount: parseFloat(state.amount),
125
175
  attachment: state.attachment,
126
- balance: state.balance,
176
+ balance: parseFloat(state.balance),
127
177
  currency: state.currency,
128
- date: state.date,
178
+ date: new Date(state.date),
129
179
  description: ((_a = state.description) === null || _a === void 0 ? void 0 : _a.trim()) || "",
130
180
  id: state.id,
131
181
  paymentModeId: state.paymentModeId,
@@ -137,60 +187,52 @@ var usePaymentState = function () {
137
187
  var byIdParams = (0, react_1.useMemo)(function () { return ({ id: state.id }); }, [state.id]);
138
188
  var deleteParams = (0, react_1.useMemo)(function () { return ({ id: state.id }); }, [state.id]);
139
189
  // ---------------------------------------------------------------------------
140
- // API CALLBACKS
190
+ // Currency Default
191
+ // ---------------------------------------------------------------------------
192
+ var defaultCurrency = (0, react_1.useMemo)(function () {
193
+ return currencies.filter(function (_a) {
194
+ var isDefault = _a.isDefault;
195
+ return isDefault;
196
+ })[0];
197
+ }, [currencies]);
198
+ // ---------------------------------------------------------------------------
199
+ // API Callbacks
141
200
  // ---------------------------------------------------------------------------
142
- var showErrorToast = (0, react_1.useCallback)(function (description) {
143
- (0, toast_utils_1.generateThemeToast)({
144
- description: description,
145
- theme: theme,
146
- variant: toast_utils_1.TOAST_VARIANT.ERROR,
147
- });
148
- }, [theme]);
149
- var showSuccessToast = (0, react_1.useCallback)(function (description) {
150
- (0, toast_utils_1.generateThemeToast)({
151
- description: description,
152
- theme: theme,
153
- variant: toast_utils_1.TOAST_VARIANT.SUCCESS,
154
- });
155
- }, [theme]);
156
201
  var listCallback = (0, react_1.useCallback)(function (_a) {
202
+ var _b;
157
203
  var data = _a.data, error = _a.error;
158
204
  if (error) {
159
205
  showErrorToast(t("messagesNetworkError"));
160
206
  }
161
207
  if (data === null || data === void 0 ? void 0 : data.items) {
162
- dispatch({
163
- payload: { payments: data.items },
164
- type: actions_1.PAYMENT_ACTION_TYPES.SET_PAYMENTS,
165
- });
166
- dispatch({
167
- payload: { count: data === null || data === void 0 ? void 0 : data.count },
168
- type: actions_1.PAYMENT_ACTION_TYPES.SET_COUNT,
208
+ context.dispatch({
209
+ type: exports.PAYMENT_ACTION_TYPES.SET_ITEMS,
210
+ payload: { items: data.items, count: (_b = data === null || data === void 0 ? void 0 : data.count) !== null && _b !== void 0 ? _b : 0 },
169
211
  });
170
212
  }
171
- }, [dispatch, showErrorToast, t]);
213
+ }, [t, showErrorToast]);
172
214
  var updateCallback = (0, react_1.useCallback)(function (_a) {
173
215
  var data = _a.data, error = _a.error;
174
216
  if (error) {
175
217
  showErrorToast(t("messagesNetworkError"));
176
218
  }
177
219
  if (data) {
220
+ (0, cache_1.invalidatePaymentsCache)();
178
221
  showSuccessToast(t("messagesPaymentUpdated"));
179
- dispatch({
180
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_FORM,
181
- });
182
- dispatch({
183
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_ERRORS,
222
+ context.dispatch({ type: exports.PAYMENT_ACTION_TYPES.RESET_FORM });
223
+ context.dispatch({
224
+ type: exports.PAYMENT_ACTION_TYPES.SET_ERRORS,
225
+ payload: { errors: {} },
184
226
  });
185
227
  listFetchNow();
186
- dispatch({
228
+ context.dispatch({
229
+ type: exports.PAYMENT_ACTION_TYPES.SET_DRAWER,
187
230
  payload: { drawer: null },
188
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DRAWER,
189
231
  });
190
232
  }
191
233
  },
192
234
  // eslint-disable-next-line react-hooks/exhaustive-deps
193
- [dispatch, showErrorToast, showSuccessToast, t]);
235
+ [t, showErrorToast, showSuccessToast]);
194
236
  var byIdCallback = (0, react_1.useCallback)(function (_a) {
195
237
  var data = _a.data, error = _a.error;
196
238
  if (error) {
@@ -214,33 +256,32 @@ var usePaymentState = function () {
214
256
  }
215
257
  }
216
258
  var updatedData = __assign(__assign({}, data), (normalizedCurrency_1 && { currency: normalizedCurrency_1 }));
217
- dispatch({
259
+ context.dispatch({
260
+ type: exports.PAYMENT_ACTION_TYPES.SET_FORM_DATA,
218
261
  payload: { form: updatedData },
219
- type: actions_1.PAYMENT_ACTION_TYPES.SET_FORM,
220
262
  });
221
- dispatch({
222
- payload: { drawer: types_1.PAYMENT_DRAWER.PAYMENT_VIEW_DRAWER },
223
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DRAWER,
263
+ context.dispatch({
264
+ type: exports.PAYMENT_ACTION_TYPES.SET_DRAWER,
265
+ payload: { drawer: paymentConfig.drawerTypes.FORM_DRAWER },
224
266
  });
225
267
  }
226
- }, [dispatch, showErrorToast, t, currencies]);
268
+ }, [t, showErrorToast, currencies]);
227
269
  var deleteCallback = (0, react_1.useCallback)(function (_a) {
228
270
  var data = _a.data, error = _a.error;
229
271
  if (error) {
230
272
  showErrorToast(t("messagesNetworkError"));
231
273
  }
232
274
  if (data) {
275
+ (0, cache_1.invalidatePaymentsCache)();
233
276
  showSuccessToast(t("messagesPaymentDeleted"));
234
- dispatch({
235
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_FORM,
236
- });
277
+ context.dispatch({ type: exports.PAYMENT_ACTION_TYPES.RESET_FORM });
237
278
  listFetchNow();
238
279
  }
239
280
  },
240
281
  // eslint-disable-next-line react-hooks/exhaustive-deps
241
- [dispatch, showErrorToast, showSuccessToast, t]);
282
+ [t, showErrorToast, showSuccessToast]);
242
283
  // ---------------------------------------------------------------------------
243
- // MODULE ENTITY HOOK
284
+ // Module Entity Hook
244
285
  // ---------------------------------------------------------------------------
245
286
  var _b = (0, util_functions_1.useModuleEntityV2)({
246
287
  byIdCallback: byIdCallback,
@@ -248,27 +289,44 @@ var usePaymentState = function () {
248
289
  deleteCallback: deleteCallback,
249
290
  deleteParams: deleteParams,
250
291
  listCallback: listCallback,
251
- listDeps: [dispatch],
292
+ listDeps: [context.dispatch],
252
293
  listParams: listParams,
253
- listUrl: constants_1.PAYMENT_API_ROUTES.PAYMENTS,
294
+ listUrl: constants_1.PAYMENT_API_ROUTES.UNIT,
254
295
  searchQuery: debouncedQuery,
255
- unitByIdUrl: constants_1.PAYMENT_API_ROUTES.PAYMENT_BY_ID,
256
- unitUrl: constants_1.PAYMENT_API_ROUTES.PAYMENT,
296
+ unitByIdUrl: constants_1.PAYMENT_API_ROUTES.UNIT,
297
+ unitUrl: constants_1.PAYMENT_API_ROUTES.UNIT,
257
298
  updateCallback: updateCallback,
258
- updateDeps: [state],
259
299
  updateParams: updateParams,
260
- }), byIdError = _b.byIdError, byIdFetchNow = _b.byIdFetchNow, byIdLoading = _b.byIdLoading, deleteError = _b.deleteError,
261
- // deleteFetchNow,
262
- deleteLoading = _b.deleteLoading, listError = _b.listError, listFetchNow = _b.listFetchNow, listLoading = _b.listLoading, updateError = _b.updateError, updateFetchNow = _b.updateFetchNow, updateLoading = _b.updateLoading;
300
+ }), byIdError = _b.byIdError, byIdFetchNow = _b.byIdFetchNow, byIdLoading = _b.byIdLoading, deleteError = _b.deleteError, deleteLoading = _b.deleteLoading, listError = _b.listError, listFetchNow = _b.listFetchNow, listLoading = _b.listLoading, updateError = _b.updateError, updateFetchNow = _b.updateFetchNow, updateLoading = _b.updateLoading;
263
301
  // ---------------------------------------------------------------------------
264
- // EFFECTS
302
+ // Effects
265
303
  // ---------------------------------------------------------------------------
266
- var defaultCurrency = (0, react_1.useMemo)(function () {
267
- return currencies.filter(function (_a) {
268
- var isDefault = _a.isDefault;
269
- return isDefault;
270
- })[0];
271
- }, [currencies]);
304
+ (0, react_1.useEffect)(function () {
305
+ (function () { return __awaiter(void 0, void 0, void 0, function () {
306
+ var _a, count, items, _b;
307
+ return __generator(this, function (_c) {
308
+ switch (_c.label) {
309
+ case 0:
310
+ _c.trys.push([0, 2, , 3]);
311
+ return [4 /*yield*/, (0, cache_1.getCachedPayments)({
312
+ params: listParams,
313
+ })];
314
+ case 1:
315
+ _a = _c.sent(), count = _a.count, items = _a.items;
316
+ context.dispatch({
317
+ type: exports.PAYMENT_ACTION_TYPES.SET_ITEMS,
318
+ payload: { items: items || [], count: count || 0 },
319
+ });
320
+ return [3 /*break*/, 3];
321
+ case 2:
322
+ _b = _c.sent();
323
+ showErrorToast(t("messagesNetworkError"));
324
+ return [3 /*break*/, 3];
325
+ case 3: return [2 /*return*/];
326
+ }
327
+ });
328
+ }); })();
329
+ }, [listParams]); // eslint-disable-line react-hooks/exhaustive-deps
272
330
  (0, react_1.useEffect)(function () {
273
331
  listFetchNow();
274
332
  // eslint-disable-next-line
@@ -281,8 +339,8 @@ var usePaymentState = function () {
281
339
  return id === invoiceId;
282
340
  })[0];
283
341
  if (invoice) {
284
- dispatch({
285
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
342
+ context.dispatch({
343
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
286
344
  payload: { key: "quoteInvoiceId", value: invoiceId },
287
345
  });
288
346
  // Normalize invoice currency to id (API may return id or code)
@@ -301,78 +359,71 @@ var usePaymentState = function () {
301
359
  invoiceCurrency_1 = foundByCode.id;
302
360
  }
303
361
  }
304
- dispatch({
305
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
362
+ context.dispatch({
363
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
306
364
  payload: {
307
365
  key: "currency",
308
366
  value: invoiceCurrency_1 || (defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id),
309
367
  },
310
368
  });
311
- dispatch({
312
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
369
+ context.dispatch({
370
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
313
371
  payload: { key: "amount", value: invoice === null || invoice === void 0 ? void 0 : invoice.total },
314
372
  });
315
- dispatch({
316
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
373
+ context.dispatch({
374
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
317
375
  payload: { key: "balance", value: "0.00" },
318
376
  });
319
- dispatch({
320
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INVOICE,
321
- payload: { invoice: invoice },
377
+ context.dispatch({
378
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
379
+ payload: { key: "invoice", value: invoice },
322
380
  });
323
- dispatch({
324
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DRAWER,
325
- payload: { drawer: types_1.PAYMENT_DRAWER.PAYMENT_FORM_DRAWER },
381
+ context.dispatch({
382
+ type: exports.PAYMENT_ACTION_TYPES.SET_DRAWER,
383
+ payload: { drawer: paymentConfig.drawerTypes.FORM_DRAWER },
326
384
  });
327
385
  }
328
386
  }
329
- }, [invoices, currencies, defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id]);
330
- // include currencies and defaultCurrency in deps because we read them when initializing form
387
+ }, [invoices, currencies, defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id]); // eslint-disable-line react-hooks/exhaustive-deps
331
388
  // ---------------------------------------------------------------------------
332
- // DRAWER & MODAL HANDLERS
389
+ // Drawer & Modal Handlers
333
390
  // ---------------------------------------------------------------------------
334
391
  var closeDrawer = (0, react_1.useCallback)(function () {
335
- dispatch({
392
+ context.dispatch({
393
+ type: exports.PAYMENT_ACTION_TYPES.SET_DRAWER,
336
394
  payload: { drawer: null },
337
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DRAWER,
338
- });
339
- dispatch({
340
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_FORM,
341
395
  });
342
- dispatch({
343
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_ERRORS,
396
+ context.dispatch({ type: exports.PAYMENT_ACTION_TYPES.RESET_FORM });
397
+ context.dispatch({
398
+ type: exports.PAYMENT_ACTION_TYPES.SET_ERRORS,
399
+ payload: { errors: {} },
344
400
  });
345
- }, [dispatch]);
401
+ }, [context]);
346
402
  // ---------------------------------------------------------------------------
347
- // CRUD OPERATION HANDLERS
403
+ // CRUD Operation Handlers
348
404
  // ---------------------------------------------------------------------------
349
405
  var handleView = (0, react_1.useCallback)(function (id) {
350
406
  byIdFetchNow(undefined, {
351
407
  params: {
352
408
  id: id,
353
- includeQuoteInvoice: true,
354
409
  includePaymentMode: true,
410
+ includeQuoteInvoice: true,
355
411
  },
356
412
  });
357
413
  }, [byIdFetchNow]);
358
- // const handleDelete = useCallback(
359
- // (id: string) => {
360
- // deleteFetchNow?.(undefined, {
361
- // body: JSON.stringify({ id }),
362
- // });
363
- // },
364
- // [deleteFetchNow]
365
- // );
414
+ // ---------------------------------------------------------------------------
415
+ // Form Submission Handler
416
+ // ---------------------------------------------------------------------------
366
417
  var handleSubmit = (0, react_1.useCallback)(function () {
367
- dispatch({
418
+ context.dispatch({
419
+ type: exports.PAYMENT_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
368
420
  payload: { disableSaveButton: true },
369
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
370
421
  });
371
422
  (0, util_functions_1.validateForm)({
372
423
  errorCallback: function (e) {
373
- dispatch({
424
+ context.dispatch({
425
+ type: exports.PAYMENT_ACTION_TYPES.SET_ERRORS,
374
426
  payload: { errors: e },
375
- type: actions_1.PAYMENT_ACTION_TYPES.SET_ERRORS,
376
427
  });
377
428
  },
378
429
  params: __assign(__assign({}, updateParams), { currency: updateParams.currency || (defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id) }),
@@ -384,84 +435,85 @@ var usePaymentState = function () {
384
435
  },
385
436
  });
386
437
  (0, util_functions_1.removeStorageValue)("paymentInvoiceId");
387
- }, [updateFetchNow, updateParams, dispatch, defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id]);
438
+ }, [context, updateFetchNow, updateParams, defaultCurrency === null || defaultCurrency === void 0 ? void 0 : defaultCurrency.id]);
388
439
  // ---------------------------------------------------------------------------
389
- // FORM HANDLERS
440
+ // Form Change Handlers
390
441
  // ---------------------------------------------------------------------------
391
442
  var handleChange = (0, react_1.useCallback)(function (key, value) {
392
443
  var _a, _b, _c;
393
444
  if (key === "amount" && typeof value === "string") {
394
445
  if (Number((_a = state === null || state === void 0 ? void 0 : state.invoice) === null || _a === void 0 ? void 0 : _a.total) > Number(value)) {
395
- dispatch({
396
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
397
- payload: { key: "paymentType", value: types_1.PAYMENT_TYPE.PARTIAL_AMOUNT },
446
+ context.dispatch({
447
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
448
+ payload: { key: "paymentType", value: util_functions_1.PAYMENT_TYPE.PARTIAL_AMOUNT },
398
449
  });
399
450
  }
400
451
  if (Number((_b = state === null || state === void 0 ? void 0 : state.invoice) === null || _b === void 0 ? void 0 : _b.total) === Number(value)) {
401
- dispatch({
402
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
403
- payload: { key: "paymentType", value: types_1.PAYMENT_TYPE.FULL_AMOUNT },
452
+ context.dispatch({
453
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
454
+ payload: { key: "paymentType", value: util_functions_1.PAYMENT_TYPE.FULL_AMOUNT },
404
455
  });
405
456
  }
406
- dispatch({
407
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
457
+ context.dispatch({
458
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
408
459
  payload: {
409
460
  key: "balance",
410
461
  value: Number(Number((_c = state === null || state === void 0 ? void 0 : state.invoice) === null || _c === void 0 ? void 0 : _c.total) - Number(value)).toFixed(2),
411
462
  },
412
463
  });
413
464
  }
414
- dispatch({
465
+ context.dispatch({
466
+ type: exports.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
415
467
  payload: { key: key, value: String(value) },
416
- type: actions_1.PAYMENT_ACTION_TYPES.SET_INPUT_FIELD,
417
468
  });
418
- dispatch({
469
+ context.dispatch({
470
+ type: exports.PAYMENT_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
419
471
  payload: { disableSaveButton: false },
420
- type: actions_1.PAYMENT_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
421
472
  });
422
- dispatch({
423
- type: actions_1.PAYMENT_ACTION_TYPES.RESET_ERRORS,
473
+ context.dispatch({
474
+ type: exports.PAYMENT_ACTION_TYPES.SET_ERRORS,
475
+ payload: { errors: {} },
424
476
  });
425
- }, [dispatch, state === null || state === void 0 ? void 0 : state.invoice]);
477
+ }, [context, state === null || state === void 0 ? void 0 : state.invoice]);
426
478
  // ---------------------------------------------------------------------------
427
- // PAGINATION HANDLERS
479
+ // Pagination Handlers
428
480
  // ---------------------------------------------------------------------------
429
481
  var handleNextClick = (0, react_1.useCallback)(function () {
430
- dispatch({
482
+ context.dispatch({
483
+ type: exports.PAYMENT_ACTION_TYPES.SET_CURRENT_PAGE,
431
484
  payload: { currentPage: state.currentPage + 1 },
432
- type: actions_1.PAYMENT_ACTION_TYPES.SET_CURRENT_PAGE,
433
485
  });
434
- }, [state.currentPage, dispatch]);
486
+ }, [context, state.currentPage]);
435
487
  var handlePreviousClick = (0, react_1.useCallback)(function () {
436
- dispatch({
488
+ context.dispatch({
489
+ type: exports.PAYMENT_ACTION_TYPES.SET_CURRENT_PAGE,
437
490
  payload: { currentPage: state.currentPage - 1 },
438
- type: actions_1.PAYMENT_ACTION_TYPES.SET_CURRENT_PAGE,
439
491
  });
440
- }, [state.currentPage, dispatch]);
492
+ }, [context, state.currentPage]);
441
493
  var handlePageLimit = (0, react_1.useCallback)(function (node, value) {
442
494
  var val = __assign({}, value);
443
- dispatch({
495
+ context.dispatch({
496
+ type: exports.PAYMENT_ACTION_TYPES.SET_PAGE_LIMIT,
444
497
  payload: { pageLimit: Number(val.option) },
445
- type: actions_1.PAYMENT_ACTION_TYPES.SET_PAGE_LIMIT,
446
498
  });
447
- }, [dispatch]);
499
+ }, [context]);
448
500
  // ---------------------------------------------------------------------------
449
- // SEARCH HANDLERS
501
+ // Search Handlers
450
502
  // ---------------------------------------------------------------------------
451
503
  var searchOnChange = (0, react_1.useCallback)(function (k, v) {
452
- dispatch({
504
+ context.dispatch({
505
+ type: exports.PAYMENT_ACTION_TYPES.SET_SEARCH_QUERY,
453
506
  payload: { searchQuery: v },
454
- type: actions_1.PAYMENT_ACTION_TYPES.SET_SEARCH_QUERY,
455
507
  });
456
- }, [dispatch]);
508
+ }, [context]);
457
509
  var clearSearch = (0, react_1.useCallback)(function () {
458
- dispatch({
510
+ context.dispatch({
511
+ type: exports.PAYMENT_ACTION_TYPES.SET_SEARCH_QUERY,
459
512
  payload: { searchQuery: "" },
460
- type: actions_1.PAYMENT_ACTION_TYPES.SET_SEARCH_QUERY,
461
513
  });
462
- }, [dispatch]);
514
+ }, [context]);
463
515
  // ---------------------------------------------------------------------------
464
- // TABLE ACTIONS
516
+ // Table Actions
465
517
  // ---------------------------------------------------------------------------
466
518
  var headerActions = (0, react_1.useMemo)(function () { return []; }, []);
467
519
  var rowActions = (0, react_1.useMemo)(function () { return [
@@ -473,31 +525,8 @@ var usePaymentState = function () {
473
525
  },
474
526
  ]; }, [handleView, t]);
475
527
  // ---------------------------------------------------------------------------
476
- // RETURN STATE
528
+ // Return State (flat, backward-compatible)
477
529
  // ---------------------------------------------------------------------------
478
- return __assign(__assign({}, state), { byIdError: byIdError, byIdLoading: byIdLoading, clearSearch: clearSearch, closeDrawer: closeDrawer, deleteError: deleteError, deleteLoading: deleteLoading, dispatch: dispatch, handleChange: handleChange, handleNextClick: handleNextClick, handlePageLimit: handlePageLimit, handlePreviousClick: handlePreviousClick, handleSubmit: handleSubmit, headerActions: headerActions, listError: listError, listFetchNow: listFetchNow, listLoading: listLoading, rowActions: rowActions, searchOnChange: searchOnChange, updateError: updateError, updateLoading: updateLoading });
479
- };
480
- // ============================================================================
481
- // CONTEXT SETUP
482
- // ============================================================================
483
- exports.PaymentStateContext = (0, react_1.createContext)(__assign(__assign({}, reducer_1.initialPaymentState), { byIdError: undefined, byIdLoading: false, clearSearch: function () { return void 0; }, closeDrawer: function () { return void 0; }, deleteError: undefined, deleteLoading: false, dispatch: function () { return void 0; }, handleChange: function () { return void 0; }, handleNextClick: function () { return void 0; }, handlePageLimit: function () { return void 0; }, handlePreviousClick: function () { return void 0; }, handleSubmit: function () { return void 0; }, headerActions: [], listError: undefined, listFetchNow: function () { return void 0; }, listLoading: false, rowActions: [], searchOnChange: function () { return void 0; }, updateError: undefined, updateLoading: false }));
484
- // ============================================================================
485
- // PROVIDER COMPONENT
486
- // ============================================================================
487
- var PaymentStateContextProvider = function (_a) {
488
- var children = _a.children;
489
- var state = usePaymentState();
490
- return (react_1.default.createElement(exports.PaymentStateContext.Provider, { value: state }, children));
491
- };
492
- exports.PaymentStateContextProvider = PaymentStateContextProvider;
493
- // ============================================================================
494
- // CUSTOM HOOK
495
- // ============================================================================
496
- var usePaymentStateContext = function () {
497
- var state = (0, react_1.useContext)(exports.PaymentStateContext);
498
- if (state === undefined) {
499
- throw new Error("usePaymentStateContext must be used within a PaymentContextProvider");
500
- }
501
- return state;
530
+ return __assign(__assign({}, state), { payments: state.items, byIdError: byIdError, byIdLoading: byIdLoading, clearSearch: clearSearch, closeDrawer: closeDrawer, deleteError: deleteError, deleteLoading: deleteLoading, dispatch: context.dispatch, handleChange: handleChange, handleNextClick: handleNextClick, handlePageLimit: handlePageLimit, handlePreviousClick: handlePreviousClick, handleSubmit: handleSubmit, headerActions: headerActions, listError: listError, listFetchNow: listFetchNow, listLoading: listLoading, rowActions: rowActions, searchOnChange: searchOnChange, updateError: updateError, updateLoading: updateLoading });
502
531
  };
503
- exports.usePaymentStateContext = usePaymentStateContext;
532
+ exports.usePaymentModule = usePaymentModule;