I´m working with Angular and NGXS on a simple application to let users create simple products with tags, but I´m struggling with updating the nested properties.
I have a User model with a Product array.
export interface User {
id: number;
name: string;
products: Product[];
}
And the Product model has an array of tags.
export interface Product {
id: number;
name: string;
tags: Tag[];
}
And the Tag model has also some properties.
export interface Tag {
id: number;
name: string;
}
I want to be able to ADD, GET, UPDATE, REMOVE single Products or single Tags.
On the GitHub Documentation of NGXS (https://github.com/ngxs/store/blob/master/docs/recipes/immutability-helpers.md) I found this examples which updates a simgle property in an array:
export interface TrelloStateModel {
tasks: {
[taskId: string]: Task;
};
}
export class TrelloState {
@Action(UpdateDueDate)
updateDueDate(ctx: StateContext<TrelloStateModel>, action: UpdateDueDate) {
ctx.setState(
patch({
tasks: patch({
[action.taskId]: patch({
dates: patch({
dueDate: action.dueDate
})
})
})
})
);
}
}
But I simply don´t know how to make this work for my object model. Is there a better alternative to this?