Reputation: 1
using ngrx store for crud operations of transactin but while deleting a transaction, if the page goes empty i want to redirect to the previous page, and also if the transaction is deleted from a page for ex.1 i want to reload and diaplay the recent transactions.
The code i have written is,
deleteTransaction(transactionId: string | undefined): void {
if (this.showForm) {
alert('Please submit or close the existing form before proceeding.');
return;
}
if (transactionId) {
this.store.dispatch(
deleteTransaction({
payload: { userId: this.userId, transactionId: transactionId },
})
);
console.log('deleted transaction');
// Reload the current page to fetch updated transactions
this.transactionService.loadTransactions(
this.userId,
this.currentPage,
this.pageSize
);
//for redirect to previous page if transactions are not available
this.store
.select(selectTransactions)
.pipe(distinctUntilChanged((prev, curr) => prev.length === curr.length))
.subscribe((transactions) => {
if (transactions.length == 0 && this.currentPage > 1) {
this.currentPage--;
console.log(this.currentPage);
this.router.navigate([], {
relativeTo: this.route,
queryParams: { page: this.currentPage },
queryParamsHandling: 'merge',
});
// Reload the current page to fetch updated transactions
this.transactionService.loadTransactions(
this.userId,
this.currentPage,
this.pageSize
);
}
});
}
}
but this code sometimes works fine, but sometimes does multiple subscrptions. How can handle this??
Upvotes: 0
Views: 21