Last active
March 12, 2021 08:14
-
-
Save carlossaraiva/2e4a8da76e52dfd257925d18d5d9bbea to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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 paymentMachine = Machine({ | |
id: "payment", | |
initial: "products", | |
context: { | |
items: 0, | |
paymentForm: null, | |
retries: 0, | |
}, | |
states: { | |
products: { | |
on: { | |
ADD_PRODUCT: { | |
target: "products", | |
actions: assign({ | |
items: (context, event) => context.items + 1, | |
}), | |
cond: (context, event) => context.items < 10, | |
}, | |
REMOVE_PRODUCT: { | |
target: "products", | |
actions: assign({ | |
items: (context, event) => context.items - 1, | |
}), | |
cond: (context, event) => context.items > 0, | |
}, | |
CONFIRM: { | |
target: "payment", | |
cond: (context, event) => context.items > 0, | |
}, | |
}, | |
}, | |
payment: { | |
on: { | |
CONFIRM: "processing", | |
}, | |
}, | |
processing: { | |
id: "processing", | |
on: { | |
SUCCESS_EMPTY: "success.empty", | |
SUCCESS_ITEMS: "success.items", | |
FAILURE: "failure", | |
}, | |
}, | |
success: { | |
states: { | |
empty: { | |
type: "final", | |
}, | |
items: { | |
type: "final", | |
}, | |
}, | |
on: { | |
RESET: "products", | |
}, | |
}, | |
failure: { | |
on: { | |
'': [ | |
{ | |
target: 'retry', | |
cond: (context, event) => { | |
// check if player won | |
console.log(context.retries < 3) | |
return context.retries < 3; | |
}, | |
}, | |
{ | |
target: "fatal", | |
cond: (context, event) => { | |
return context.retries > 3; | |
}, | |
}, | |
], | |
}, | |
retry: { | |
id: 'retry', | |
on: { | |
RETRY: { | |
target: '#processing', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1, | |
}), | |
}, | |
}, | |
}, | |
fatal: { | |
}, | |
}, | |
}, | |
}); | |
const paymentService = interpret(paymentMachine); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment