Last active
April 2, 2020 12:28
-
-
Save ananthhh/f0768df1d084922f937382265aae2b1d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
| const constants = { | |
| ACTIONS: { changeAction: 'changeAction', onSuccess: 'onSuccess' }, | |
| GUARDS: { isFormValid: 'isFormValid' }, | |
| SERVICES: { doSubmit: 'doSubmit' } | |
| }; | |
| const {ACTIONS, GUARDS, SERVICES} = constants; | |
| const formMachine = Machine({ | |
| id: 'form', | |
| initial: 'idle', | |
| context: { | |
| formData: {}, | |
| submitResult: undefined, | |
| submitError: undefined, | |
| }, | |
| states: { | |
| idle: { | |
| on: { | |
| SUBMIT: [ | |
| { target: 'loading', cond: GUARDS.isFormValid }, | |
| { target: 'invalid' } | |
| ], | |
| CHANGE: { | |
| actions: ACTIONS.changeAction | |
| } | |
| } | |
| }, | |
| loading: { | |
| invoke: { | |
| src: SERVICES.doSubmit, | |
| onDone: { | |
| target: 'success', | |
| actions: assign({ submitResult: (_, event) => event.data }) | |
| }, | |
| onError: { | |
| target: 'failure', | |
| actions: assign({ submitError: (_, event) => event.data }) | |
| } | |
| } | |
| }, | |
| invalid: { | |
| on: { | |
| CHANGE: [ | |
| { target: 'idle', cond: GUARDS.isFormValid }, | |
| { actions: ACTIONS.changeAction } | |
| ] | |
| } | |
| }, | |
| success: { | |
| type: 'final', | |
| entry: [ACTIONS.onSuccess] | |
| }, | |
| failure: { | |
| on: { | |
| SUBMIT: [ | |
| { target: 'loading', cond: GUARDS.isFormValid }, | |
| 'invalid' | |
| ], | |
| CHANGE: { | |
| target: 'idle', | |
| actions: ACTIONS.changeAction | |
| } | |
| } | |
| } | |
| } | |
| }, { | |
| guards: { | |
| [GUARDS.isFormValid]: (context, _event) => !Object.values(context.formData).filter(r => !r).length | |
| }, | |
| actions: { | |
| [ACTIONS.changeAction]: assign({ | |
| formData: (context, event) => ({ | |
| ...context.formData, | |
| [event.data.key]: event.data.value | |
| }) | |
| }) | |
| }, | |
| services: { | |
| [SERVICES.doSubmit]: async (context, _event) => new Promise(resolve => setTimeout(() => resolve(context.formData), 5000)) | |
| } | |
| }); | |
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
| const constants = { | |
| ACTIONS: { changeAction: 'changeAction', onSuccess: 'onSuccess' }, | |
| GUARDS: { isFormValid: 'isFormValid' }, | |
| SERVICES: { doSubmit: 'doSubmit' } | |
| }; | |
| const {ACTIONS, GUARDS, SERVICES} = constants; | |
| const formMachine = Machine({ | |
| id: 'form', | |
| initial: 'idle', | |
| context: { | |
| formData: {}, | |
| submitResult: undefined, | |
| submitError: undefined, | |
| }, | |
| states: { | |
| idle: { | |
| on: { | |
| SUBMIT: [ | |
| { target: 'loading', cond: GUARDS.isFormValid }, | |
| { target: 'invalid' } | |
| ], | |
| CHANGE: { | |
| actions: ACTIONS.changeAction | |
| } | |
| } | |
| }, | |
| loading: { | |
| invoke: { | |
| src: SERVICES.doSubmit, | |
| onDone: { | |
| target: 'success', | |
| actions: assign({ submitResult: (_, event) => event.data }) | |
| }, | |
| onError: { | |
| target: 'failure', | |
| actions: assign({ submitError: (_, event) => event.data }) | |
| } | |
| } | |
| }, | |
| invalid: { | |
| on: { | |
| CHANGE: [ | |
| { target: 'idle', cond: GUARDS.isFormValid }, | |
| { actions: ACTIONS.changeAction } | |
| ] | |
| } | |
| }, | |
| success: { | |
| type: 'final', | |
| entry: [ACTIONS.onSuccess] | |
| }, | |
| failure: { | |
| on: { | |
| SUBMIT: [ | |
| { target: 'loading', cond: GUARDS.isFormValid }, | |
| 'invalid' | |
| ], | |
| CHANGE: { | |
| target: 'idle', | |
| actions: ACTIONS.changeAction | |
| } | |
| } | |
| } | |
| } | |
| }, { | |
| guards: { | |
| [GUARDS.isFormValid]: (context, _event) => !Object.values(context.formData).filter(r => !r).length | |
| }, | |
| actions: { | |
| [ACTIONS.changeAction]: assign({ | |
| formData: (context, event) => ({ | |
| ...context.formData, | |
| [event.data.key]: event.data.value | |
| }) | |
| }) | |
| }, | |
| services: { | |
| [SERVICES.doSubmit]: async (context, _event) => new Promise(resolve => setTimeout(() => resolve(context.formData), 5000)) | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment