Last active
September 25, 2020 23:53
-
-
Save scarabaeus/dd655865c9258156f61bcd1ccc32eeec to your computer and use it in GitHub Desktop.
Example Communication State Machine: https://xstate.js.org/viz/?gist=dd655865c9258156f61bcd1ccc32eeec
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
const showNotifications = false; | |
const showExceptions = false; | |
const showSkipEvents = false; | |
const forwardOnlySkips = true; | |
const notificationActions = (stepName) => showNotifications ? ([`Send ${stepName} SMS`, `Send ${stepName} Email`, `Send ${stepName} Via Other`]) : ([]); | |
const claimDispositionActions = notificationActions('Claim Disposition'); | |
const skipEvents = (currentAction) => { | |
const skips = { | |
SKIP_TO_ASSESSMENT: {target: 'Assessment'}, | |
SKIP_TO_REPAIR: {target: 'Repair', actions: notificationActions('Repair Started')}, | |
SKIP_TO_RENTAL: {target: 'Rental', actions: notificationActions('Rental Started')}, | |
SKIP_TO_PICKUP: {target: 'Pickup', actions: notificationActions('Pickup Started')}, | |
SKIP_TO_PAYMENT: {target: 'Payment', actions: notificationActions('Payment Started')} | |
}; | |
if (forwardOnlySkips) { | |
const keys = Object.keys(skips); | |
for (let i = 0; i < keys.length; i++) { | |
delete skips[keys[i]]; | |
if (keys[i] === `SKIP_TO_${currentAction.toUpperCase()}`) { | |
break; | |
} | |
} | |
} else { | |
delete skips[`SKIP_TO_${currentAction.toUpperCase()}`] | |
} | |
return showSkipEvents ? skips : {}}; | |
const fetchMachine1 = Machine({ | |
id: 'Communication State Machine', | |
initial: 'Filed Your Claim', | |
context: {}, | |
states: { | |
'Filed Your Claim': { | |
on: { | |
ASESSMENT_MESSAGE: {target: 'Assessment', actions: notificationActions('Assess Started')} | |
} | |
}, | |
'Assessment': { | |
on: { | |
REPAIR_MESSAGE: {target: 'Repair', actions: notificationActions('Repair Started')}, | |
CLOSE_CLAIM_MESSAGE: {target: 'Claim Closed', actions: claimDispositionActions}, | |
...(showExceptions && {EXCEPTION_MESSAGE: { target: 'Call Claims Representative', actions: notificationActions('Assess Exception')}}), | |
...skipEvents('Assessment') | |
}, | |
}, | |
'Repair': { | |
on: { | |
RENTAL_MESSAGE: { target: 'Rental', actions: notificationActions('Start Rental')}, | |
CLOSE_CLAIM_MESSAGE: {target: 'Claim Closed', actions: claimDispositionActions}, | |
...(showExceptions && {EXCEPTION_MESSAGE: { target: 'Call Claims Representative', actions: notificationActions('Repair Exception')}}), | |
...skipEvents('Repair') | |
} | |
}, | |
'Rental': { | |
on: { | |
CLOSE_CLAIM_MESSAGE: {target: 'Claim Closed', actions: claimDispositionActions}, | |
PICKUP_MESSAGE: { target: 'Pickup', actions: notificationActions('Start Pickup')}, | |
...(showExceptions && {EXCEPTION_MESSAGE: { target: 'Call Claims Representative', actions: notificationActions('Rental Exception')}}), | |
...skipEvents('Rental') | |
} | |
}, | |
'Pickup': { | |
on: { | |
CLOSE_CLAIM_MESSAGE: {target: 'Claim Closed', actions: claimDispositionActions}, | |
PAYMENT_MESSAGE: { target: 'Payment', actions: notificationActions('Start Pickup')}, | |
...(showExceptions && {EXCEPTION_MESSAGE: { target: 'Call Claims Representative', actions: notificationActions('Rental Exception')}}), | |
...skipEvents('Pickup') | |
} | |
}, | |
'Payment': { | |
on: { | |
CLOSE_CLAIM_MESSAGE: {target: 'Claim Closed', actions: claimDispositionActions}, | |
...(showExceptions && {EXCEPTION_MESSAGE: { target: 'Call Claims Representative', actions: notificationActions('Rental Exception')}}), | |
...skipEvents('Payment') | |
} | |
}, | |
'Claim Closed': { | |
type: 'final' | |
}, | |
...(showExceptions && {'Call Claims Representative': { | |
on: { | |
...(showNotifications && {ASSESSMENT_EXCEPTION_RESOLVED: { | |
target: 'Assessment' | |
}, | |
REPAIR_EXCEPTION_RESOLVED: { | |
target: 'Repair' | |
}, | |
RENTAL_EXCEPTION_RESOLVED: { | |
target: 'Rental' | |
}, | |
PICKUP_EXCEPTION_RESOLVED: { | |
target: 'Pickup' | |
}, | |
PAYMENT_EXCEPTION_RESOLVED: { | |
target: 'Payment' | |
}}) | |
} | |
}}) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment