Skip to content

Instantly share code, notes, and snippets.

@felipe
Last active October 12, 2021 20:43
Show Gist options
  • Save felipe/e7fb55cbad266ef3762e74c4b5610e04 to your computer and use it in GitHub Desktop.
Save felipe/e7fb55cbad266ef3762e74c4b5610e04 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: "creditApplication",
initial: "selectCardType",
context: {},
states: {
selectCardType: {
initial: "cardSelection",
onDone: [
{
target: "customerSearch",
cond: {
type: "hasRequiredValues",
contextItems: ["cardType"],
},
},
{
target: "selectCardType",
},
],
states: {
cardSelection: {
on: {
VISA: {
target: "selected",
actions: assign({
cardType: "VISA",
}),
},
RETAIL: {
target: "selected",
actions: assign({
cardType: "RETAIL",
}),
},
},
},
selected: {
type: "final",
},
},
},
customerSearch: {
initial: "collectCustomerMobile",
onDone: [{ target: "collectApplicantInformation" }, { target: "#creditApplication.selectCardType"}],
states: {
collectCustomerMobile: {
on: {
CONFIRM: "searchByCustomerMobile",
CANCEL: "cancel",
},
},
searchByCustomerMobile: {
on: {
FOUND: 'confirmFoundCustomerMobile',
NOTFOUND: 'collectCustomerInfo'
}
},
collectCustomerInfo: {
on: {
CONTINUE: "searchByCustomerEmail",
EDIT: "collectCustomerMobile",
},
},
searchByCustomerEmail: {
on: {
FOUND: 'confirmFoundCustomerEmail',
NOTFOUND: 'createNewCustomer'
}
},
confirmFoundCustomerMobile: {
on: {
CONFIRM: "continue",
CANCEL: "collectCustomerInfo"
}
},
confirmFoundCustomerEmail: {
on: {
CONFIRM: "continue",
EDIT: "collectCustomerInfo",
CANCEL: "confirmCancelCustomerSearch"
}
},
confirmCancelCustomerSearch: {
on: {
CONFIRM: { target: "cancel",
actions: assign({loyalty: {}})}
}
},
createNewCustomer: {
on: {
SUCCESS: "continue",
ERROR: "collectCustomerInfo"
}
},
continue: {
type: "final",
},
cancel: {
type: "final",
}
},
},
collectApplicantInformation: {
id: "collectApplicantInformation",
initial: "collect",
onDone: [
{
target: "submitCreditApplication",
cond: {
type: "hasRequiredValues",
contextItems: [
"validAddress",
"socialSecurityNumber",
"dateOfBirth",
"yearlyIncome",
],
},
}
],
states: {
collect: {
on: {
ACCEPT: {
target: "collected",
actions: assign({
validAddress: true,
socialSecurityNumber: "1",
dateOfBirth: "1",
yearlyIncome: "1",
}),
},
REJECT: {
target: "cancelled",
actions: assign({
validAddress: false,
socialSecurityNumber: "",
dateOfBirth: "",
yearlyIncome: "",
}),
}
},
},
collected: {
type: "final",
},
cancelled: {
type: "final",
},
},
},
submitCreditApplication: {
initial: "confirm",
states: {
confirm: {
on: {
ACCEPT: "submit",
REJECT: "confirm",
},
},
submit: {
on: {
APPROVED: "showApprovalMessage",
DENIED: "showDenialMessage",
REFERRED: "showReferralMessage",
FAILED: "showFailureMessage",
},
},
showApprovalMessage: {
type: "final",
},
showDenialMessage: {
type: "final",
},
showReferralMessage: {
type: "final",
},
showFailureMessage: {
on: {
RETRY: "confirm",
EXIT: "#creditApplication.selectCardType",
},
},
},
},
},
},
{
guards: {
hasRequiredValues: (context, _event, { cond }) => {
var isSet = true;
cond.contextItems.forEach((item) => {
if (!context[item]) {
isSet = false;
}
});
return isSet;
},
},
actions: {
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment