@global.engg/switcher-lib 0.1.7 → 0.1.16

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.
@@ -175,6 +175,7 @@
175
175
  AppSwitcherService.prototype.appSwitch = function (app, kyc_status) {
176
176
  var _a, _b, _c;
177
177
  this.sendGaEvent(app);
178
+ this.moengageEvent(app);
178
179
  if (this.userData.allow_engage && app === 'engage') {
179
180
  window.open(this.SR_CONSTANTS.EngageWebBaseUrl + "gate-keeper?token=" + this.userData.token, '_blank');
180
181
  }
@@ -388,6 +389,10 @@
388
389
  }
389
390
  this.send(category, eventName, label);
390
391
  };
392
+ AppSwitcherService.prototype.moengageEvent = function (event) {
393
+ var _a, _b;
394
+ (_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: event, company_id: this.userData.company_id });
395
+ };
391
396
  AppSwitcherService.prototype.send = function (eventCategory, eventAction, eventLabel) {
392
397
  var _a, _b, _c;
393
398
  var source = ((_a = document.cookie
@@ -456,6 +461,11 @@
456
461
  this.isLoadingRecentProducts = false; // Start as false since we'll load from cache first
457
462
  this.shouldShowRecentSection = false;
458
463
  this.hasLoadedData = false; // Track if we've loaded data at least once
464
+ this.isApiCallInProgress = false; // Prevent duplicate API calls
465
+ this.apiRetryCount = 0; // Track API retry attempts
466
+ this.modalHasBeenOpened = false; // Track if modal has been opened at least once
467
+ this.skeletonMinDuration = 1000; // Minimum skeleton loader duration (1 second)
468
+ this.skeletonStartTime = 0; // Track when skeleton started showing
459
469
  this.appSwitcherApps = {
460
470
  shipping_fulfilment_india: [
461
471
  {
@@ -721,6 +731,7 @@
721
731
  console.log('package version is 0.0.71 move to update 4 sep ======');
722
732
  this.isModalOpen = true;
723
733
  this.showProducts = false;
734
+ this.modalHasBeenOpened = true; // Mark that modal has been opened
724
735
  var sidebar = document.querySelector('app-sidebar');
725
736
  if (sidebar) {
726
737
  sidebar.setAttribute('style', 'display: none');
@@ -730,7 +741,7 @@
730
741
  if (intercomWidget) {
731
742
  intercomWidget.style.display = 'none';
732
743
  }
733
- // Only load data if we haven't loaded it before
744
+ // Only load data if we haven't loaded it before AND modal has been opened
734
745
  if (!this.hasLoadedData) {
735
746
  this.loadRecentProductsData();
736
747
  }
@@ -769,14 +780,18 @@
769
780
  _this.appSwitcherApps[category] = _this.appSwitcherApps[category].filter(function (app) { return !appsToRemove_1.includes(app.appName); });
770
781
  });
771
782
  }
772
- // Load data once on component initialization
773
- this.loadRecentProductsData();
783
+ // CRITICAL: Do NOT load data here - only load when modal is explicitly opened
784
+ // this.loadRecentProductsData(); // This should NEVER be uncommented
785
+ console.log('SwitcherComponent initialized - NO API calls should happen until modal is opened');
774
786
  };
775
787
  SwitcherComponent.prototype.ngOnDestroy = function () {
776
788
  this.windowRef.nativeWindow.removeEventListener('resize', this.resizeListener);
777
789
  if (this.resizeTimeout) {
778
790
  clearTimeout(this.resizeTimeout);
779
791
  }
792
+ if (this.apiCallTimeout) {
793
+ clearTimeout(this.apiCallTimeout);
794
+ }
780
795
  };
781
796
  SwitcherComponent.prototype.checkScreenSize = function () {
782
797
  var width = this.windowRef.nativeWindow.innerWidth;
@@ -809,6 +824,20 @@
809
824
  return 'https://apiv2.shiprocket.co/v1/';
810
825
  }
811
826
  };
827
+ SwitcherComponent.prototype.isTokenValid = function () {
828
+ try {
829
+ var token = this.userData.token;
830
+ if (!token)
831
+ return false;
832
+ var payload = JSON.parse(atob(token.split('.')[1]));
833
+ var now = Math.floor(Date.now() / 1000);
834
+ return payload.exp > now;
835
+ }
836
+ catch (error) {
837
+ console.warn('Invalid JWT token:', error);
838
+ return false;
839
+ }
840
+ };
812
841
  SwitcherComponent.prototype.analyticsevent = function (item) {
813
842
  this.appSwitcherService.send('Switcher Service Selection', 'Recent Used Product', 'App: ' + item.displayName);
814
843
  };
@@ -848,7 +877,11 @@
848
877
  };
849
878
  this._http.post(this.getBaseUrl() + "settings/company-service", data, { headers: headers }).subscribe(function (res) {
850
879
  // API call complete, but we don't wait for it
851
- }, function (error) { });
880
+ console.log('POST API call successful for:', displayName);
881
+ }, function (error) {
882
+ // Log error but don't retry for POST calls
883
+ console.warn('POST API call failed for:', displayName, error);
884
+ });
852
885
  // Handle UI updates and navigation independently with timeout
853
886
  setTimeout(function () {
854
887
  if (clickedApp) {
@@ -884,8 +917,38 @@
884
917
  SwitcherComponent.prototype.loadRecentProductsData = function () {
885
918
  // First, try to load from localStorage for immediate display
886
919
  this.loadFromCache();
887
- // Then fetch fresh data from API
888
- this.fetchRecentProductsFromAPI();
920
+ // Only fetch from API if we don't have cached data
921
+ if (this.recentUsedProducts.length === 0) {
922
+ // Show skeleton loader while fetching from API
923
+ this.shouldShowRecentSection = true;
924
+ this.isLoadingRecentProducts = true;
925
+ this.skeletonStartTime = Date.now(); // Track when skeleton started
926
+ // Debounce API calls to prevent rapid successive calls
927
+ this.debouncedFetchFromAPI();
928
+ }
929
+ else {
930
+ // We have cached data, just mark as loaded
931
+ this.hasLoadedData = true;
932
+ }
933
+ };
934
+ SwitcherComponent.prototype.debouncedFetchFromAPI = function () {
935
+ var _this = this;
936
+ // Clear previous timeout
937
+ if (this.apiCallTimeout) {
938
+ clearTimeout(this.apiCallTimeout);
939
+ }
940
+ // Debounce API calls by 500ms to prevent rapid successive calls
941
+ this.apiCallTimeout = setTimeout(function () {
942
+ _this.fetchRecentProductsFromAPI();
943
+ }, 500);
944
+ };
945
+ SwitcherComponent.prototype.hideSkeletonWithMinDuration = function () {
946
+ var _this = this;
947
+ var elapsedTime = Date.now() - this.skeletonStartTime;
948
+ var remainingTime = Math.max(0, this.skeletonMinDuration - elapsedTime);
949
+ setTimeout(function () {
950
+ _this.isLoadingRecentProducts = false;
951
+ }, remainingTime);
889
952
  };
890
953
  SwitcherComponent.prototype.loadFromCache = function () {
891
954
  // Check if we have recent products stored in localStorage from previous sessions
@@ -945,15 +1008,40 @@
945
1008
  };
946
1009
  SwitcherComponent.prototype.fetchRecentProductsFromAPI = function () {
947
1010
  var _this = this;
948
- // Only show loading if we don't have cached data
949
- if (this.recentUsedProducts.length === 0) {
950
- this.isLoadingRecentProducts = true;
1011
+ // CRITICAL: Only allow API calls when modal has been explicitly opened
1012
+ if (!this.modalHasBeenOpened) {
1013
+ console.warn('API call blocked: Modal has not been opened yet');
1014
+ return;
1015
+ }
1016
+ // Prevent duplicate API calls
1017
+ if (this.isApiCallInProgress || this.hasLoadedData) {
1018
+ return;
1019
+ }
1020
+ // Check if token is valid before making API call
1021
+ if (!this.isTokenValid()) {
1022
+ console.warn('Invalid or expired JWT token, skipping API call');
1023
+ this.isLoadingRecentProducts = false;
1024
+ this.hasLoadedData = true;
1025
+ this.shouldShowRecentSection = false;
1026
+ return;
951
1027
  }
1028
+ // Check retry limit
1029
+ if (this.apiRetryCount >= 3) {
1030
+ console.log('Max API retries reached, stopping API calls');
1031
+ this.isLoadingRecentProducts = false;
1032
+ this.hasLoadedData = true;
1033
+ this.shouldShowRecentSection = false;
1034
+ return;
1035
+ }
1036
+ this.isApiCallInProgress = true;
1037
+ // Loading state is already set in loadRecentProductsData() if needed
952
1038
  var headers = {
953
1039
  'Content-Type': 'application/json',
954
1040
  'Authorization': 'Bearer ' + this.userData.token
955
1041
  };
956
1042
  this._http.get(this.getBaseUrl() + "settings/company-service", { headers: headers }).subscribe(function (res) {
1043
+ // Success - reset retry count
1044
+ _this.apiRetryCount = 0;
957
1045
  _this.appSwitchList = res.data;
958
1046
  // Map the API response to recentUsedProducts
959
1047
  var newRecentProducts = res.data.map(function (item) {
@@ -985,13 +1073,23 @@
985
1073
  localStorage.removeItem('recentUsedProducts');
986
1074
  }
987
1075
  }
988
- // Set loading to false and mark as loaded
989
- _this.isLoadingRecentProducts = false;
1076
+ // Hide skeleton with minimum duration to prevent flicker
1077
+ _this.hideSkeletonWithMinDuration();
990
1078
  _this.hasLoadedData = true;
1079
+ _this.isApiCallInProgress = false;
991
1080
  }, function (error) {
992
- // Set loading to false even on error
993
- _this.isLoadingRecentProducts = false;
1081
+ // Increment retry count
1082
+ _this.apiRetryCount++;
1083
+ console.warn("API call failed (attempt " + _this.apiRetryCount + "/3):", error);
1084
+ // If 403 error, don't retry immediately
1085
+ if (error.status === 403) {
1086
+ console.log('403 Forbidden - stopping retries immediately');
1087
+ _this.apiRetryCount = 3; // Max out retries
1088
+ }
1089
+ // Hide skeleton with minimum duration even on error
1090
+ _this.hideSkeletonWithMinDuration();
994
1091
  _this.hasLoadedData = true;
1092
+ _this.isApiCallInProgress = false;
995
1093
  // Don't change shouldShowRecentSection if we have cached data
996
1094
  if (_this.recentUsedProducts.length === 0) {
997
1095
  _this.shouldShowRecentSection = false;
@@ -999,8 +1097,13 @@
999
1097
  });
1000
1098
  };
1001
1099
  SwitcherComponent.prototype.getCompanyService = function () {
1002
- // This method is kept for backward compatibility but now just calls the new method
1003
- this.fetchRecentProductsFromAPI();
1100
+ // This method is kept for backward compatibility but only calls API if modal has been opened
1101
+ if (this.modalHasBeenOpened) {
1102
+ this.fetchRecentProductsFromAPI();
1103
+ }
1104
+ else {
1105
+ console.warn('getCompanyService() blocked: Modal has not been opened yet');
1106
+ }
1004
1107
  };
1005
1108
  return SwitcherComponent;
1006
1109
  }());