Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Available variables:
- // - Machine
- // - interpret
- // - assign
- // - send
- // - sendParent
- // - spawn
- // - raise
- // - actions
- // - XState (all XState exports)
- const machineConfig = {
- id: 'signIn',
- context: {
- email: '',
- password: ''
- },
- initial: 'dataEntry',
- states: {
- dataEntry: {
- on: {
- ENTER_EMAIL: {
- actions: 'cacheEmail'
- },
- ENTER_PASSWORD: {
- actions: 'cachePassword'
- },
- EMAIL_BLUR: { target: 'emailErr.badFormat', cond: 'isBadEmailFormat' },
- PASSWORD_BLUR: { target: 'passwordErr.tooShort', cond: 'idPasswordShort' },
- SUBMIT: [
- { target: 'emailErr.badFormat', cond: 'isBadEmailFormat' },
- { target: 'passwordErr.tooShort', cond: 'idPasswordShort' },
- { target: 'awaitingResponse' }
- ]
- }
- },
- awaitingResponse: {
- // Make a call to the authentication service
- invoke: {
- src: 'requestSignIn',
- onDone: { target: 'signedIn' },
- onError: [
- { cond: 'isNoAccount', target: 'emailErr.noAccount' },
- { cond: 'isIncorrectPassword', target: 'passwordErr.incorrect' },
- { cond: 'isServiceErr', target: 'serviceErr' }
- ]
- }
- },
- emailErr: {
- on: {
- ENTER_EMAIL: {
- actions: 'cacheEmail',
- target: 'dataEntry'
- }
- },
- initial: 'barFormat',
- states: {
- badFormat: {},
- noAccount: {}
- }
- },
- passwordErr: {
- on: {
- ENTER_PASSWORD: {
- actions: 'cachePassword',
- target: 'dataEntry'
- }
- },
- initial: 'tooShort',
- states: {
- tooShort: {},
- incorrect: {}
- }
- },
- serviceErr: {
- on: {
- SUBMIT: {
- target: 'awaitingResponse'
- }
- }
- },
- signedIn: {
- type: 'final'
- },
- onDone: {
- actions: 'onAuthentication'
- }
- }
- }
- const fetchMachine = Machine(machineConfig);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement