The Angular NGRX action payload seems to undergo modifications between dispatching and execution

Angular v10, NGRX v10

I am facing a perplexing issue with my Plan object and Task properties using Angular v10 and NGRX v10. Despite making updates to Task properties within my TaskService by deep cloning the Plan object and dispatching an Action to update the Store, I notice that the Task properties revert back to their original values when logging in the Reducer. This has left me puzzled for four days now.

The process begins with dragging a Task on a Gantt Chart triggering the

TaskService.updateTasks(tasksToUpdate)
method, which is supposed to handle any changes made during user interaction.

To troubleshoot, I created a simplified version of my updateTasks method called updateTasksSimple, but the problem persists. Here is the simplified method:

 // Simplified updateTasks method
 public updateTasksSimple(tasks: ITask[]): void {
    // Code here
 }

Further investigation revealed that even after logging the updated Tasks before dispatching from TaskService, the Redux store doesn't reflect these updated values when accessed by other components subscribed to the Store. This inconsistency has me baffled.

Any guidance or insights on this matter would be greatly appreciated.

Update:

Upon further examination, I noticed a peculiar behavior in my app with regards to updating Task properties. Despite successfully updating Task properties on the Gantt page and dispatching actions to the Store, the Task page does not display the updated values if navigated directly. However, if I reload the app, update the Tasks twice on the Gantt page, and then navigate to the Task page, it shows the correct values. It seems like the initial values received by the observable are outdated, leading to this discrepancy.

This strange behavior has left me scratching my head as to why the initial values received by the observable do not reflect the current state in the Store. Any suggestions or assistance on resolving this issue would be highly valued.

Answer №1

One thing that raises suspicion is the way you handle the assignment of tasks in the code snippet. Instead of simply assigning tasks by reference, consider creating a deep copy of the updated tasks:

    // Map updated tasks
    updatedPlan.tasks = tasksToUpdate.map(t => ({
      ...t,
      t.start = new Date(2021, 2, 26, 16);
      t.end = new Date(2021, 2, 26, 22);
      t.durationHours = 6;
    }));
    // It might be better to avoid this line
    // updatedPlan.tasks = tasksToUpdate;

Answer №2

Through thorough investigation, I discovered that the issue stemmed from my misuse of NGRX. In a different section of my code, I had subscribed to a selector that returned currentPlanWithChildren.tasks, an Observable<ITask[]>. From there, I inadvertently mutated the Tasks object while sorting the array, which ultimately caused the problem. To resolve this, I simply adjusted my sorting method to tasks.slice().sort(t => ...

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

Can classes be encapsulated within a NgModule in Angular 2/4?

Looking to organize my classes by creating a module where I can easily import them like a separate package. Take a look at this example: human.ts (my class file) export class Human { private numOfLegs: Number; constructor() { this.numOfLegs = 2 ...

What is the best way to bring in a file or subfolder from a folder that has been

Currently, I am facing a situation where I need to specify to TSC in the tsconfig file that I want to include specific files or subfolders from a folder while ignoring others. How can I achieve this? For instance: /. |-folder 1 |->file2.ts |-& ...

How to transition from using a CDN to NPM for implementing the Google Maps JavaScript MarkerClusterer?

Currently integrating Google Maps JavaScript MarkerClusterer from CDN, I am considering transitioning to the NPM version for Typescript checking in my JavaScript files. However, I am encountering difficulties understanding how to make this switch. The docu ...

What is the most effective way to move specific data from one page to another in Angular/Typescript?

Welcome to my Main Page! https://i.stack.imgur.com/m9ASF.png This is where I want to start my journey. https://i.stack.imgur.com/E8pAW.png My goal is to click the Last 1 Day button to redirect to another page with the date filter and ItemId values already ...

What is the reason behind TypeScript indicating that `'string' cannot be assigned to the type 'RequestMode'`?

I'm attempting to utilize the Fetch API in TypeScript, but I keep encountering an issue The error message reads: Type 'string' is not assignable to type 'RequestMode'. Below is the code snippet causing the problem export class ...

Issue with scrollIntoView in devices with a width lower than 1200px

In my Angular 5 project, I have a table where each row is generated dynamically using *ngFor and given an id based on the username. <tbody *ngFor="let User of FullTable; let i = index"> <tr id='{{User.username}}'> <th scope="r ...

Guide on generating a request through iteration using Javascript

I'm currently working on a request that involves multiple methods, and I want to streamline the code by using an enum to iterate through and construct the request. However, my attempt at doing this has resulted in unexpected outcomes. The original co ...

The Angular component exported from one module cannot be utilized in a different module

Why am I unable to use a custom component exported in my AppModule within another module that is imported into the AppModule? Isn't an exported component supposed to be visible globally? I am attempting to utilize the CalendarComponent with the selec ...

Issue with sending post parameters through Angular http POST request

Below is the code snippet I am currently using to make the call: const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; this.http.post("https://fakedomain.com/info.php", { "uuid": ...

Cross-origin resource sharing (CORS) is preventing access because the origin and allowed origin match

Utilizing Dockerized Ory Kratos and Angular (www folder hosted via nginx for header modifications) on localhost and attempting to run the following code: const headers = { Accept: 'application/json', }; fetch('http://127.0.0.1:4433/self-s ...

Navigate in Angular to the server but add token headers using an interceptor

I need to incorporate authenticated file serving from the backend into my Angular routing: { path: 'files/:id/:name', pathMatch: 'full', ??? } My HTTP interceptor is already set up to include token headers in other requests. What I wan ...

In Javascript, what significance does the symbol ":" hold?

While exploring the Ionic framework, I came across the following code snippet: import { AlertController } from 'ionic-angular'; export class MyPage { constructor(public alertCtrl: AlertController) { } I'm curious about the significanc ...

Oops! The type '{}' is lacking the properties listed below

interface Human { firstName: string; lastName: string; } let human1: Human = {}; human1.firstName = "John" human1.lastName = "Doe" Upon declaring human1, an error pops up: Type '{}' is missing the following properties from type Human ...

I am encountering a CORS error in Nest.js despite having CORS enabled

I'm currently working on a project using next.js for the frontend and nest.js for the backend. Despite having CORS enabled in my main.ts file of nest.js, I keep encountering CORS errors. Below is an excerpt from my main.ts file: import { NestFac ...

Unable to employ the .some() method with an array consisting of objects

I am currently attempting to determine whether my array of objects contains an attribute with a specific value. I wanted to use the array.some() method instead of a foreach loop, but I keep encountering an error: Error TS2345: Argument of type '(ele ...

Tips for implementing an automatic refresh functionality in Angular

I am dealing with 3 files: model.ts, modal.html, and modal.ts. I want the auto refresh feature to only happen when the modal is open and stop when it is closed. The modal displays continuous information. modal.htlm : <button class="btn btn-success ...

A guide on implementing a "Select All" trigger in mat-select with Angular8/Material

Here is the code I have created: <mat-form-field appearance="outline"> <mat-label>Handler Type</mat-label> <mat-select multiple [(value)]="handlerType"> <mat-option *ngFor="let handler of handlerT ...

Exploring Angular: Embracing the Power of Query String Parameters

I've been struggling with subscribing to query string parameters in Angular 2+. Despite looking at various examples, I can't seem to make it work. For instance, on this Stack Overflow thread, the question is about obtaining query parameters from ...

Angular: Material Button that Adjusts According to Header Size

How can I adjust the size of a mat-icon to match the header size? Despite my efforts, the arrows remain small. <h1> <button mat-icon-button (click)="testEvent()"> <mat-icon>keyboard_arrow_up</mat-icon> < ...

Data is not being displayed in the Angular table

Currently, I am working on developing a time tracking application as part of a project. However, I have encountered an issue while attempting to showcase all the entries in a table format (as shown in the image below). Despite having two entries according ...