Created
December 21, 2020 10:14
-
-
Save vuhrmeister/f96529d758e49b9c860b5bfdd6b347bd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 companyMachine = Machine({ | |
id: 'company', | |
initial: 'tentative', | |
context: { | |
hasActiveContract: true, | |
minUntilDate: new Date('2020-12-31') | |
}, | |
states: { | |
tentative: { | |
always: 'sales', // TODO for now | |
on: { | |
SALES: 'sales' | |
} | |
}, | |
sales: { | |
on: { | |
ONBOARDING: 'onboarding' | |
} | |
}, | |
onboarding: { | |
on: { | |
ONBOARDED: { | |
target: 'active', | |
cond: 'hasActiveContract' | |
}, | |
SALES: 'sales' | |
} | |
}, | |
active: { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
TERMINATE: 'terminated', | |
PAYMENT_DELAYED: 'paymentDelay' | |
} | |
}, | |
terminated: { | |
entry: 'setTerminatedAt', | |
always: [ | |
{ target: '#company.inactive', cond: 'hasContractAlreadyEnded' } | |
], | |
on: { | |
CONTRACT_ENDED: '#company.inactive', | |
REACTIVATE: 'idle' | |
} | |
}, | |
paymentDelay: { | |
on: { | |
PAID: 'idle', | |
DEACTIVATE: '#company.inactive.paymentDelay' | |
} | |
} | |
} | |
}, | |
inactive: { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
SALES: '#company.sales' | |
} | |
}, | |
paymentDelay: { | |
on: { | |
PAID: '#company.active' | |
} | |
} | |
} | |
} | |
} | |
}, { | |
guards: { | |
hasActiveContract: ctx => ctx.hasActiveContract, | |
hasContractAlreadyEnded: ctx => isBeforeToday(ctx.minUntilDate) | |
}, | |
actions: { | |
setTerminatedAt: assign(ctx => ({ terminatedAt: new Date() })) | |
} | |
}) | |
function isBeforeToday (date) { | |
const until = new Date(date) | |
const today = new Date() | |
today.setHours(0) | |
today.setMinutes(0) | |
today.setSeconds(0) | |
return until.getTime() < today.getTime() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment