Reputation: 1825
I have a type like this:
declare type Relation = {
_id?: string,
root : string,
oPrimary :string,
nodes : [Ontology]
}
declare type Ontology = {
id : string,
ont : string[]
}
and in my reducer, I have a state defined like this:
export interface RelatedBusinessState {
relation : Relation
}
My components update fields in this document, usually inserting or removing a node.The data is stored in a mongo db, and the reading and writing to the db is done in an @Effect via a call to a service.
I would prefer not to update the whole document every time there is a small change, but @addtoset or similar. So the @Effect needs to know enough for a selector, and the specific fields that need change.
What is the best way to set the state in the reducer function? The state is an observable so any changes will be reflected in any subscriber. If I add a key in the object, say edit : {_id : thisid,...} I get typescript errors in the @Effect function that Property 'edit' does not exist on type Relation.
The @Effect saves the data and gets the updated document, dispatching it to an UPDATE_COMPLETE reducer function that updates the state.
EDIT: I'll try to clarify. There may not be an issue at all except my misunderstanding. But here goes.
I'm subscribing to the state, and the component and view will update when the state is changed in the reducer.
My backend data storage requires only the changed data to update the document in the collection.
The nodes and ontologies will be changed frequently and quickly in the component; simply adding something or removing something. I want the view to reflect the change quickly.
So an action call in the component would look something like this, where addNode is a function that dispatches the action.
addNode({ id : token, ont : ['Customer']});
The action creator would look like this:
static ADD_NODE = '[Relations] Add Node';
addNode(node : Ontology) : Action {
return {
type : RelatedBusinessAction.ADD_NODE,
payload : node
}
}
The @Effects are where the database calls are made.
@Effect nodeadd$ = this.updates$
.whenAction(RelatedBusinessActions.ADD_NODE
.map<Relation>(toPayload)
My question is what does toPayload return? Does it return the updated state set in the reducer, or does it return the payload of the action?
Upvotes: 0
Views: 1649
Reputation: 3315
My question is what does toPayload return? Does it return the updated state set in the reducer, or does it return the payload of the action?
it's simply return action.payload.
export function toPayload(action: Action) {
return action.payload;
}
Upvotes: 0