Unable to perform action on Angular 4 data table page. The NGX Store and effect are not functioning properly

Every time I click on the "go to page" button, it redirects me back to page 1. I recently switched to Angular 4, which is quite new to me since I started with Angular 1 a while ago.

The strange thing is that the console.log in the page component is logging multiple times. It first logs the correct page number, but then logs number 1 again. I've spent the whole day trying to figure it out, but it's just really weird.

Here is a snippet of my page:

<div class="portlet light">
  <div class="portlet-title">
    <div class="caption">
      <div class="caption-subject bold uppercase" translate>PLAYER_INVENTORY</div>
    </div>
  </div>
  <div class="portlet-body">
    <!-- Code omitted for brevity -->

In my component.ts file:

import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, convertToParamMap} from '@angular/router';
import {Store} from '@ngrx/store';
import {Translation, TranslationService} from 'angular-l10n';
// Other import statements omitted for brevity

@Component({ selector: 'storever-inventory', templateUrl: './inventory.component.html', styleUrls: ['./inventory.component.scss'] })
export class InventoryComponent extends Translation implements OnInit, OnDestroy {
  // Component code omitted for brevity
}

In my effect:

import {Injectable} from '@angular/core';
import {Response} from '@angular/http';
import {convertToParamMap, Params, Router} from '@angular/router';
import {Actions, Effect} from '@ngrx/effects';
// Other import statements omitted for brevity

@Injectable()
export class InventoryEffect extends BaseEffect {
  // Effect code omitted for brevity
}

In my action:

import {Action} from '@ngrx/store';
// Action code omitted for brevity

Reducer:

import {Action} from '@ngrx/store';
// Reducer code omitted for brevity

This is my service:

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs/Observable';
// Service code omitted for brevity

Answer №1

I discovered the error in the code. It occurred because of the page load requirement from the app.

import {Action} from '@ngrx/store';
import * as _ from 'lodash';

import {DEFAULT_PAGE_SIZE, UserContextActionTypes} from '../../shared';
import {
  ChangeInventoryPageSizeAction,
  ClearInventoryAction,
  InventoryActionTypes,
  LoadInventoryAction,
  LoadInventoryFailAction,
  LoadInventorySuccessAction,
  OrderInventoryByAction,
  PageInventoryToAction,
  SearchInventoryAction,
  ToggleSearchInventoryAction
} from '../actions/inventory';
import {Inventory} from '../models/inventory';
import {InventoryFilterForm} from '../models/inventory-filter-form';
import {AudioPlaylistsState} from './audio-playlists';

export interface InventoryState {
  showFilter: boolean;
  array: Inventory[];
  count: number;
}

const initialState: InventoryState = {
  showFilter: true,
  array: [],
  count: 0,
};

export function inventoryReducer(state = initialState, action: Action): InventoryState {
  switch (action.type) {
    case InventoryActionTypes.TOGGLE_SEARCH:
      return handleToggleSearchAction(state, action);
    case InventoryActionTypes.CLEAR:
      return handleClearAction(state);
    case InventoryActionTypes.LOAD_SUCCESS:
      return handleLoadSuccessAction(state, action);
    case InventoryActionTypes.LOAD_FAIL:
      return handleLoadFailAction(state);
    /*
    case InventoryActionTypes.LOAD:
      return handleLoadAction();
    */
    case UserContextActionTypes.CHANGE_CLIENT:
      return handleChangeClientAction();
    default:
      return state;
  }
}

function handleToggleSearchAction(state: InventoryState, action: ToggleSearchInventoryAction): InventoryState {
  const newState: InventoryState = { showFilter: action.payload, array: state.array, count: state.count };
  return newState;
}

function handleClearAction(state: InventoryState): InventoryState {
  const newState: InventoryState = { showFilter: state.showFilter, array: [], count: 0 };
  return newState;
}

function handleLoadSuccessAction(state: InventoryState, action: LoadInventorySuccessAction): InventoryState {
  const newState: InventoryState = { showFilter: state.showFilter, array: action.payload.array, count: action.payload.count };
  return newState;
}

function handleLoadFailAction(state: InventoryState): InventoryState {
  const newState: InventoryState = { showFilter: state.showFilter, array: [], count: 0 };
  return newState;
}

function handleChangeClientAction(): InventoryState {
  return { showFilter: true, array: [], count: 0 };
}
/*
function handleLoadAction(): InventoryState {
  return initialState;
}
*/
export const inventorySelectors = {
  showFilter: (state: InventoryState) => _.get<boolean>(state, 'showFilter', true),
  array: (state: InventoryState) => _.get<Inventory[]>(state, 'array', []),
  count: (state: InventoryState) => _.get<number>(state, 'count', 0),
};

Appreciate the feedback provided. It's always important to monitor network activity to understand the calls being made.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why isn't my Promise fulfilling its purpose?

Having trouble with promises, I believe I grasp the concept but it's not functioning as expected in my project. Here is a snippet of my code : (I am working with TypeScript using Angular 2 and Ionic 2) ngOnInit() { Promise.resolve(this.loadStatut ...

What are some key indicators in the source code that differentiate TypeScript from JavaScript?

Reviewing some code on Github, I am looking for ways to quickly determine whether the script is written in JavaScript or TypeScript. Are there any simple tips or hints that can help with this? For instance, when examining an array declaration like the on ...

React Native: Once a user has successfully logged in, I would like the app to automatically direct them to the "Home" screen

After a user signs in, I would like my app to navigate home. However, it seems this is not working because the roots have not been updated. You can view the App code here to get a better understanding of what I am trying to communicate. What is the most e ...

Angular: extracting value from forkJoin nested within another observable's pipe

Here is the scenario that needs to be implemented: An API call is made which returns a response containing an array of objects. The objects are then mapped to another array of objects. For each item in this new array, another API call needs to be made. Th ...

Using Angular to transmit data to a transcluded component

Is it possible to have a video-uploader component where users can upload one or multiple videos, with the ability to choose from three different view options: Seperate - In progress videos and uploaded videos are displayed in separate tables. Combine ...

Updating nested forms in Angular 4

The nested form structure I am working with is a 'triple level' setup: FormGroup->ArrayOfFormGroups->FormGroup At the top level (myForm): this.fb.group({ name: '', description: '', q ...

How to Extract Component Name from a URL in Angular

Routes are defined in the Angular app's app-routing.module.ts file as shown below: const routes: Routes = [ { path: 'abc/:id', component: AbcComponent }, { path: 'xyz/:id/tester/:mapId', component: XyzComponent }, ...

Leveraging Angular 8's HttpClient to retrieve and display a complex JSON object in an HTML table

I am currently working with Angular 8, where I need to query an endpoint to fetch an object. Upon calling the endpoint using Advanced REST Client, I receive the following JSON response: GET: http://localhost:8090/curso_conductor/ Response: { "dato": [ ...

Uncertainty surrounding the combination of observables due to their varying outcomes

Currently, I am developing an angular2 application that implements the ngrx store approach for state management. The source code for the app is available on github here The Issue at Hand The specific challenge I am encountering with this method involves ...

Bringing together a collection of objects connected by shared array elements

Given the types defined as: type A = { commonKey: { a: string }[] }; type B = { commonKey: { b: number }[] }; Is it possible to create the type below without explicitly specifying commonKey? type C = { commonKey: { a: string, b: number }[] } My initial a ...

"Transform the appearance of the datepicker input field with Material 15's dynamic

I am in need of assistance to change the color to white for the input date and add an underline to a datepicker element <mat-form-field class="date-criteria-select " [floatLabel]="'always'"> <mat-label class=" ...

Create a dedicated component to specify the column definition for an Angular Material table

While I have reviewed the documentation on material, I aim to take it a step further with the following customization: wrapper-table.html <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <ng-content>& ...

Configuring Typescript target and library to utilize Promise.allSettled on outdated web browsers

I am currently using TypeScript version 4.3.5 and Node.js version 14.18.1 in my project. The code I am working on is compiled to target both old and new browsers by setting target=es5 in the tsconfig file. I make use of both Promise.all and Promise.allSett ...

Steps to combine NativeScript and Angular CLI

Exploring the potential of integrating NativeScript with Angular CLI to develop applications for both web and native mobile platforms. I attempted to use Nathan Walker's NativeScript Magic, but encountered difficulties creating a fresh application wit ...

Using TypeScript's reference function within an HTML document

It feels like ages since my early days of web development. Back when I first started coding, we would reference a script using a <script> tag: <html> <head> <script src="lealet.js"></script> <!-- I know the path isn´t c ...

How to retrieve the data from a PHP file using Angular 4 CLI?

Is there a way to retrieve the response from a PHP file using Angular 4? If the PHP file is placed in the assets folder, the GET request will identify the file and proceed to download its content. For example: headers: Headers ok: true status: 200 status ...

Passing specific props to child components based on their type in a React application using TypeScript

Sorry if this question has already been addressed somewhere else, but I couldn't seem to find a solution. I'm looking for a way to pass props conditionally to children components based on their type (i.e. component type). For example, consider ...

Bringing in Angular Material on Stackblitz

After importing material(7.2.1) into my stackblitz link, I am still unable to see the exact UI of material. I have tried to figure it out, but no luck. You can view the stackblitz I created here. ...

Ionic 3 is unable to find a provider for CallNumber

Recently, I have been working with Ionic 3 and encountered an issue when trying to call a number using the Call Number plugin. Here are the steps I followed to add the plugin: ionic cordova plugin add call-number npm install --save @ionic-native/call-numb ...

Tips for preserving @typedef during the TypeScript to JavaScript transpilation process

I have a block of TypeScript code as shown below: /** * @typedef Foo * @type {Object} * @property {string} id */ type Foo = { id: string } /** * bar * @returns {Foo} */ function bar(): Foo { const foo:Foo = {id: 'foo'} return f ...