Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. const context = {
  2. password: ""
  3. };
  4.  
  5. const _actions = {
  6. assignPassword: XState.assign({
  7. password: (_, event) => 'password'
  8. }),
  9. validatePassword: ctx => {
  10. setTimeout(() => {
  11. if (ctx.password === "password") {
  12. send("VALID");
  13. } else {
  14. send("INVALID");
  15. }
  16. }, 2000);
  17. },
  18.  
  19. clearPassword: XState.assign({
  20. password: () => {
  21. return '';
  22. }
  23. })
  24. };
  25.  
  26. Machine(
  27. {
  28. initial: "idle",
  29. context,
  30. states: {
  31. idle: {
  32. entry: "clearPassword",
  33. on: {
  34. SUBMIT: { target: "validating", cond: "passwordEntered" },
  35. CHANGE: {
  36. target: "idle",
  37. actions: "assignPassword",
  38. internal: true // this prevents onEntry from running again
  39. }
  40. },
  41. initial: 'normal',
  42. states: {
  43. normal: {},
  44. error: {
  45. after: {
  46. 2000: "normal"
  47. }
  48. }
  49. }
  50. },
  51. validating: {
  52. onEntry: "validatePassword",
  53. on: {
  54. VALID: "success",
  55. INVALID: "idle.error"
  56. }
  57. },
  58. success: {}
  59. },
  60. on: {
  61. RESET: ".idle"
  62. }
  63. },
  64. {
  65. actions: _actions,
  66. guards: {
  67. passwordEntered: ctx => {
  68. return ctx.password && ctx.password.length
  69. }
  70. }
  71. }
  72. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement