Skip to content

Instantly share code, notes, and snippets.

@vuhrmeister
Created December 21, 2020 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vuhrmeister/f96529d758e49b9c860b5bfdd6b347bd to your computer and use it in GitHub Desktop.
Save vuhrmeister/f96529d758e49b9c860b5bfdd6b347bd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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