Skip to content

Instantly share code, notes, and snippets.

@bsbodden
Created June 13, 2020 14:47
Show Gist options
  • Save bsbodden/91c80fe60b5d348b54c3bc91348ca9d7 to your computer and use it in GitHub Desktop.
Save bsbodden/91c80fe60b5d348b54c3bc91348ca9d7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const askForProduct = (context, event) => {}
const confirmOperation = (context, event) => {}
const askForReplacementReason = (context, event) => {}
const askForReplacementReasonExplanation = (context, event) => {}
const regularExchangeFlow = Machine(
{
id: 'regular-exchange',
initial: 'inactive',
context: {
operation: 'exchange',
targetProduct: null,
targetProductId: null,
reason: null,
scannedTags: [],
products: [],
},
states: {
inactive: {
on: {
START: [
{
target: 'product_selection',
cond: 'hasMoreThanOneProductTypeInstalled',
},
{
target: 'replacement_reason',
cond: 'hasAtLeastOneProductInstalled',
actions: (context, event) => {
const [product] = context.products;
context.targetProduct = product.HospitalProductDescription;
context.targetProductId = product.HospitalProductID;
},
},
{
target: 'no_installed_products',
},
],
},
entry: (context, event) => {
console.log('Loaded regular-exchange flow...');
},
},
product_selection: {
invoke: {
id: 'askForProduct',
src: (context, event) => askForProduct(context, event),
onDone: {
target: 'replacement_reason',
actions: assign({
targetProduct: (context, event) => event.data.name,
targetProductId: (context, event) => event.data.id,
}),
},
onError: {
target: 'cancelled',
},
},
},
replacement_reason: {
invoke: {
id: 'askForReplacementReason',
src: askForReplacementReason,
},
on: {
REASON_SELECTED: {
target: 'scan_product',
actions: assign({
reason: (context, event) => event.data,
}),
},
REASON_OTHER_SELECTED: {
target: 'replacement_reason_explanation',
},
CANCEL: 'cancelled',
},
},
replacement_reason_explanation: {
invoke: {
id: 'askForReplacementReasonExplanation',
src: askForReplacementReasonExplanation,
},
on: {
EXPLANATION_PROVIDED: {
target: 'scan_product',
actions: assign({
reason: (context, event) => event.data,
}),
},
CANCEL: 'replacement_reason',
},
},
scan_product: {
entry: ['scan'],
on: {
TAGS_SCANNED: {
target: 'confirm',
actions: assign({
scannedTags: (context, event) => {
if (event.data) {
context.scannedTags = event.data;
}
return context.scannedTags;
},
}),
},
CANCEL: 'cancelled',
},
},
confirm: {
invoke: {
id: 'confirm',
src: (context, event) => confirmOperation(context, event),
onDone: {
target: 'finished',
},
onError: {
target: 'cancelled',
},
},
},
no_installed_products: {
entry: ['announce_no_products_installed'],
on: {
'': 'cancelled',
},
},
finished: {
type: 'final',
entry: ['submit_changes'],
},
cancelled: {
type: 'final',
entry: ['go_back'],
},
},
},
{
actions: {
submit_changes: (context, event) => {},
scan: (context, event) => {},
go_back: (context, event) => {},
announce_no_products_installed: (context, event) => {},
},
guards: {
hasMoreThanOneProductTypeInstalled: context =>
context.products && context.products.length > 1,
hasAtLeastOneProductInstalled: context =>
context.products && context.products.length > 0,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment