Making decisions in Flux
So far, we've only been creating workflows that have two states and don't diverge in any way. Flux has the capability
to decide which transition to take based on events
. For this example, we'll be making a workflow of a payment
system. A starting state simulates a checkout from a client. Another two states are for handling the chosen payment
method. One is for retrieving card details while the other is for displaying bank information. How do
we invoke a state based on a client's payment method? To do this, we create a transition for each payment method.
We'll use a Flux input called payment
that defines the chosen payment method and map it to $fluxEvent
.
- Create a transition from the
Checkout
state to theGet Card Details
state. When prompted for the event, type incard
. - Create another transition from the
Checkout
state to theDisplay Bank Information
state. When prompted for the event, type inbank
. Notice that checkout state shows a validation error. This is because we have defined the events of the transitions and$fluxEvent
is not set. We'll fix that next. - Select the
Checkout
state and go to the Mapper. Mappayment
from the Flux input to the$fluxEvent
variable. At this point, the validation error should be gone. - Provide an action for retrieving card details and displaying bank information state. For this example, we'll just log the process.
To test, run the Flux service and set the value of payment
to card
. Notice that the card details state's action is
invoked. Rerun the Flux service and this time set the value of payment
to bank
. This time, the bank detail state's
action is invoked instead.
And just like that, you've told Flux how to make a decision. $fluxEvent
is the variable that Flux checks and compares
against all available transitions to decide how to proceed. All outcomes should be accounted for. This allows you to
assign a transition that catches all other events.
Quickly navigate to states or transitions with validation issues
Use the shortcuts and to navigate to the next or previous Flux state or transition with a validation issue.