Last active
November 18, 2020 11:36
-
-
Save lbineau/d628b11dbbca0f86126c4e0f54e93890 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const onErrorHandler = { | |
target: "error", | |
actions: assign({ | |
error: (_, event) => event.data, | |
}), | |
}; | |
const INITIAL_CONTEXT = { | |
token: null, | |
error: null, | |
uiIsBusy: false | |
} | |
const paypalWorkflowMachine = Machine({ | |
id: 'paypalWorkflowMachine', | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
INITIALIZE: 'initial' | |
} | |
}, | |
initial: { | |
// Demo purpose to immediatly fire a success event | |
/* | |
entry: sendParent('PAYPAL_POPUP_SUCCESS', { | |
delay: 1000 | |
}), | |
*/ | |
on: { | |
SUCCESS: sendParent('PAYPAL_POPUP_SUCCESS'), | |
ERROR: { | |
actions: sendParent('PAYPAL_POPUP_ERROR') | |
}, | |
FAIL: { | |
actions: sendParent('PAYPAL_POPUP_FAIL') | |
} | |
} | |
} | |
} | |
}) | |
const paymentMachine = Machine({ | |
id: "BrainTreePaypalPaymentMachine", | |
context: INITIAL_CONTEXT, | |
initial: 'paymentMethodSelection', | |
states: { | |
paymentMethodSelection: { | |
on: { | |
SELECTED: 'paymentMethodSelected' | |
} | |
}, | |
paymentMethodSelected: { | |
on: { | |
REVIEW_ORDER: 'paymentMethodReview' | |
} | |
}, | |
paymentMethodReview: { | |
on: { | |
PAY_WITH_PAYPAL: 'waitingForPaypalPopup' | |
} | |
}, | |
waitingForPaypalPopup: { | |
invoke: { | |
id: 'paypalWorkflowMachine', | |
src: paypalWorkflowMachine | |
}, | |
entry: [ | |
send('INITIALIZE', { to: 'paypalWorkflowMachine' }), | |
assign({ uiIsBusy: true }) | |
], | |
exit: assign({ uiIsBusy: false }), | |
on: { | |
PAYPAL_POPUP_SUCCESS: 'handlePaypalPopupSuccess', | |
PAYPAL_POPUP_ERROR: 'handlePaypalError', | |
PAYPAL_POPUP_CANCEL: 'handlePaypalCancel', | |
} | |
}, | |
handlePaypalPopupSuccess: { | |
invoke: { | |
id: 'confirmCardPayment', | |
src: () => Promise.all("setPaymentMethodOnCartService", "placeOrderService"), | |
onDone: { | |
target: "paymentSuccess" | |
} | |
}, | |
exit: 'createOrderIdCookie' | |
}, | |
handlePaypalError: { | |
on: { | |
NEXT: 'paymentMethodSelection' | |
} | |
}, | |
handlePaypalCancel: { | |
on: { | |
NEXT: 'paymentMethodReview' | |
} | |
}, | |
paymentSuccess: { | |
type: 'final' | |
} | |
} | |
}, { | |
actions: {}, | |
guards: {} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment