What is considered as NGRX best practices: update store properties or send a request to update everything?

73 Views Asked by At

In our team we're still new to using NGRX with angular, and we faced a new situation for us. The following describe the case and the proposed scenarios:

First, we are working on users management, a simple CRUD table for users of our application. A user can have a role (Normal User, Admin). A user can be assigned to a single Admin.

our store is partially like this:

store {
  users: Users[],
  admins: Users[]
}

When loading the page at the first time, 2 requests are sent for getting users (including admins as they also can be managed by other admins), and for getting a list of admins.

So my question regarding the CRUD operations.

Add a new User: there's 2 cases for this operation, adding an admin, and adding a normal user. After getting a success response from the backend (http code 200),

should we add the new created user/admin to "users/admins" array property in the store ? or should we send a new request to get all users/admins and assign it to the "users/admins" array?

Deleting a User: there's 2 cases for this operation, deleting an admin, and deleting a normal user. If deleting a normal user, the only users array in the store should be changed. But if deleting an admin, the admins array in the store must be changed, and also we must remove the relation the deleted admin and each user in the users array, that is assigned to him.

the question here, is after getting a success response of the deletion, should we just update the store, or we must send a new request to get all users of that page and all admins and update the store ?

The same questions and 2 cases above for Updating a user

what is considered to be best practice using NGRX ?

P.S If you are wondering why using an admins array, it is because we are using lazy loading for table pagination, thus when adding/updating a user, we need the full list of all the admins to show it as dropdown in our form.

1

There are 1 best solutions below

0
Brandon Taylor On

It depends on the UX you'd like...

If you care whether or not the current user sees any users/admins created by other people, then yes, you would need to get all users/admins again.

If you don't, then you already have all of the data for your current session, and you just need to add or remove the user/admin data from your arrays.

My $0.02