ngOnInit(): void { this.featureInventoryProductsTableEnabled =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
ngOnInit(): void { this.featureInventoryProductsTableEnabled = checkFeatureFlag(SubstanceFeatureFlag.LIST_INVENTORY_MODE); this.isShowLoading = true; this.localeString = this.cookieHelper.getStore('languageCulture'); this._listeningEvent(); this._checkPermission(); this._getCompanyProfile(); this.checkDevice();
this.globalService.subUserProfile().pipe(takeUntil(this.ngUnsubscribe)).subscribe(userProfile => {
this.userId = userProfile.id?.toString();
const lastViewModeMobile = localStorage.getItem(`${this.userId}${Constants.localStorageKey.LAST_VIEW_MODE_MOBILE_SUBSTANCE_INDEX}`);
if (lastViewModeMobile) {
this.selectedViewMode = this.listViewMode.find(item => item.id === +lastViewModeMobile);
}
this.route.params.pipe(takeUntil(this.ngUnsubscribe)).subscribe(async params => {
if (environment.server.isAnonymous && StringHelper.isValidGUID(params['locationId'])) {
await this.globalService.subLocations(true, true, [params['locationId']]);
}
this.isShowLoading = true;
this.isChangingLocation = true;
this.inputSearch.pageIndex = this.substancePageIndex = 0;
this.setPaginator(0);
console.log(params, 'params');
if (StringHelper.isValidGUID(params['locationId']) && params['locationId'] !== this.allText) {
this.locationId = params['locationId'];
console.log(this.locationId, 'locationId 111');
this.inputSearch.locationIds = [];
this.inputSearch.locationId = this.locationId;
this.globalService.submitActiveLocation(this.locationId);
this.globalService.setLocationIdToLocalStorage(this.locationId);
this.globalService.locationList$.pipe(takeUntil(this.ngUnsubscribe)).subscribe(allLocation => {
if (!this.checkLocationExisted(allLocation)) {
this.router.navigate([this.isAnonymous ? '/not-permission' : '/substance/all']);
return;
}
console.log(this.locationId, params, 'locationId');
this.globalService.getLocationByIdNotIncludeChemicals(this.locationId, true).pipe(takeUntil(this.ngUnsubscribe)).subscribe((locationDetail) => {
if (!locationDetail) {
return;
}
locationDetail.locationAttribute = locationDetail.locationAttribute || [];
this.locationDetail = locationDetail;
console.log(this.locationDetail, 'locationDetail');
const lastActivedTab = localStorage.getItem(`${this.userId}${Constants.localStorageKey.LAST_ACTIVED_TAB}`);
if (this.isShowWATab) {
const request: GetCountWorkAssignmentInLocationRequest = new GetCountWorkAssignmentInLocationRequest();
request.locationId = this.locationId;
this.waService.getCountWorkAssignmentInLocation(request).pipe(takeUntil(this.ngUnsubscribe)).subscribe((result) => {
this.isDisableWATab = !result;
if (lastActivedTab) {
this.activeTab = this.isDisableWATab ? this.activeSubstanceTab.id : +lastActivedTab;
}
});
}
else {
this.activeTab = lastActivedTab ? +lastActivedTab : this.activeSubstanceTab.id;
}
combineLatest(
this.globalService.locationList$,
this.globalService.echa_substances$,
this.globalService.getChemicalDBConfigurationByNames([Constants.ExpiredSubstance, Constants.ConflictSubstanceWithSubstance, Constants.CheckMixturesChemicals]),
).pipe(takeUntil(this.ngUnsubscribe)).subscribe(async ([allLocation, substanceFromECHAPIC, configs]) => {
this.allLocation = allLocation;
this.listSubstanceFromECHAPIC = substanceFromECHAPIC;
this.configExpiresYear = configs.find(config => config.name === Constants.ExpiredSubstance);
this.showConflictChemicalAndChemical = configs.find(config => config.name === Constants.ConflictSubstanceWithSubstance)?.status;
this.isCheckMixturesChemical = configs.find(config => config.name === Constants.CheckMixturesChemicals)?.status;
if (this.locationDetail && this.allLocation.filter(x => x.id == this.locationDetail?.id).length > 0) {
this._initBreadcrumbData();
}
}, (error) => {
console.error(error);
});
}, (error) => {
console.error(error);
});
});
} else if (params['locationId'] !== this.allText) {
this.router.navigate([this.isAnonymous ? '/not-permission' : '/substance/all']);
return;
} else {
this.locationId = null;
this.locationDetail = null;
this.inputSearch.locationId = null;
combineLatest(
this.globalService.locationList$,
this.globalService.echa_substances$,
this.globalService.getChemicalDBConfigurationByNames([Constants.ExpiredSubstance, Constants.ConflictSubstanceWithSubstance, Constants.CheckMixturesChemicals]),
).pipe(takeUntil(this.ngUnsubscribe)).subscribe(async ([allLocation, substanceFromECHAPIC, configs]) => {
this.allLocation = allLocation;
this.listSubstanceFromECHAPIC = substanceFromECHAPIC;
this.configExpiresYear = configs.find(config => config.name === Constants.ExpiredSubstance);
this.showConflictChemicalAndChemical = configs.find(config => config.name === Constants.ConflictSubstanceWithSubstance)?.status;
this.isCheckMixturesChemical = configs.find(config => config.name === Constants.CheckMixturesChemicals)?.status;
if (this.locationDetail && this.allLocation.filter(x => x.id == this.locationDetail?.id).length > 0) {
this._initBreadcrumbData();
}
}, (error) => {
console.error(error);
});
}
}, (error) => {
console.error(error);
});
});
if (this.featureInventoryProductsTableEnabled) {
this.loadFontFamily();
}
}
when i click to other detail from '9cb9dc94-2709-46a6-a8c2-de82e402652d' to '821490e7-bab1-49f5-acd4-1c2b3a58f28d' it has issue, why '9cb9dc94-2709-46a6-a8c2-de82e402652d' is called first 9cb9dc94-2709-46a6-a8c2-de82e402652d {locationId: '9cb9dc94-2709-46a6-a8c2-de82e402652d'} locationId {locationId: '821490e7-bab1-49f5-acd4-1c2b3a58f28d'} 'params' 821490e7-bab1-49f5-acd4-1c2b3a58f28d locationId 111 821490e7-bab1-49f5-acd4-1c2b3a58f28d {locationId: '821490e7-bab1-49f5-acd4-1c2b3a58f28d'} locationId
if i try to setTimout({this.globalService.getLocationByIdNotIncludeChemi...}, 100), this is working
The issue you're encountering is likely due to the asynchronous nature of Angular's routing and the way subscriptions and observables are handled. When you navigate to a new route, the ngOnInit lifecycle hook is called, and the code inside it is executed. However, due to the asynchronous nature of the operations (like HTTP requests and subscriptions), the order of execution might not be as expected.
Here's a breakdown of what might be happening:
ngOnInit is called, and the code inside it starts executing.ngOnInit involves several asynchronous operations, such as HTTP requests and subscriptions.ngOnInit is called.Use setTimeout: As you mentioned, using setTimeout can sometimes work as a quick fix, but it's not a reliable solution because it introduces a fixed delay, which might not always be sufficient or necessary.
Unsubscribe Properly: Ensure that you properly unsubscribe from observables when the component is destroyed to avoid memory leaks and unexpected behavior. You are already using takeUntil which is good practice.
Guard Against Rapid Navigation: You can add a guard to prevent rapid navigation or ensure that the previous route's operations are completed before navigating to a new route.
Use ngOnDestroy: Ensure that any cleanup is done in ngOnDestroy to avoid lingering subscriptions or operations.
Here's a refined version of your ngOnInit with some additional checks and cleanup:
ngOnInit(): void {
this.featureInventoryProductsTableEnabled = checkFeatureFlag(SubstanceFeatureFlag.LIST_INVENTORY_MODE);
this.isShowLoading = true;
this.localeString = this.cookieHelper.getStore('languageCulture');
this._listeningEvent();
this._checkPermission();
this._getCompanyProfile();
this.checkDevice();
this.globalService.subUserProfile().pipe(takeUntil(this.ngUnsubscribe)).subscribe(userProfile => {
this.userId = userProfile.id?.toString();
const lastViewModeMobile = localStorage.getItem(`${this.userId}${Constants.localStorageKey.LAST_VIEW_MODE_MOBILE_SUBSTANCE_INDEX}`);
if (lastViewModeMobile) {
this.selectedViewMode = this.listViewMode.find(item => item.id === +lastViewModeMobile);
}
this.route.params.pipe(takeUntil(this.ngUnsubscribe)).subscribe(async params => {
if (environment.server.isAnonymous && StringHelper.isValidGUID(params['locationId'])) {
await this.globalService.subLocations(true, true, [params['locationId']]);
}
this.isShowLoading = true;
this.isChangingLocation = true;
this.inputSearch.pageIndex = this.substancePageIndex = 0;
this.setPaginator(0);
console.log(params, 'params');
if (StringHelper.isValidGUID(params['locationId']) && params['locationId'] !== this.allText) {
this.locationId = params['locationId'];
console.log(this.locationId, 'locationId 111');
this.inputSearch.locationIds = [];
this.inputSearch.locationId = this.locationId;
this.globalService.submitActiveLocation(this.locationId);
this.globalService.setLocationIdToLocalStorage(this.locationId);
this.globalService.locationList$.pipe(takeUntil(this.ngUnsubscribe)).subscribe(allLocation => {
if (!this.checkLocationExisted(allLocation)) {
this.router.navigate([this.isAnonymous ? '/not-permission' : '/substance/all']);
return;
}
console.log(this.locationId, params, 'locationId');
this.globalService.getLocationByIdNotIncludeChemicals(this.locationId, true).pipe(takeUntil(this.ngUnsubscribe)).subscribe((locationDetail) => {
if (!locationDetail) {
return;
}
locationDetail.locationAttribute = locationDetail.locationAttribute || [];
this.locationDetail = locationDetail;
console.log(this.locationDetail, 'locationDetail');
const lastActivedTab = localStorage.getItem(`${this.userId}${Constants.localStorageKey.LAST_ACTIVED_TAB}`);
if (this.isShowWATab) {
const request: GetCountWorkAssignmentInLocationRequest = new GetCountWorkAssignmentInLocationRequest();
request.locationId = this.locationId;
this.waService.getCountWorkAssignmentInLocation(request).pipe(takeUntil(this.ngUnsubscribe)).subscribe((result) => {
this.isDisableWATab = !result;
if (lastActivedTab) {
this.activeTab = this.isDisableWATab ? this.activeSubstanceTab.id : +lastActivedTab;
}
});
} else {
this.activeTab = lastActivedTab ? +lastActivedTab : this.activeSubstanceTab.id;
}
combineLatest(
this.globalService.locationList$,
this.globalService.echa_substances$,
this.globalService.getChemicalDBConfigurationByNames([Constants.ExpiredSubstance, Constants.ConflictSubstanceWithSubstance, Constants.CheckMixturesChemicals]),
).pipe(takeUntil(this.ngUnsubscribe)).subscribe(async ([allLocation, substanceFromECHAPIC, configs]) => {
this.allLocation = allLocation;
this.listSubstanceFromECHAPIC = substanceFromECHAPIC;
this.configExpiresYear = configs.find(config => config.name === Constants.ExpiredSubstance);
this.showConflictChemicalAndChemical = configs.find(config => config.name === Constants.ConflictSubstanceWithSubstance)?.status;
this.isCheckMixturesChemical = configs.find(config => config.name === Constants.CheckMixturesChemicals)?.status;
if (this.locationDetail && this.allLocation.filter(x => x.id == this.locationDetail?.id).length > 0) {
this._initBreadcrumbData();
}
}, (error) => {
console.error(error);
});
}, (error) => {
console.error(error);
});
});
} else if (params['locationId'] !== this.allText) {
this.router.navigate([this.isAnonymous ? '/not-permission' : '/substance/all']);
return;
} else {
this.locationId = null;
this.locationDetail = null;
this.inputSearch.locationId = null;
combineLatest(
this.globalService.locationList$,
this.globalService.echa_substances$,
this.globalService.getChemicalDBConfigurationByNames([Constants.ExpiredSubstance, Constants.ConflictSubstanceWithSubstance, Constants.CheckMixturesChemicals]),
).pipe(takeUntil(this.ngUnsubscribe)).subscribe(async ([allLocation, substanceFromECHAPIC, configs]) => {
this.allLocation = allLocation;
this.listSubstanceFromECHAPIC = substanceFromECHAPIC;
this.configExpiresYear = configs.find(config => config.name === Constants.ExpiredSubstance);
this.showConflictChemicalAndChemical = configs.find(config => config.name === Constants.ConflictSubstanceWithSubstance)?.status;
this.isCheckMixturesChemical = configs.find(config => config.name === Constants.CheckMixturesChemicals)?.status;
if (this.locationDetail && this.allLocation.filter(x => x.id == this.locationDetail?.id).length > 0) {
this._initBreadcrumbData();
}
}, (error) => {
console.error(error);
});
}
}, (error) => {
console.error(error);
});
});
if (this.featureInventoryProductsTableEnabled) {
this.loadFontFamily();
}
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
ngOnDestroy method ensures that all subscriptions are properly unsubscribed when the component is destroyed.ngOnDestroy helps in avoiding memory leaks and unexpected behavior.This should help in resolving the issue where the previous route's operations are still in progress when navigating to a new route.