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
- Create a transition from the
Checkoutstate to the
Get Card Detailsstate. When prompted for the event, type in
- Create another transition from the
Checkoutstate to the
Display Bank Informationstate. When prompted for the event, type in
bank. Notice that checkout state shows a validation error. This is because we have defined the events of the transitions and
$fluxEventis not set. We'll fix that next.
- Select the
Checkoutstate and go to the Mapper. Map
paymentfrom the Flux input to the
$fluxEventvariable. 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
card. Notice that the card details state's action is
invoked. Rerun the Flux service and this time set the value of
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.