Last active
December 12, 2019 23:24
-
-
Save sesteva/6ec6c6aada6712e6244ac0d643ba7936 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
// split into Cart and Checkout | |
const cartActions = { | |
incrementCount: assign((ctx, evt)=> ({ | |
count: ctx.count + 1 | |
})), | |
decreaseCount: assign((ctx, evt)=> ({ | |
count: ctx.count - 1 | |
})) | |
} | |
const cartGuards= { | |
isEmpty: (ctx, evt) => ctx.count < 1 | |
} | |
const cartmachine = Machine({ | |
id: 'cart', | |
initial: 'empty', | |
context: { | |
count: 0 | |
}, | |
states: { | |
empty: { | |
on: { | |
ADD: { | |
target: 'unpaid.normal', | |
actions: ['incrementCount'] | |
}, | |
} | |
}, | |
unpaid: { | |
id: 'unpaid', | |
on: { | |
'': { | |
target: '#cart.empty', | |
cond: 'isEmpty' | |
}, | |
REMOVE: { | |
target: '.normal', | |
actions: 'decreaseCount', | |
}, | |
ADD: { | |
target: '.normal', | |
actions: ['incrementCount'] | |
}, //update count | |
PAY: '#cart.paying' | |
}, | |
states: { | |
error: {}, | |
normal: {} | |
} | |
}, | |
paying:{ | |
on: { | |
RESOLVE: 'paid', | |
REJECT: 'unpaid.error' | |
} | |
}, | |
paid: { | |
} | |
} | |
},{ | |
actions: cartActions, | |
guards: cartGuards | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment