@global.engg/switcher-lib 0.1.7 → 0.1.30

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.
@@ -144,15 +144,26 @@ class AppSwitcherService {
144
144
  });
145
145
  }
146
146
  appSwitch(app, kyc_status) {
147
- var _a, _b, _c;
147
+ var _a, _b;
148
148
  this.sendGaEvent(app);
149
+ // moengage event
150
+ (_b = (_a = this.windowRef.nativeWindow) === null || _a === void 0 ? void 0 : _a.Moengage) === null || _b === void 0 ? void 0 : _b.track_event("product_navigation_icon_clicked", { icon_name: app, company_id: this.userData.company_id });
151
+ // Store window reference BEFORE delay to prevent popup blocking
152
+ const windowRef = this.windowRef.nativeWindow;
153
+ // Add delay after MoEngage tracking
154
+ // setTimeout(() => {
155
+ // }, 500); // 500ms delay
156
+ this.executeAppSwitch(app, kyc_status, windowRef);
157
+ }
158
+ executeAppSwitch(app, kyc_status, windowRef) {
159
+ var _a, _b, _c;
149
160
  if (this.userData.allow_engage && app === 'engage') {
150
- window.open(`${this.SR_CONSTANTS.EngageWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
161
+ windowRef.open(`${this.SR_CONSTANTS.EngageWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
151
162
  }
152
163
  else if (app === 'checkout') {
153
164
  let url = `https://fastrr-dashboard.pickrr.com/dashboard?utm_source=sr_dashboard&utm_medium=appswitcher&utm_campaign=appswitcher01&utm_content=appswitcher01&cid=${this.userData.company_id}`;
154
165
  url = `${url}&token=${this.userData.token}`;
155
- window.open(url, '_blank');
166
+ windowRef.open(url, '_blank');
156
167
  // if ((this.isCheckoutAllowed(this.userData?.company_id) || this.userData.allow_checkout)) {
157
168
  // url = `${url}&token=${this.userData.token}`
158
169
  // window.open(url, '_blank');
@@ -161,26 +172,31 @@ class AppSwitcherService {
161
172
  // }
162
173
  }
163
174
  else if (this.userData.allow_fbs && app === 'fullfilment') {
164
- window.open(`${this.SR_CONSTANTS.SrfWebBaseUrl}login?token=${this.userData.token}`, '_blank');
175
+ windowRef.open(`${this.SR_CONSTANTS.SrfWebBaseUrl}login?token=${this.userData.token}`, '_blank');
165
176
  }
166
177
  else if (!this.userData.allow_fbs && app === 'fullfilment') {
167
- window.open(`${this.SR_CONSTANTS.FulfillmentWebBaseUrl}?utm_source=panel&utm_medium=app_switcher&utm_campaign=SR-Panel-non-srf-switcher&token=${this.userData.token}`, '_blank');
178
+ windowRef.open(`${this.SR_CONSTANTS.FulfillmentWebBaseUrl}?utm_source=panel&utm_medium=app_switcher&utm_campaign=SR-Panel-non-srf-switcher&token=${this.userData.token}`, '_blank');
168
179
  }
169
180
  else if (app === 'rocketbox') {
170
181
  if (this.userData.allow_rb_panel || this.userData.is_rb_user) {
171
- window.location.href = `${this.DOMAIN}/rocketbox`;
182
+ // Use setTimeout for window.location.href redirects
183
+ setTimeout(() => {
184
+ window.location.href = `${this.DOMAIN}/rocketbox`;
185
+ }, 500);
172
186
  }
173
187
  else {
174
- window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
188
+ setTimeout(() => {
189
+ window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
190
+ }, 500);
175
191
  }
176
192
  }
177
193
  else if (((_a = this.userData) === null || _a === void 0 ? void 0 : _a.allow_sr_convert) &&
178
194
  !((_b = this.userData) === null || _b === void 0 ? void 0 : _b.allow_engage) &&
179
195
  app === 'sr_convert') {
180
- window.open(`${this.SR_CONSTANTS.WigzoWebBaseUrl}/${this.userData.token}`, '_blank');
196
+ windowRef.open(`${this.SR_CONSTANTS.WigzoWebBaseUrl}/${this.userData.token}`, '_blank');
181
197
  }
182
198
  else if (app === 'sr_genie') {
183
- window.open(`https://ai-test.kartrocket.com/login?token=${this.userData.token}`, '_blank');
199
+ windowRef.open(`https://ai-test.kartrocket.com/login?token=${this.userData.token}`, '_blank');
184
200
  }
185
201
  else if (app === 'one_impression') {
186
202
  // Calling one-impression and redirecting to new tab
@@ -190,10 +206,10 @@ class AppSwitcherService {
190
206
  this.fetchOpenInAppLink();
191
207
  }
192
208
  else if (app === 'wigzo') {
193
- window.open(`https://app.wigzo.com/auth/shiprocket/login/${this.userData.token}`, '_blank');
209
+ windowRef.open(`https://app.wigzo.com/auth/shiprocket/login/${this.userData.token}`, '_blank');
194
210
  }
195
211
  else if (app === 'omuni') {
196
- window.open(`https://omuni.com/`, '_blank');
212
+ windowRef.open(`https://omuni.com/`, '_blank');
197
213
  // if (this.userData?.is_omuni2_user == 1) {
198
214
  // this.httpService.get('omuni/switch').subscribe(
199
215
  // (res) => {
@@ -212,59 +228,82 @@ class AppSwitcherService {
212
228
  // }
213
229
  }
214
230
  else if (app === 'trends') {
215
- window.open(`https://trends.shiprocket.in/`, '_blank');
231
+ windowRef.open(`https://trends.shiprocket.in/`, '_blank');
216
232
  }
217
233
  else if (app === 'creditScore') {
218
234
  if ((_c = this.userData) === null || _c === void 0 ? void 0 : _c.is_crif_enabled) {
219
- window.open(`https://capital-web.shiprocket.in/crif-home/?utm_source=App-Switcher&utm_medium=Manual-Adhoc&utm_content=crif_credit_score&utm_term=app_switcher_click&authToken=` + this.userData.token, '_blank');
235
+ windowRef.open(`https://capital-web.shiprocket.in/crif-home/?utm_source=App-Switcher&utm_medium=Manual-Adhoc&utm_content=crif_credit_score&utm_term=app_switcher_click&authToken=` + this.userData.token, '_blank');
220
236
  }
221
237
  else {
222
- window.open(`https://www.shiprocket.in/capital/`, '_blank');
238
+ windowRef.open(`https://www.shiprocket.in/capital/`, '_blank');
223
239
  }
224
240
  }
225
241
  else if (app === 'capital') {
226
- window.open(`https://capital-web.shiprocket.in/home/?utm_source=App-Switcher&utm_medium=Manual-Adhoc&utm_content=capital&utm_term=flexi_business_loan&authToken=` + this.userData.token, '_blank');
242
+ windowRef.open(`https://capital-web.shiprocket.in/home/?utm_source=App-Switcher&utm_medium=Manual-Adhoc&utm_content=capital&utm_term=flexi_business_loan&authToken=` + this.userData.token, '_blank');
227
243
  }
228
244
  else if (app === 'marketing') {
229
- window.location.href = `${this.DOMAIN}/seller/home`;
245
+ // Use setTimeout for window.location.href redirects
246
+ setTimeout(() => {
247
+ window.location.href = `${this.DOMAIN}/seller/home`;
248
+ }, 500);
230
249
  }
231
250
  else if (app === 'whatsapp_solutions') {
232
251
  if (this.userData.allow_engage) {
233
- window.open(`${this.SR_CONSTANTS.EngageWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
252
+ windowRef.open(`${this.SR_CONSTANTS.EngageWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
234
253
  }
235
254
  else {
236
- window.open(`https://app-engage.shiprocket.in/auth/register`, '_blank');
255
+ windowRef.open(`https://app-engage.shiprocket.in/auth/register`, '_blank');
237
256
  }
238
257
  }
239
258
  else if (app === 'internationalParcels') {
240
259
  if (kyc_status == 0 || kyc_status == 1 || kyc_status == 2 || kyc_status == 4) {
241
- window.location.href = `${this.DOMAIN}/seller/internationalDashboard/overview`;
260
+ // Use setTimeout for window.location.href redirects
261
+ setTimeout(() => {
262
+ window.location.href = `${this.DOMAIN}/seller/internationalDashboard/overview`;
263
+ }, 500);
242
264
  }
243
265
  else {
244
- window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
266
+ setTimeout(() => {
267
+ window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
268
+ }, 500);
245
269
  }
246
270
  }
247
271
  else if (app === 'i_rocketbox') {
248
- window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
272
+ // Use setTimeout for window.location.href redirects
273
+ setTimeout(() => {
274
+ window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
275
+ }, 500);
249
276
  }
250
277
  else if (app === 'brand_launch') {
251
- window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
278
+ // Use setTimeout for window.location.href redirects
279
+ setTimeout(() => {
280
+ window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
281
+ }, 500);
252
282
  }
253
283
  else if (app === 'express_parcel') {
254
- window.location.href = `${this.DOMAIN}/seller/home`;
284
+ // Use setTimeout for window.location.href redirects
285
+ setTimeout(() => {
286
+ window.location.href = `${this.DOMAIN}/seller/home`;
287
+ }, 500);
255
288
  }
256
289
  else if (app === 'instant_deliveries') {
257
- window.open(`${this.SR_CONSTANTS.QuickWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
290
+ windowRef.open(`${this.SR_CONSTANTS.QuickWebBaseUrl}gate-keeper?token=${this.userData.token}`, '_blank');
258
291
  return;
259
292
  }
260
293
  else if (app === 'warehousing') {
261
- window.open(`https://fulfillment.shiprocket.in/`, '_blank');
294
+ windowRef.open(`https://fulfillment.shiprocket.in/`, '_blank');
262
295
  }
263
296
  else if (app == 'srocketbox') {
264
- window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
297
+ // Use setTimeout for window.location.href redirects
298
+ setTimeout(() => {
299
+ window.location.href = `${this.DOMAIN}/seller/settings/internationalsettings/internationalkyc`;
300
+ }, 500);
265
301
  }
266
302
  else if (app == 'somuni') {
267
- window.location.href = `${this.DOMAIN}/seller/home`;
303
+ // Use setTimeout for window.location.href redirects
304
+ setTimeout(() => {
305
+ window.location.href = `${this.DOMAIN}/seller/home`;
306
+ }, 500);
268
307
  }
269
308
  }
270
309
  sendGaEvent(event) {
@@ -359,6 +398,8 @@ class AppSwitcherService {
359
398
  }
360
399
  this.send(category, eventName, label);
361
400
  }
401
+ moengageEvent(event) {
402
+ }
362
403
  send(eventCategory, eventAction, eventLabel) {
363
404
  var _a, _b, _c;
364
405
  const source = ((_a = document.cookie
@@ -425,6 +466,11 @@ class SwitcherComponent {
425
466
  this.isLoadingRecentProducts = false; // Start as false since we'll load from cache first
426
467
  this.shouldShowRecentSection = false;
427
468
  this.hasLoadedData = false; // Track if we've loaded data at least once
469
+ this.isApiCallInProgress = false; // Prevent duplicate API calls
470
+ this.apiRetryCount = 0; // Track API retry attempts
471
+ this.modalHasBeenOpened = false; // Track if modal has been opened at least once
472
+ this.skeletonMinDuration = 1000; // Minimum skeleton loader duration (1 second)
473
+ this.skeletonStartTime = 0; // Track when skeleton started showing
428
474
  this.appSwitcherApps = {
429
475
  shipping_fulfilment_india: [
430
476
  {
@@ -685,10 +731,16 @@ class SwitcherComponent {
685
731
  this.appSwitchList = this.appSwitcherService.appSwitchList;
686
732
  }
687
733
  openModal(param) {
688
- var _a;
689
- console.log('package version is 0.0.71 move to update 4 sep ======');
734
+ var _a, _b, _c, _d, _e;
735
+ if (param == 'mobile') {
736
+ (_b = (_a = this.windowRef.nativeWindow) === null || _a === void 0 ? void 0 : _a.Moengage) === null || _b === void 0 ? void 0 : _b.track_event("bottom_navigation_clicked", { "company_id": this.company_id, "button_name": 'Switcher' });
737
+ }
738
+ else {
739
+ (_d = (_c = this.windowRef.nativeWindow) === null || _c === void 0 ? void 0 : _c.Moengage) === null || _d === void 0 ? void 0 : _d.track_event("button_clicked", { "company_id": this.company_id, "button_name": 'Switcher' });
740
+ }
690
741
  this.isModalOpen = true;
691
742
  this.showProducts = false;
743
+ this.modalHasBeenOpened = true; // Mark that modal has been opened
692
744
  const sidebar = document.querySelector('app-sidebar');
693
745
  if (sidebar) {
694
746
  sidebar.setAttribute('style', 'display: none');
@@ -698,7 +750,7 @@ class SwitcherComponent {
698
750
  if (intercomWidget) {
699
751
  intercomWidget.style.display = 'none';
700
752
  }
701
- // Only load data if we haven't loaded it before
753
+ // Only load data if we haven't loaded it before AND modal has been opened
702
754
  if (!this.hasLoadedData) {
703
755
  this.loadRecentProductsData();
704
756
  }
@@ -707,7 +759,7 @@ class SwitcherComponent {
707
759
  }, 200);
708
760
  if (param == 'mobile') {
709
761
  this.userJson = JSON.parse(localStorage.getItem('ngStorage-USER') || localStorage.getItem('_userData') || '{}');
710
- this.company_id = this.userJson != null ? (_a = this.userJson) === null || _a === void 0 ? void 0 : _a.company_id : '';
762
+ this.company_id = this.userJson != null ? (_e = this.userJson) === null || _e === void 0 ? void 0 : _e.company_id : '';
711
763
  this.appSwitcherService.send('All Products', 'All Products Mobile', 'Company Id: ' + this.company_id);
712
764
  }
713
765
  else if (param == 'desktop') {
@@ -736,14 +788,18 @@ class SwitcherComponent {
736
788
  this.appSwitcherApps[category] = this.appSwitcherApps[category].filter((app) => !appsToRemove.includes(app.appName));
737
789
  });
738
790
  }
739
- // Load data once on component initialization
740
- this.loadRecentProductsData();
791
+ // CRITICAL: Do NOT load data here - only load when modal is explicitly opened
792
+ // this.loadRecentProductsData(); // This should NEVER be uncommented
793
+ console.log('SwitcherComponent initialized - NO API calls should happen until modal is opened');
741
794
  }
742
795
  ngOnDestroy() {
743
796
  this.windowRef.nativeWindow.removeEventListener('resize', this.resizeListener);
744
797
  if (this.resizeTimeout) {
745
798
  clearTimeout(this.resizeTimeout);
746
799
  }
800
+ if (this.apiCallTimeout) {
801
+ clearTimeout(this.apiCallTimeout);
802
+ }
747
803
  }
748
804
  checkScreenSize() {
749
805
  const width = this.windowRef.nativeWindow.innerWidth;
@@ -775,6 +831,20 @@ class SwitcherComponent {
775
831
  return 'https://apiv2.shiprocket.co/v1/';
776
832
  }
777
833
  }
834
+ isTokenValid() {
835
+ try {
836
+ const token = this.userData.token;
837
+ if (!token)
838
+ return false;
839
+ const payload = JSON.parse(atob(token.split('.')[1]));
840
+ const now = Math.floor(Date.now() / 1000);
841
+ return payload.exp > now;
842
+ }
843
+ catch (error) {
844
+ console.warn('Invalid JWT token:', error);
845
+ return false;
846
+ }
847
+ }
778
848
  analyticsevent(item) {
779
849
  this.appSwitcherService.send('Switcher Service Selection', 'Recent Used Product', 'App: ' + item.displayName);
780
850
  }
@@ -813,7 +883,11 @@ class SwitcherComponent {
813
883
  };
814
884
  this._http.post(`${this.getBaseUrl()}settings/company-service`, data, { headers }).subscribe((res) => {
815
885
  // API call complete, but we don't wait for it
816
- }, (error) => { });
886
+ console.log('POST API call successful for:', displayName);
887
+ }, (error) => {
888
+ // Log error but don't retry for POST calls
889
+ console.warn('POST API call failed for:', displayName, error);
890
+ });
817
891
  // Handle UI updates and navigation independently with timeout
818
892
  setTimeout(() => {
819
893
  if (clickedApp) {
@@ -849,8 +923,36 @@ class SwitcherComponent {
849
923
  loadRecentProductsData() {
850
924
  // First, try to load from localStorage for immediate display
851
925
  this.loadFromCache();
852
- // Then fetch fresh data from API
853
- this.fetchRecentProductsFromAPI();
926
+ // Only fetch from API if we don't have cached data
927
+ if (this.recentUsedProducts.length === 0) {
928
+ // Show skeleton loader while fetching from API
929
+ this.shouldShowRecentSection = true;
930
+ this.isLoadingRecentProducts = true;
931
+ this.skeletonStartTime = Date.now(); // Track when skeleton started
932
+ // Debounce API calls to prevent rapid successive calls
933
+ this.debouncedFetchFromAPI();
934
+ }
935
+ else {
936
+ // We have cached data, just mark as loaded
937
+ this.hasLoadedData = true;
938
+ }
939
+ }
940
+ debouncedFetchFromAPI() {
941
+ // Clear previous timeout
942
+ if (this.apiCallTimeout) {
943
+ clearTimeout(this.apiCallTimeout);
944
+ }
945
+ // Debounce API calls by 500ms to prevent rapid successive calls
946
+ this.apiCallTimeout = setTimeout(() => {
947
+ this.fetchRecentProductsFromAPI();
948
+ }, 500);
949
+ }
950
+ hideSkeletonWithMinDuration() {
951
+ const elapsedTime = Date.now() - this.skeletonStartTime;
952
+ const remainingTime = Math.max(0, this.skeletonMinDuration - elapsedTime);
953
+ setTimeout(() => {
954
+ this.isLoadingRecentProducts = false;
955
+ }, remainingTime);
854
956
  }
855
957
  loadFromCache() {
856
958
  // Check if we have recent products stored in localStorage from previous sessions
@@ -909,15 +1011,40 @@ class SwitcherComponent {
909
1011
  // event.target.src = 'path/to/default-image.png';
910
1012
  }
911
1013
  fetchRecentProductsFromAPI() {
912
- // Only show loading if we don't have cached data
913
- if (this.recentUsedProducts.length === 0) {
914
- this.isLoadingRecentProducts = true;
1014
+ // CRITICAL: Only allow API calls when modal has been explicitly opened
1015
+ if (!this.modalHasBeenOpened) {
1016
+ console.warn('API call blocked: Modal has not been opened yet');
1017
+ return;
1018
+ }
1019
+ // Prevent duplicate API calls
1020
+ if (this.isApiCallInProgress || this.hasLoadedData) {
1021
+ return;
915
1022
  }
1023
+ // Check if token is valid before making API call
1024
+ if (!this.isTokenValid()) {
1025
+ console.warn('Invalid or expired JWT token, skipping API call');
1026
+ this.isLoadingRecentProducts = false;
1027
+ this.hasLoadedData = true;
1028
+ this.shouldShowRecentSection = false;
1029
+ return;
1030
+ }
1031
+ // Check retry limit
1032
+ if (this.apiRetryCount >= 3) {
1033
+ console.log('Max API retries reached, stopping API calls');
1034
+ this.isLoadingRecentProducts = false;
1035
+ this.hasLoadedData = true;
1036
+ this.shouldShowRecentSection = false;
1037
+ return;
1038
+ }
1039
+ this.isApiCallInProgress = true;
1040
+ // Loading state is already set in loadRecentProductsData() if needed
916
1041
  const headers = {
917
1042
  'Content-Type': 'application/json',
918
1043
  'Authorization': 'Bearer ' + this.userData.token
919
1044
  };
920
1045
  this._http.get(`${this.getBaseUrl()}settings/company-service`, { headers }).subscribe((res) => {
1046
+ // Success - reset retry count
1047
+ this.apiRetryCount = 0;
921
1048
  this.appSwitchList = res.data;
922
1049
  // Map the API response to recentUsedProducts
923
1050
  const newRecentProducts = res.data.map((item) => {
@@ -949,13 +1076,23 @@ class SwitcherComponent {
949
1076
  localStorage.removeItem('recentUsedProducts');
950
1077
  }
951
1078
  }
952
- // Set loading to false and mark as loaded
953
- this.isLoadingRecentProducts = false;
1079
+ // Hide skeleton with minimum duration to prevent flicker
1080
+ this.hideSkeletonWithMinDuration();
954
1081
  this.hasLoadedData = true;
1082
+ this.isApiCallInProgress = false;
955
1083
  }, (error) => {
956
- // Set loading to false even on error
957
- this.isLoadingRecentProducts = false;
1084
+ // Increment retry count
1085
+ this.apiRetryCount++;
1086
+ console.warn(`API call failed (attempt ${this.apiRetryCount}/3):`, error);
1087
+ // If 403 error, don't retry immediately
1088
+ if (error.status === 403) {
1089
+ console.log('403 Forbidden - stopping retries immediately');
1090
+ this.apiRetryCount = 3; // Max out retries
1091
+ }
1092
+ // Hide skeleton with minimum duration even on error
1093
+ this.hideSkeletonWithMinDuration();
958
1094
  this.hasLoadedData = true;
1095
+ this.isApiCallInProgress = false;
959
1096
  // Don't change shouldShowRecentSection if we have cached data
960
1097
  if (this.recentUsedProducts.length === 0) {
961
1098
  this.shouldShowRecentSection = false;
@@ -963,8 +1100,13 @@ class SwitcherComponent {
963
1100
  });
964
1101
  }
965
1102
  getCompanyService() {
966
- // This method is kept for backward compatibility but now just calls the new method
967
- this.fetchRecentProductsFromAPI();
1103
+ // This method is kept for backward compatibility but only calls API if modal has been opened
1104
+ if (this.modalHasBeenOpened) {
1105
+ this.fetchRecentProductsFromAPI();
1106
+ }
1107
+ else {
1108
+ console.warn('getCompanyService() blocked: Modal has not been opened yet');
1109
+ }
968
1110
  }
969
1111
  }
970
1112
  SwitcherComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: SwitcherComponent, deps: [{ token: AppSwitcherService }, { token: i2.HttpClient }, { token: WindowRef }], target: i0.ɵɵFactoryTarget.Component });