Angular application: Update state with NGXS and WebSocket, but UI not reflecting changes

I've integrated NGXS into my Angular project for state management, with updates being triggered by a WebSocket plugin associated with NGXS.

Here is the structure I have implemented:

model.ts

export interface Student {
  id: string;
  name: string;
  passed: boolean;
}

student.action.ts

export class GetStudents{
 ...
}
export class UpdateStatus {
  static readonly type = '[Student] Update Status';
  constructor( studentId: string ) {}
}

student.state.ts

...
export class StudentStateModel {
  students: Student[];
}

@State<StudentStateModel>({
  name: 'student',
  defaults: {
    students: []
  }
})

@Action(GetStudents)
... 

@Action(UpdateStatus)
  updateStatus(
    { getState, setState }: StateContext<StudentStateModel>,
    { studentId }: UpdateStatus
  ) {
  
    const students = getState().students;
    const index = students.findIndex((i) => i.id === studentId);

    setState((state: ApprovalStateModel) => {
      state.students[index].passed = true;
      return state;
    });
  }
...

I have created 2 components (parent and child):

1/ The parent component maintains the list of students

students$: Observable<any>; // Get list of Students from Store GetStudents

students.component.html

<div *ngFor= "let student of students$ | async">
    <app-student-detail
      [student] = "student"
    ></app-student-detail>
  </div>

2/ The child component displays the details of each student and includes an input

@Input() student: Student = null;

student-detail.component.html

<div *ngIf="student">
    <p>Id: {{student.id}}</p>
    <p>Name: {{student.name}}</p>
    <p>Result: {{student.passed}} </p>
  </div

I utilized the WebSocket plugin to update the student state by sending a message

 {"type": "[Student] Update Status", "studentId" : "10001"}

Outcome:

While the log indicates that the state was successfully updated with passed = true for student id 10001, the UI does not reflect this change.

I suspect there may be an issue with my implementation, any suggestions or insights would be greatly appreciated.

Answer №1

Implementing a state operator can greatly enhance your state management capabilities. One effective approach is using the updateItem method to locate and modify an item within an array:

import { patch, updateItem } from '@ngxs/store/operators';
    
...

@Action(UpdateStatus)
updateStudentStatus(ctx: StateContext<StudentStateModel>, { studentId }: UpdateStatus) {
  ctx.setState(
    patch({
      students: updateItem<Student>(
        student => student.studentId === studentId,
        student => ({
          ...student,
          passed: true
        })
      )
    })
  );
}

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 you explain the significance of the 'project' within the parserOptions in the .eslintrc.js file?

Initially, I struggle with speaking English. Apologies for the inconvenience :( Currently, I am using ESLint in Visual Studio Code and delving into studying Nest.js. I find it difficult to grasp the 'project' setting within the parserOptions sec ...

Determining the presence of an object within an array of objects in Angular

I am currently working with Angular 8 and I need to check if an object exists within an array of objects. Here is the object I want to check for: let objVenIns = { 'CntNumber': 4, 'CntMixer': 2, ...

The `HubConnection` class constructor is restricted to private access and can only be accessed within its class declaration

Currently, I am in the process of integrating an Angular and SignalR project. The guidance I am following comes from a resource on Medium. I have successfully installed all essential packages and included the code snippet below in my app.component.ts file ...

Guide on deploying an Angular 2 CLI project on a locally hosted Ubuntu production server

I'm currently working on setting up a locally hosted Ubuntu server and I'm looking to deploy an Angular 2 CLI project on it. I'm fairly new to Angular 2, so any assistance would be greatly appreciated! ...

What is the syntax for creating a link tag with interpolation in Angular 2 / Ionic 2?

As I work on developing an app using Ionic 2/Angular 2, I have encountered a challenge that I am struggling to overcome. Let me provide some context: I am retrieving multiple strings from a webservice, and some of these strings contain links. Here is an e ...

The exploration of child routes and modules

I'm currently working on a somewhat large project and I've decided to break it down into modules. However, I'm facing an issue with accessing the routes of admin.module.ts. In my app.module, I have imported the admin Module. imports: [ Br ...

Perplexed by the persistent failure of this Jasmine test accompanied by a vexing "timer in queue" error

I'm attempting to test a function that uses RxJS to broadcast long press events to subscribers. Below is the implementation of the function: export function watchForLongPress(target: HTMLElement) { let timer: number; const notifier = new Subject& ...

What is the best approach to access the reportProgress of several observables combined within a forkJoin?

I'm currently working on an Angular project where I need to upload multiple files through a form. Each file could be quite large, so I can't just do one POST request with all the files due to server size limits. It would be great if I could impl ...

The incorrect argument is being used to infer the generic type

I have implemented the NoInfer feature from the library called ts-toolbelt. However, it seems that the example provided below does not reflect its intended effect. In this scenario, the generic type is deduced from the util function as soon as it is intr ...

How can I adjust the default font and field sizes in Angular Material to enhance the user experience?

Creating a website aimed at catering to the elderly demographic. When I zoom in to 300%, it looks just right for them. Is there a way to make this the default without having to specify every HTML tag? For example, is there a mixin or another method that c ...

Trouble displaying JSON data in HTML using *ngFor in Angular

Just starting out with angular and I've created a services class that returns product details in JSON format. api.service.ts import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/ ...

Problem with Angular2, NodeJS, and Passport Integration

At the moment, my Angular2 front-end is running on localhost:3000 while the NodeJS back-end (using KrakenJS) is running on localhost:8000. When I input the credentials and make a call to the this.http.post('http://localhost:8000/login', body, { h ...

NestJs: supporting multiple directories for views

Currently, I am in the process of creating an MVC application using the nestJs framework with the hbs template-engine integrated. The documentation specifies that the following configuration is necessary for nestJs to serve views: async function bootstra ...

Eliminate repeat entries in MongoDB database

Within our MongoDB collection, we have identified duplicate revisions pertaining to the same transaction. Our goal is to clean up this collection by retaining only the most recent revision for each transaction. I have devised a script that successfully re ...

Error detected in Ionic socket.io chat page

I'm encountering a minor issue with my chat page. I'm trying to create a chat application, and everything is working fine except for the chat button, which causes the app to crash. Being a beginner, I might be missing something obvious. The issue ...

Utilizing CucumberJs, Protractor, and TypeScript to Implement Dynamic Tags/Variables in .feature Files

Currently, I am facing a challenge where I have multiple environments with various features enabled. My goal is to streamline the CI/CD process by leveraging the available variables. Is it possible to automate this process by dynamically reading these va ...

What causes JSON object properties to be null when sent as an Angular request to the Spring Java backend?

When sending a Post request from both the Angular Application and Chrome Poster, I encountered an issue. Using Chrome Poster, I provided the following information: URL: http://localhost:8080/rentapp/policy Header: content-type application/json Conte ...

Retrieve JSON Data in Angular 2

I've defined a model with the following structure: export class ExampleData{ id: string; url: string; } When I make a call to a service, it returns the JSON data shown below: [ { "id": "abc", "url": "/path/to/folder" }, { ...

Having trouble with React Hook Form controlled input and typing

My application utilizes the react-hook-forms library along with the office-ui-fabric-react framework. To integrate the framework inputs, I wrap the 3rd party component using the <Controller> element. The current setup is functional as shown below: ...

Why is the mat-toolbar and mat-toolbar-row in the footer (shared) component of Angular 6 not showing up correctly, despite no error being reported?

Despite reviewing similar questions, I have not been able to find a solution for my specific issue. I have searched through the following links but failed to find a solution: Angular Material v6 Mat-Footer - "Cannot read property 'template' of ...