Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Last active March 23, 2020 16:08
Show Gist options
  • Save DanCouper/5232727cf353b0190076ecb77263365e to your computer and use it in GitHub Desktop.
Save DanCouper/5232727cf353b0190076ecb77263365e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const authMachine = Machine({
id: 'auth',
initial: 'idle',
context: {
error: null,
isAuthorised: false,
retries: 3,
},
states: {
idle: {
on: {
SUBMITTED_USERNAME: {
target: 'validatingUsername',
},
},
},
validatingUsername: {
on: {
RESOLVED_USERNAME: {
target: 'presentCodeInput',
},
REJECTED_USERNAME: {
target: 'idle',
actions: assign({
error: (context, event) => event.error,
})
}
}
},
presentCodeInput: {
on: {
SUBMITTED_CODE:{
target: 'validatingCode',
},
}
},
validatingCode: {
on: {
RESOLVED_CODE: {
target: 'success',
actions: assign({
isAuthorised: true
}),
},
REJECTED_CODE: [
{
target: 'presentCodeInput',
cond: (context) => context.retries < 1,
actions: assign({
retries: (context, event) => context.retries - 1,
error: (context, event) => event.error
}),
},
{
target: 'idle',
actions: assign({
retries: 3,
error: (context, event) => event.error
})
},
],
}
},
success: {
type: 'final'
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment