@os1-platform/dispatch-mobile 3.0.4 → 3.1.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.
Files changed (218) hide show
  1. package/android/build.gradle +38 -9
  2. package/android/gradle.properties +17 -2
  3. package/lib/commonjs/components/dispatch/DispatchExecNavigator.js.map +1 -1
  4. package/lib/commonjs/components/executiontasks/forms/widgets/DateTimeWidget.js.map +1 -1
  5. package/lib/commonjs/components/executiontasks/imageCapture/CaptureUtils.js +1 -1
  6. package/lib/commonjs/components/executiontasks/imageCapture/CaptureUtils.js.map +1 -1
  7. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOTPET.js +467 -287
  8. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOTPET.js.map +1 -1
  9. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOTPSchema.js +14 -1
  10. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOTPSchema.js.map +1 -1
  11. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOtpStyles.js +270 -0
  12. package/lib/commonjs/components/executiontasks/validateOTP/ValidateOtpStyles.js.map +1 -0
  13. package/lib/commonjs/components/executiontasks/validateOTP/components/ContactListView.js +99 -0
  14. package/lib/commonjs/components/executiontasks/validateOTP/components/ContactListView.js.map +1 -0
  15. package/lib/commonjs/components/executiontasks/validateOTP/components/OTPView.js +195 -0
  16. package/lib/commonjs/components/executiontasks/validateOTP/components/OTPView.js.map +1 -0
  17. package/lib/commonjs/components/executiontasks/validateOTP/components/VerificationComplete.js +106 -0
  18. package/lib/commonjs/components/executiontasks/validateOTP/components/VerificationComplete.js.map +1 -0
  19. package/lib/commonjs/components/executiontasks/validateOTP/constants.js +12 -5
  20. package/lib/commonjs/components/executiontasks/validateOTP/constants.js.map +1 -1
  21. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/ContactCard.js +68 -0
  22. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/ContactCard.js.map +1 -0
  23. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/OTPInput.js +12 -5
  24. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/OTPInput.js.map +1 -1
  25. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/ResendOTPButton.js +61 -0
  26. package/lib/commonjs/components/executiontasks/validateOTP/sub-components/ResendOTPButton.js.map +1 -0
  27. package/lib/commonjs/components/executiontasks/validateOTP/utils.js +235 -114
  28. package/lib/commonjs/components/executiontasks/validateOTP/utils.js.map +1 -1
  29. package/lib/commonjs/components/utils/Scale.js +22 -0
  30. package/lib/commonjs/components/utils/Scale.js.map +1 -0
  31. package/lib/commonjs/components/widgets/ConfirmCancelModal.js +47 -0
  32. package/lib/commonjs/components/widgets/ConfirmCancelModal.js.map +1 -0
  33. package/lib/commonjs/components/widgets/SnackBarCustom.js +36 -4
  34. package/lib/commonjs/components/widgets/SnackBarCustom.js.map +1 -1
  35. package/lib/commonjs/components/widgets/SplitTextInput.js +48 -18
  36. package/lib/commonjs/components/widgets/SplitTextInput.js.map +1 -1
  37. package/lib/commonjs/constants/apiConstants.js +4 -1
  38. package/lib/commonjs/constants/apiConstants.js.map +1 -1
  39. package/lib/commonjs/constants/constants.js +2 -1
  40. package/lib/commonjs/constants/constants.js.map +1 -1
  41. package/lib/commonjs/icons/icExclamationcircleo_dark.svg +3 -0
  42. package/lib/commonjs/icons/icFailed.gif +0 -0
  43. package/lib/commonjs/icons/icRadioChecked.svg +4 -0
  44. package/lib/commonjs/icons/icRadioUnchecked.svg +4 -0
  45. package/lib/commonjs/icons/icSuccess.gif +0 -0
  46. package/lib/commonjs/icons/index.js +28 -0
  47. package/lib/commonjs/icons/index.js.map +1 -0
  48. package/lib/commonjs/index.js +19 -0
  49. package/lib/commonjs/index.js.map +1 -1
  50. package/lib/commonjs/manager/sdk/DispatchSDKManager.js +5 -4
  51. package/lib/commonjs/manager/sdk/DispatchSDKManager.js.map +1 -1
  52. package/lib/commonjs/manager/sdk/callbacksHandler.js +9 -2
  53. package/lib/commonjs/manager/sdk/callbacksHandler.js.map +1 -1
  54. package/lib/commonjs/manager/syncmanager/constants.js +3 -1
  55. package/lib/commonjs/manager/syncmanager/constants.js.map +1 -1
  56. package/lib/commonjs/manager/syncmanager/document/DocumentHttpClient.js +3 -2
  57. package/lib/commonjs/manager/syncmanager/document/DocumentHttpClient.js.map +1 -1
  58. package/lib/commonjs/manager/syncmanager/document/DocumentSyncManager.js +70 -80
  59. package/lib/commonjs/manager/syncmanager/document/DocumentSyncManager.js.map +1 -1
  60. package/lib/commonjs/models/sdk.js +1 -0
  61. package/lib/commonjs/models/sdk.js.map +1 -1
  62. package/lib/commonjs/ui/screens/ValidateOTPScreen.js +132 -12
  63. package/lib/commonjs/ui/screens/ValidateOTPScreen.js.map +1 -1
  64. package/lib/commonjs/utils/ApiUtils.js +122 -3
  65. package/lib/commonjs/utils/ApiUtils.js.map +1 -1
  66. package/lib/commonjs/utils/ExecTaskUtils.js +67 -7
  67. package/lib/commonjs/utils/ExecTaskUtils.js.map +1 -1
  68. package/lib/commonjs/utils/context/valdiateOTPContext.js +11 -0
  69. package/lib/commonjs/utils/context/valdiateOTPContext.js.map +1 -0
  70. package/lib/commonjs/utils/helper.js.map +1 -1
  71. package/lib/commonjs/utils/hooks/useTimer.js +68 -30
  72. package/lib/commonjs/utils/hooks/useTimer.js.map +1 -1
  73. package/lib/module/components/dispatch/DispatchExecNavigator.js.map +1 -1
  74. package/lib/module/components/executiontasks/forms/widgets/DateTimeWidget.js.map +1 -1
  75. package/lib/module/components/executiontasks/imageCapture/CaptureUtils.js +1 -1
  76. package/lib/module/components/executiontasks/imageCapture/CaptureUtils.js.map +1 -1
  77. package/lib/module/components/executiontasks/validateOTP/ValidateOTPET.js +480 -290
  78. package/lib/module/components/executiontasks/validateOTP/ValidateOTPET.js.map +1 -1
  79. package/lib/module/components/executiontasks/validateOTP/ValidateOTPSchema.js +14 -0
  80. package/lib/module/components/executiontasks/validateOTP/ValidateOTPSchema.js.map +1 -1
  81. package/lib/module/components/executiontasks/validateOTP/ValidateOtpStyles.js +264 -0
  82. package/lib/module/components/executiontasks/validateOTP/ValidateOtpStyles.js.map +1 -0
  83. package/lib/module/components/executiontasks/validateOTP/components/ContactListView.js +88 -0
  84. package/lib/module/components/executiontasks/validateOTP/components/ContactListView.js.map +1 -0
  85. package/lib/module/components/executiontasks/validateOTP/components/OTPView.js +194 -0
  86. package/lib/module/components/executiontasks/validateOTP/components/OTPView.js.map +1 -0
  87. package/lib/module/components/executiontasks/validateOTP/components/VerificationComplete.js +96 -0
  88. package/lib/module/components/executiontasks/validateOTP/components/VerificationComplete.js.map +1 -0
  89. package/lib/module/components/executiontasks/validateOTP/constants.js +11 -4
  90. package/lib/module/components/executiontasks/validateOTP/constants.js.map +1 -1
  91. package/lib/module/components/executiontasks/validateOTP/sub-components/ContactCard.js +59 -0
  92. package/lib/module/components/executiontasks/validateOTP/sub-components/ContactCard.js.map +1 -0
  93. package/lib/module/components/executiontasks/validateOTP/sub-components/OTPInput.js +11 -5
  94. package/lib/module/components/executiontasks/validateOTP/sub-components/OTPInput.js.map +1 -1
  95. package/lib/module/components/executiontasks/validateOTP/sub-components/ResendOTPButton.js +52 -0
  96. package/lib/module/components/executiontasks/validateOTP/sub-components/ResendOTPButton.js.map +1 -0
  97. package/lib/module/components/executiontasks/validateOTP/utils.js +219 -108
  98. package/lib/module/components/executiontasks/validateOTP/utils.js.map +1 -1
  99. package/lib/module/components/utils/Scale.js +14 -0
  100. package/lib/module/components/utils/Scale.js.map +1 -0
  101. package/lib/module/components/widgets/ConfirmCancelModal.js +40 -0
  102. package/lib/module/components/widgets/ConfirmCancelModal.js.map +1 -0
  103. package/lib/module/components/widgets/SnackBarCustom.js +36 -4
  104. package/lib/module/components/widgets/SnackBarCustom.js.map +1 -1
  105. package/lib/module/components/widgets/SplitTextInput.js +48 -19
  106. package/lib/module/components/widgets/SplitTextInput.js.map +1 -1
  107. package/lib/module/constants/apiConstants.js +4 -1
  108. package/lib/module/constants/apiConstants.js.map +1 -1
  109. package/lib/module/constants/constants.js +1 -0
  110. package/lib/module/constants/constants.js.map +1 -1
  111. package/lib/module/icons/icExclamationcircleo_dark.svg +3 -0
  112. package/lib/module/icons/icFailed.gif +0 -0
  113. package/lib/module/icons/icRadioChecked.svg +4 -0
  114. package/lib/module/icons/icRadioUnchecked.svg +4 -0
  115. package/lib/module/icons/icSuccess.gif +0 -0
  116. package/lib/module/icons/index.js +5 -0
  117. package/lib/module/icons/index.js.map +1 -0
  118. package/lib/module/index.js +2 -0
  119. package/lib/module/index.js.map +1 -1
  120. package/lib/module/manager/sdk/DispatchSDKManager.js +5 -3
  121. package/lib/module/manager/sdk/DispatchSDKManager.js.map +1 -1
  122. package/lib/module/manager/sdk/callbacksHandler.js +12 -3
  123. package/lib/module/manager/sdk/callbacksHandler.js.map +1 -1
  124. package/lib/module/manager/syncmanager/constants.js +2 -0
  125. package/lib/module/manager/syncmanager/constants.js.map +1 -1
  126. package/lib/module/manager/syncmanager/document/DocumentHttpClient.js +3 -2
  127. package/lib/module/manager/syncmanager/document/DocumentHttpClient.js.map +1 -1
  128. package/lib/module/manager/syncmanager/document/DocumentSyncManager.js +70 -80
  129. package/lib/module/manager/syncmanager/document/DocumentSyncManager.js.map +1 -1
  130. package/lib/module/models/sdk.js +2 -0
  131. package/lib/module/models/sdk.js.map +1 -1
  132. package/lib/module/ui/screens/ValidateOTPScreen.js +139 -11
  133. package/lib/module/ui/screens/ValidateOTPScreen.js.map +1 -1
  134. package/lib/module/utils/ApiUtils.js +118 -2
  135. package/lib/module/utils/ApiUtils.js.map +1 -1
  136. package/lib/module/utils/ExecTaskUtils.js +66 -7
  137. package/lib/module/utils/ExecTaskUtils.js.map +1 -1
  138. package/lib/module/utils/context/valdiateOTPContext.js +4 -0
  139. package/lib/module/utils/context/valdiateOTPContext.js.map +1 -0
  140. package/lib/module/utils/helper.js +1 -0
  141. package/lib/module/utils/helper.js.map +1 -1
  142. package/lib/module/utils/hooks/useTimer.js +67 -30
  143. package/lib/module/utils/hooks/useTimer.js.map +1 -1
  144. package/lib/typescript/components/executiontasks/collectPayment/PaymentStyles.d.ts +3 -3
  145. package/lib/typescript/components/executiontasks/deliver/DeliverETInput.d.ts +0 -1
  146. package/lib/typescript/components/executiontasks/scan/modalConstant.d.ts +0 -1
  147. package/lib/typescript/components/executiontasks/validateOTP/ValidateOTPET.d.ts +3 -3
  148. package/lib/typescript/components/executiontasks/validateOTP/ValidateOTPSchema.d.ts +169 -5
  149. package/lib/typescript/components/executiontasks/validateOTP/ValidateOtpStyles.d.ts +255 -0
  150. package/lib/typescript/components/executiontasks/validateOTP/components/ContactListView.d.ts +3 -0
  151. package/lib/typescript/components/executiontasks/validateOTP/components/OTPView.d.ts +3 -0
  152. package/lib/typescript/components/executiontasks/validateOTP/components/VerificationComplete.d.ts +3 -0
  153. package/lib/typescript/components/executiontasks/validateOTP/constants.d.ts +11 -4
  154. package/lib/typescript/components/executiontasks/validateOTP/sub-components/ContactCard.d.ts +8 -0
  155. package/lib/typescript/components/executiontasks/validateOTP/sub-components/ResendOTPButton.d.ts +12 -0
  156. package/lib/typescript/components/executiontasks/validateOTP/utils.d.ts +31 -7
  157. package/lib/typescript/components/utils/Scale.d.ts +4 -0
  158. package/lib/typescript/components/widgets/ConfirmCancelModal.d.ts +9 -0
  159. package/lib/typescript/components/widgets/SnackBarCustom.d.ts +4 -8
  160. package/lib/typescript/components/widgets/SplitTextInput.d.ts +1 -1
  161. package/lib/typescript/components/widgets/Timer.d.ts +3 -3
  162. package/lib/typescript/constants/apiConstants.d.ts +3 -0
  163. package/lib/typescript/constants/constants.d.ts +1 -0
  164. package/lib/typescript/icons/index.d.ts +4 -0
  165. package/lib/typescript/index.d.ts +2 -0
  166. package/lib/typescript/manager/sdk/callbacksHandler.d.ts +4 -1
  167. package/lib/typescript/manager/syncmanager/constants.d.ts +2 -0
  168. package/lib/typescript/models/execTasks/ETConfig.d.ts +1 -1
  169. package/lib/typescript/models/sdk.d.ts +3 -0
  170. package/lib/typescript/network/GraphQLClient.d.ts +4 -4
  171. package/lib/typescript/styles/executionTasks/CommonStyles.d.ts +2 -2
  172. package/lib/typescript/ui/screens/HttpRequestScreen.d.ts +0 -1
  173. package/lib/typescript/utils/ApiUtils.d.ts +6 -2
  174. package/lib/typescript/utils/ExecTaskUtils.d.ts +1 -0
  175. package/lib/typescript/utils/context/paymentContext.d.ts +0 -1
  176. package/lib/typescript/utils/context/valdiateOTPContext.d.ts +6 -0
  177. package/lib/typescript/utils/hooks/useModal.d.ts +0 -2
  178. package/lib/typescript/utils/hooks/useTimer.d.ts +7 -5
  179. package/package.json +46 -44
  180. package/src/components/dispatch/DispatchExecNavigator.tsx +1 -1
  181. package/src/components/executiontasks/forms/widgets/DateTimeWidget.tsx +2 -2
  182. package/src/components/executiontasks/imageCapture/CaptureUtils.ts +1 -1
  183. package/src/components/executiontasks/validateOTP/ValidateOTPET.tsx +654 -381
  184. package/src/components/executiontasks/validateOTP/ValidateOTPSchema.ts +193 -8
  185. package/src/components/executiontasks/validateOTP/ValidateOtpStyles.ts +296 -0
  186. package/src/components/executiontasks/validateOTP/components/ContactListView.tsx +109 -0
  187. package/src/components/executiontasks/validateOTP/components/OTPView.tsx +216 -0
  188. package/src/components/executiontasks/validateOTP/components/VerificationComplete.tsx +124 -0
  189. package/src/components/executiontasks/validateOTP/constants.ts +11 -4
  190. package/src/components/executiontasks/validateOTP/sub-components/ContactCard.tsx +72 -0
  191. package/src/components/executiontasks/validateOTP/sub-components/OTPInput.tsx +13 -7
  192. package/src/components/executiontasks/validateOTP/sub-components/ResendOTPButton.tsx +86 -0
  193. package/src/components/executiontasks/validateOTP/utils.ts +313 -143
  194. package/src/components/utils/Scale.ts +13 -0
  195. package/src/components/widgets/ConfirmCancelModal.tsx +49 -0
  196. package/src/components/widgets/SnackBarCustom.tsx +37 -13
  197. package/src/components/widgets/SplitTextInput.tsx +58 -21
  198. package/src/constants/apiConstants.ts +3 -0
  199. package/src/constants/constants.ts +2 -2
  200. package/src/icons/icExclamationcircleo_dark.svg +3 -0
  201. package/src/icons/icFailed.gif +0 -0
  202. package/src/icons/icRadioChecked.svg +4 -0
  203. package/src/icons/icRadioUnchecked.svg +4 -0
  204. package/src/icons/icSuccess.gif +0 -0
  205. package/src/icons/index.ts +5 -0
  206. package/src/index.tsx +2 -0
  207. package/src/manager/sdk/DispatchSDKManager.ts +6 -5
  208. package/src/manager/sdk/callbacksHandler.ts +29 -12
  209. package/src/manager/syncmanager/constants.ts +2 -0
  210. package/src/manager/syncmanager/document/DocumentHttpClient.ts +29 -29
  211. package/src/manager/syncmanager/document/DocumentSyncManager.ts +75 -84
  212. package/src/models/sdk.ts +6 -0
  213. package/src/ui/screens/ValidateOTPScreen.tsx +194 -32
  214. package/src/utils/ApiUtils.ts +131 -3
  215. package/src/utils/ExecTaskUtils.ts +77 -15
  216. package/src/utils/context/valdiateOTPContext.ts +9 -0
  217. package/src/utils/helper.ts +0 -1
  218. package/src/utils/hooks/useTimer.ts +94 -36
@@ -32,7 +32,7 @@ export async function getBaseETResponse(
32
32
  const startLocation = await cache.getObjectFromCache(
33
33
  DispatchSdkCache.KEYS.TASK_START_LOCATION
34
34
  );
35
- console.log("getBaseETResponse:startLocation:",startLocation);
35
+ console.log('getBaseETResponse:startLocation:', startLocation);
36
36
  let baseResponse: BaseETResponse = {
37
37
  entityCode: entityCode,
38
38
  success: success,
@@ -40,13 +40,13 @@ export async function getBaseETResponse(
40
40
  reasonCode: reasonCode,
41
41
  id: taskId,
42
42
  meta: {
43
- startLocation:{
44
- latitude:startLocation.latitude,
45
- longitude:startLocation.longitude
43
+ startLocation: {
44
+ latitude: startLocation.latitude,
45
+ longitude: startLocation.longitude,
46
46
  },
47
- endLocation:{
48
- latitude:lastLocation.latitude,
49
- longitude:lastLocation.longitude
47
+ endLocation: {
48
+ latitude: lastLocation.latitude,
49
+ longitude: lastLocation.longitude,
50
50
  },
51
51
  // location: {
52
52
  // latitude: location.latitude,
@@ -61,7 +61,7 @@ export async function getBaseETResponse(
61
61
  },
62
62
  mergedObjectiveIds,
63
63
  };
64
- console.log("getBaseETResponse:baseResponse:",JSON.stringify(baseResponse));
64
+ console.log('getBaseETResponse:baseResponse:', JSON.stringify(baseResponse));
65
65
 
66
66
  return baseResponse;
67
67
  }
@@ -155,8 +155,6 @@ export function onTaskStart(
155
155
  await cache.setKeyInCache(keys.TASK_START_TIME, Date.now().toString());
156
156
  await cache.setKeyInCache(keys.CURRENT_WORKING_TASK_ID, eventData.taskId);
157
157
  await cache.setObjectInCache(keys.TASK_START_LOCATION, location);
158
-
159
-
160
158
  })();
161
159
  if (triggerEvent) {
162
160
  const customEtName = eventData.taskName;
@@ -269,13 +267,77 @@ export function handleDynamicValues(jsonInput: any, dataBlob: any) {
269
267
  return jsonInput;
270
268
  }
271
269
 
270
+ export function parseRequestBody(body: any, userInput: any) {
271
+ const requestBody = JSON.parse(JSON.stringify(body)); // Deep clone the input
272
+
273
+ // Preprocess `userInput` placeholders before calling handleDynamicValues
274
+ const preprocessUserInput = (jsonInput: any) => {
275
+ for (const key in jsonInput) {
276
+ if (Object.prototype.hasOwnProperty.call(jsonInput, key)) {
277
+ const value = jsonInput[key];
278
+
279
+ if (typeof value === 'string' && value.includes('{{$.userInput}}')) {
280
+ // Replace `{{$.userInput}}` with the OTP value from userInput
281
+ jsonInput[key] = userInput.otp || value;
282
+ } else if (typeof value === 'object' && value !== null) {
283
+ // Recursively preprocess nested objects
284
+ preprocessUserInput(value);
285
+ } else if (Array.isArray(value)) {
286
+ // Recursively preprocess arrays
287
+ jsonInput[key] = value.map((item) => {
288
+ if (typeof item === 'string' && item.includes('{{$.userInput}}')) {
289
+ return userInput.otp || userInput.input || item;
290
+ } else if (
291
+ typeof item === 'string' &&
292
+ item.includes('{{$.inputs.userInput}}')
293
+ ) {
294
+ return userInput.input || item;
295
+ } else if (typeof item === 'object' && item !== null) {
296
+ preprocessUserInput(item);
297
+ return item;
298
+ }
299
+ return item;
300
+ });
301
+ }
302
+ }
303
+ }
304
+ return jsonInput;
305
+ };
306
+
307
+ // Step 1: Preprocess the body for `{{$.userInput}}`
308
+ const preprocessedBody = preprocessUserInput(requestBody);
309
+
310
+ // Step 2: Pass the preprocessed body to `handleDynamicValues`
311
+ const resolvedBody = handleDynamicValues(preprocessedBody, userInput);
312
+
313
+ return resolvedBody;
314
+ }
315
+
316
+ // old masking
317
+ // export const maskContact = (contact: string) => {
318
+ // if (contact && contact?.includes('@')) {
319
+ // const [name, domain] = contact.split('@');
320
+ // const maskedName = name.substring(0, 3) + '****';
321
+ // const maskedEmail = maskedName + '@' + domain;
322
+ // return maskedEmail;
323
+ // } else {
324
+ // return `${contact && contact.slice(0, 2)}****${contact.slice(-4)}`;
325
+ // }
326
+ // };
272
327
  export const maskContact = (contact: string) => {
273
- if (contact.includes('@')) {
328
+ if (contact && contact.includes('@')) {
274
329
  const [name, domain] = contact.split('@');
275
- const maskedName = name.substring(0, 3) + '****';
276
- const maskedEmail = maskedName + '@' + domain;
277
- return maskedEmail;
330
+ let maskedName;
331
+ if (name.length <= 2) {
332
+ maskedName = name.substring(0, 1) + '*****';
333
+ } else {
334
+ maskedName = name.substring(0, 2) + '*****';
335
+ }
336
+ return maskedName + '@' + domain;
278
337
  } else {
279
- return `${contact.slice(0, 2)}****${contact.slice(-4)}`;
338
+ const visibleDigits = 4;
339
+ const maskedPart = '*'.repeat(Math.max(contact.length - visibleDigits, 0));
340
+ const visiblePart = contact.slice(-visibleDigits);
341
+ return maskedPart + visiblePart;
280
342
  }
281
343
  };
@@ -0,0 +1,9 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import { createContext } from 'react';
3
+ import { DispatchSDKConfig } from '../../models/sdk';
4
+ import NetworkClient from '../../network/NetworkClient';
5
+
6
+ export const ValidateOTPContext = createContext<{
7
+ sdkConfig: DispatchSDKConfig;
8
+ networkInstance: NetworkClient;
9
+ } | null>(null);
@@ -1,6 +1,5 @@
1
1
  import Network from '../resources/network';
2
2
  import { DispatchExecutor as dispatchExecutor } from '@os1-platform/platform-coreos-execution-engine-sdk';
3
- import ETTransition from '@os1-platform/platform-coreos-execution-engine-sdk/';
4
3
 
5
4
  /**
6
5
  *
@@ -1,44 +1,102 @@
1
- import { useEffect, useState } from 'react';
2
-
3
- const useTimer = (props: {
4
- initialMinute: number;
5
- initialSeconds: number;
6
- }): [string | null, () => void] => {
7
- const { initialMinute = 0, initialSeconds = 0 } = props;
8
- const [minutes, setMinutes] = useState(initialMinute);
9
- const [seconds, setSeconds] = useState(initialSeconds);
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import { useCallback, useEffect, useRef, useState } from 'react';
3
+ import { AppState, AppStateStatus } from 'react-native';
4
+
5
+ // Function to format the expiry time (without showing '0 sec' when it's unnecessary)
6
+ export const formatExpiryTime = (timeInSeconds: number) => {
7
+ const minutes = Math.floor(timeInSeconds / 60);
8
+ const seconds = timeInSeconds % 60;
9
+
10
+ if (minutes > 0 && seconds > 0) {
11
+ return `${minutes} min ${seconds} sec`;
12
+ } else if (minutes > 0 && seconds === 0) {
13
+ return `${minutes} min`;
14
+ } else {
15
+ return `${seconds} sec`;
16
+ }
17
+ };
18
+
19
+ const useResendOtpTimer = (
20
+ initialTimerValue: number,
21
+ onTimerEnd?: () => void
22
+ ) => {
23
+ const [timer, setTimer] = useState(initialTimerValue);
24
+ const appState = useRef(AppState.currentState);
25
+ const initialTimeRef = useRef(Date.now());
26
+
27
+ const resetTimer = useCallback(() => {
28
+ initialTimeRef.current = Date.now();
29
+ setTimer(initialTimerValue);
30
+ }, [initialTimerValue]);
31
+
32
+ const updateTimer = useCallback(() => {
33
+ const currentTime = Date.now();
34
+ const elapsedTimeInSeconds = Math.floor(
35
+ (currentTime - initialTimeRef.current) / 1000
36
+ );
37
+ const remainingTime = Math.max(0, initialTimerValue - elapsedTimeInSeconds);
38
+
39
+ if (remainingTime !== timer) {
40
+ setTimer(remainingTime);
41
+ }
42
+
43
+ if (remainingTime === 0 && onTimerEnd) {
44
+ onTimerEnd();
45
+ }
46
+ }, [initialTimerValue, onTimerEnd, timer]);
47
+
10
48
  useEffect(() => {
11
- let myInterval = setInterval(() => {
12
- if (seconds > 0) {
13
- setSeconds(seconds - 1);
14
- }
15
- if (seconds === 0) {
16
- if (minutes === 0) {
17
- clearInterval(myInterval);
18
- } else {
19
- setMinutes(minutes - 1);
20
- setSeconds(59);
21
- }
49
+ resetTimer();
50
+ }, [initialTimerValue, resetTimer]);
51
+
52
+ useEffect(() => {
53
+ // Update the timer immediately on mount
54
+ updateTimer();
55
+ const intervalId = setInterval(updateTimer, 1000);
56
+ return () => clearInterval(intervalId);
57
+ }, [updateTimer]);
58
+
59
+ useEffect(() => {
60
+ const handleAppStateChange = (nextAppState: AppStateStatus) => {
61
+ if (
62
+ appState.current.match(/inactive|background/) &&
63
+ nextAppState === 'active'
64
+ ) {
65
+ // Calculate elapsed time and adjust the initial time reference
66
+ const currentTime = Date.now();
67
+ const elapsedTimeInSeconds = Math.floor(
68
+ (currentTime - initialTimeRef.current) / 1000
69
+ );
70
+ const newRemainingTime = Math.max(
71
+ 0,
72
+ initialTimerValue - elapsedTimeInSeconds
73
+ );
74
+
75
+ // Update initial time ref based on new remaining time
76
+ initialTimeRef.current =
77
+ currentTime - (initialTimerValue - newRemainingTime) * 1000;
78
+
79
+ // Update the timer
80
+ setTimer(newRemainingTime);
81
+ updateTimer();
22
82
  }
23
- }, 1000);
83
+ appState.current = nextAppState;
84
+ };
85
+
86
+ const subscription = AppState.addEventListener(
87
+ 'change',
88
+ handleAppStateChange
89
+ );
24
90
  return () => {
25
- clearInterval(myInterval);
91
+ subscription.remove();
26
92
  };
27
- });
93
+ }, [updateTimer, initialTimerValue]);
28
94
 
29
- const resetTimer = () => {
30
- if (!minutes && !seconds) {
31
- setMinutes(initialMinute);
32
- setSeconds(initialSeconds);
33
- }
95
+ return {
96
+ formattedTime: formatExpiryTime(timer),
97
+ timer: timer,
98
+ resetTimer,
34
99
  };
35
-
36
- const timer =
37
- minutes === 0 && seconds === 0
38
- ? null
39
- : `${minutes}: ${seconds < 10 ? `0${seconds}` : seconds}`;
40
-
41
- return [timer, resetTimer];
42
100
  };
43
101
 
44
- export default useTimer;
102
+ export default useResendOtpTimer;