angular - How to Reload the previous page correctly using ngrx store - Stack Overflow

using ngrx store for crud operations of transaction but while deleting a transaction, if the page goes

using ngrx store for crud operations of transaction 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 display 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??

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104387a4611470.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信