Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Available variables:
- // - Machine
- // - interpret
- // - assign
- // - send
- // - sendParent
- // - spawn
- // - raise
- // - actions
- // - XState (all XState exports)
- const invoiceMachine = Machine({
- id: 'invoice',
- initial: 'draft',
- context: {
- openBalance: 10,
- paymentMade: 10,
- },
- states: {
- draft: {
- on: {
- SEND_EMAIL: {
- target: 'emailed', actions: ['sendInvoiceEmail']
- },
- VOID: 'voided',
- }
- },
- emailed: {
- on: {
- APPROVE: {
- target: 'approved', actions: ['saveInvoiceState']
- },
- AUTO_APPROVE: {
- target: 'approved', actions: ['saveInvoiceState']
- },
- REJECT: {
- target: 'approved', actions: ['saveInvoiceState']
- },
- }
- },
- rejected: {
- on: {
- APPROVE: {
- target: 'approved', actions: ['saveInvoiceState']
- },
- }
- },
- approved: {
- on: {
- '': { target: 'unpaid', cond: context => context && context.paymentMade === 0, actions: ['saveInvoiceState'] },
- PAY: [
- { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
- { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
- ],
- GEN_NUMBER: {
- target: 'approved', actions: ['generateInvoiceNumber']
- },
- MOVE_TO_WALLET: {
- target: 'paid', actions: ['moveToWallet']
- },
- }
- },
- paid: {
- on: {
- BOUNCE: {
- target: 'bounced',
- actions: ['bounceInvoice']
- },
- REGEN: 'checking',
- },
- },
- bounced: {
- on: {
- PAY: [
- { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
- { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
- ],
- MOVE_TO_WALLET: {
- target: 'paid', actions: ['moveToWallet']
- },
- }
- },
- checking: {
- on: {
- '': [
- { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
- { target: 'partial', cond: context => context && context.openBalance > 0 , actions: ['payInvoice'] },
- { target: 'unpaid', cond: context => context && context.paymentMade === 0, actions: ['payInvoice'] },
- ]
- }
- },
- partial: {
- on: {
- PAY: [
- { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
- { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
- ],
- BOUNCE: {
- target: 'bounced',
- actions: ['bounceInvoice']
- },
- GEN_NUMBER: {
- target: 'partial', actions: ['generateInvoiceNumber']
- },
- MOVE_TO_WALLET: {
- target: 'paid', actions: ['moveToWallet']
- },
- }
- },
- unpaid: {
- on: {
- PAY: [
- { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
- { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
- ],
- GEN_NUMBER: {
- target: 'unpaid', actions: ['generateInvoiceNumber']
- },
- MOVE_TO_WALLET: {
- target: 'paid', actions: ['moveToWallet']
- },
- }
- },
- voided: {
- type: 'final',
- }
- }
- }, {
- actions: {
- bounceInvoice: () => {
- console.log('bounce Invoice')
- },
- sendInvoiceEmail: () => {
- console.log('Invoice Email')
- },
- saveInvoiceState: (_1, _2, { state }) => {
- console.log('Save Invoice', state)
- },
- payInvoice: () => {
- console.log('Pay Invoice');
- },
- generateInvoiceNumber: () => {
- console.log('Gen Invoice Number');
- },
- moveToWallet: () => {
- console.log('Move to wallet');
- },
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement