I'm currently using ng2 file upload from valor software to perform image upload to server.
My previous code was written this way: After added a new post, the image uploader will upload the image based on the response id (which is the post Id).
this.socialPostService.addPost(postHeader).subscribe((response: PostHeader) => {
this.pictureUploader.setOptions({
url: this.baseUrl + 'socialphotos/post/' + response.id
});
this.pictureUploader.uploadAll();
}, error => {
// this.errorService.handleError(error);
this.errorService.handleError(error);
});
I'm learning state management and moving this to NGRX approach and i found it difficult to perform the image upload after added the post because i could not return the post id from the post adding action.
Below is the new approach im trying :
Effect to work with server and add the post.
@Effect()
AddPost$: Observable<Action> = this.actions$.pipe(
ofType<PostActions.AddToPost>(
PostActions.ActionTypes.Add_To_Post
),
mergeMap((action: PostActions.AddToPost) =>
this.socialPostService.addPost(action.payload).pipe(
map((newPost: PostHeader) =>
new PostActions.AddSuccess(newPost)
),
catchError(err => of(new PostActions.AddFailed(err)))
)
)
);
Dispatch add post action :
this.store.dispatch(new postHeaderActions.AddToPost(postHeader));
//Return post Id and perform image uploading based on that
Is there anyway to return the newly added post id from the ngrx effect so that i could perform image uploading based on that?
Thanks
map
ofaddPost
? – KiraAGPostActions.AddSuccess
to call file upload. Or you need to use the selector, pick thepostId
and call the file upload. – KiraAGmap
ofaddPost
? – KiraAG