Skip to content

Instantly share code, notes, and snippets.

@kimmobrunfeldt
Created October 8, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimmobrunfeldt/a1e8bec57f99d4dd0885 to your computer and use it in GitHub Desktop.
Save kimmobrunfeldt/a1e8bec57f99d4dd0885 to your computer and use it in GitHub Desktop.
Flux / Redux - How to chain complex actions?

Flux / Redux chaining actions?

Example scenario

Moderation UI where moderator can do various operations to comments. For example:

  • Do operations to single comments, e.g. publish a comment
  • Do operations to multiple comments at a time
  • Answer to a comment. If the original comment is not published, it will be implicitly published when moderator answers to a comment.

These operations can be categorized to two categories:

Requires only one async call to API:

  • Do operations to single comments, e.g. publish or delete a comment

Requires multiple async calls to API and more complex logic:

  • Do operations to multiple comments at a time
  • Answer to a comment. If the original comment is not published, it will be implicitly published when moderator answers to a comment.

"Primitive" actions

Async actions:

  • publishComment(id) Publishes a comment to public.
  • createModeratorComment(commentId, newComment) Creates a new moderator comment.

Sync actions:

  • showLoader()
  • hideLoader()
  • lockButtons()
  • unlockButtons()

Complex actions which consist of primitive actions

  • publishAllComments(commendIds)

    • showLoader()
    • lockButtons()
    • Call publishComment(id) for each comment. Run requests sequentially.
    • unlockButtons()
    • hideLoader()
  • answerAndPublish(moderatorComment, originalCommentId)

    • showLoader()
    • lockButtons()
    • Call createModeratorComment(moderatorComment). If it doesn't succeed, show error and stop operation.
    • Call publishComment(originalCommentId)
    • unlockButtons()
    • hideLoader()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment