I have a data structure defined as follows:
declare type Relation = {
_id?: string,
root : string,
oPrimary :string,
nodes : [Ontology]
}
declare type Ontology = {
id : string,
ont : string[]
}
In the reducer, the state is structured like this:
export interface RelatedBusinessState {
relation : Relation
}
Components are responsible for updating specific fields within this document, such as inserting or removing nodes. The data is stored in a MongoDB database, and read/write operations are handled by an @Effect calling a service.
Instead of updating the entire document with each minor change, I would like to use methods like @addtoset. Therefore, the @Effect needs to determine which selector and fields need to be updated.
What is the best approach for setting the state in the reducer function? Since the state is observable, any changes will automatically reflect in subscribers. If I introduce a new key like edit : {_id : thisid,...}, TypeScript errors occur in the @Effect stating that 'edit' does not exist on type Relation.
The @Effect saves the data, retrieves the updated document, and dispatches it to an UPDATE_COMPLETE reducer function for state update.
EDIT: Let me elaborate further. There may be no issue at hand aside from a misunderstanding. Here is my scenario.
I am observing state changes, and the component/view updates accordingly when the state changes in the reducer.
My backend storage only requires updated data to modify the document in its collection.
The nodes and ontologies frequently change in the component; additions or removals occur swiftly. I want these changes reflected promptly in the view.
An action call in the component might look similar to this, where addNode is a function dispatching the action:
addNode({ id : token, ont : ['Customer']});
The action creator appears as follows:
static ADD_NODE = '[Relations] Add Node';
addNode(node : Ontology) : Action {
return {
type : RelatedBusinessAction.ADD_NODE,
payload : node
}
}
The @Effects handle the calls to the database.
@Effect nodeadd$ = this.updates$
.whenAction(RelatedBusinessActions.ADD_NODE
.map<Relation>(toPayload)
My query pertains to what exactly does toPayload return? Does it provide the updated state set in the reducer or the payload from the action?