Last active
May 29, 2020 03:07
-
-
Save ThaddeusJiang/d0faf22bb431308e9daa731a55bde469 to your computer and use it in GitHub Desktop.
order plan statechart https://xstate.js.org/viz/?gist=d0faf22bb431308e9daa731a55bde469
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) | |
const fetchMachine = Machine({ | |
id: 'order-plan', | |
initial: 'idle', | |
context: { | |
plan: undefined, | |
user: undefined, | |
error: undefined, | |
}, | |
states: { | |
idle: { | |
on: { | |
CHOOSE_PLAN: { | |
target: 'ready', | |
actions: assign({ | |
plan: (_, event) => event.data, | |
}), | |
}, | |
}, | |
}, | |
ready: { | |
on: { | |
CHECKOUT: 'waiting', | |
CHANGE_PLAN: { | |
target: 'ready', | |
actions: assign({ | |
plan: (_, event) => event.data, | |
}), | |
}, | |
}, | |
}, | |
waiting: { | |
invoke: { | |
src: 'checkout', | |
onDone: { | |
target: 'done', | |
actions: assign({ | |
result: (_, event) => event.data, | |
}), | |
}, | |
onError: { | |
target: 'error', | |
actions: assign({ | |
error: (_, event) => event.data, | |
}), | |
}, | |
}, | |
}, | |
error: { | |
on: { | |
CHECKOUT: 'waiting', | |
CHANGE_PLAN: { | |
target: 'ready', | |
actions: assign({ | |
plan: (_, event) => event.data, | |
}), | |
}, | |
}, | |
}, | |
done: { | |
on: { | |
CHECKOUT: 'waiting', | |
CHANGE_PLAN: { | |
target: 'ready', | |
actions: assign({ | |
plan: (_, event) => event.data, | |
}), | |
}, | |
SAVE_PAYMENT: 'finish', | |
}, | |
}, | |
finish: { | |
type: 'final', | |
entry: 'goNextPage', | |
}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment