Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // Available variables:
  2. // - Machine
  3. // - interpret
  4. // - assign
  5. // - send
  6. // - sendParent
  7. // - spawn
  8. // - raise
  9. // - actions
  10. // - XState (all XState exports)
  11.  
  12. const machineConfig = {
  13. id: 'signIn',
  14.  
  15. context: {
  16. email: '',
  17. password: ''
  18. },
  19.  
  20. initial: 'dataEntry',
  21.  
  22. states: {
  23. dataEntry: {
  24. on: {
  25. ENTER_EMAIL: {
  26. actions: 'cacheEmail'
  27. },
  28. ENTER_PASSWORD: {
  29. actions: 'cachePassword'
  30. },
  31. EMAIL_BLUR: { target: 'emailErr.badFormat', cond: 'isBadEmailFormat' },
  32. PASSWORD_BLUR: { target: 'passwordErr.tooShort', cond: 'idPasswordShort' },
  33. SUBMIT: [
  34. { target: 'emailErr.badFormat', cond: 'isBadEmailFormat' },
  35. { target: 'passwordErr.tooShort', cond: 'idPasswordShort' },
  36. { target: 'awaitingResponse' }
  37. ]
  38. }
  39. },
  40.  
  41. awaitingResponse: {
  42. // Make a call to the authentication service
  43. invoke: {
  44. src: 'requestSignIn',
  45. onDone: { target: 'signedIn' },
  46. onError: [
  47. { cond: 'isNoAccount', target: 'emailErr.noAccount' },
  48. { cond: 'isIncorrectPassword', target: 'passwordErr.incorrect' },
  49. { cond: 'isServiceErr', target: 'serviceErr' }
  50. ]
  51. }
  52. },
  53.  
  54. emailErr: {
  55. on: {
  56. ENTER_EMAIL: {
  57. actions: 'cacheEmail',
  58. target: 'dataEntry'
  59. }
  60. },
  61. initial: 'barFormat',
  62. states: {
  63. badFormat: {},
  64. noAccount: {}
  65. }
  66. },
  67.  
  68. passwordErr: {
  69. on: {
  70. ENTER_PASSWORD: {
  71. actions: 'cachePassword',
  72. target: 'dataEntry'
  73. }
  74. },
  75. initial: 'tooShort',
  76. states: {
  77. tooShort: {},
  78. incorrect: {}
  79. }
  80. },
  81. serviceErr: {
  82. on: {
  83. SUBMIT: {
  84. target: 'awaitingResponse'
  85. }
  86. }
  87. },
  88. signedIn: {
  89. type: 'final'
  90. },
  91. onDone: {
  92. actions: 'onAuthentication'
  93. }
  94. }
  95. }
  96.  
  97.  
  98. const fetchMachine = Machine(machineConfig);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement