Skip to content

Instantly share code, notes, and snippets.

@jankei
Last active January 5, 2020 14:16
Show Gist options
  • Save jankei/0fb113f1ba6178c7c100cb14ce4dc569 to your computer and use it in GitHub Desktop.
Save jankei/0fb113f1ba6178c7c100cb14ce4dc569 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine(
{
id: 'prebillerVerified',
initial: 'disabled',
context: {
warnings: {
coverageForms: {
message: 'At least one Payer must be active.',
on: true,
valid: values => values.some(c => c.policyActive),
},
patient: {
message: 'Patient Address must be specified.',
on: false,
valid: ({ address1, city, state, zip }) =>
address1 && city && state && zip.length === 5,
},
locations: {
message: 'Both addresses must either be a Facility or a Residence.',
on: false,
valid: ([a, b]) => {
const isFacility = l =>
l.location && l.location.hasOwnProperty('emsFacilityId');
return (
(isFacility(a) || a.isResidence) &&
(isFacility(b) || b.isResidence)
);
},
},
zollMsg: {
message:
'Cannot unset Prebiller Verified because trip was exported to Zoll.',
on: false,
},
chiefComplaintForm: {
message: 'Chief Complaint must be selected.',
on: false,
valid: ({ chiefComplaint }) => chiefComplaint.id,
},
chiefComplaintNotAllowedMessage: {
message: 'Selected Chief Complaint does not allow verification.',
on: false,
},
},
},
states: {
disabled: {
id: 'disabled',
on: {
CHANGE: 'changed',
},
},
changed: {
entry: ['valueChanges'],
on: {
'': [
{ target: 'enabled', cond: 'noWarnings' },
{ target: 'disabled' },
],
},
},
enabled: {
initial: 'unchecked',
on: {
CHANGE: 'changed',
},
states: {
unchecked: {
on: {
CHECK: 'checked',
},
},
checked: {
on: { UNCHECK: 'unchecked' },
},
},
},
},
},
{
actions: {
valueChanges: assign({
warnings: (context, event) => {
if (context.warnings[event.id].valid(event.values)) {
context.warnings[event.id].on = false;
} else {
context.warnings[event.id].on = true;
}
context.warnings.displayText = Object.values(context.warnings).reduce(
(acc, curr) => {
if (curr.on) {
acc += curr.message + '\n';
}
return acc;
},
''
);
return { ...context.warnings };
},
}),
},
guards: {
noWarnings: context => !Object.values(context.warnings).some(w => w.on),
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment