Last active
March 23, 2020 16:08
-
-
Save DanCouper/5232727cf353b0190076ecb77263365e 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 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