Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 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 todoMachine = Machine({
  13. id: "todo",
  14. initial: "reading",
  15. context: {
  16. id: undefined,
  17. title: "",
  18. prevTitle: ""
  19. },
  20. on: {
  21. TOGGLE_COMPLETE: {
  22. target: ".reading.completed",
  23. actions: [assign({ completed: true }), "notifyChanged"]
  24. },
  25. DELETE: "deleted"
  26. },
  27. states: {
  28. reading: {
  29. initial: "unknown",
  30. states: {
  31. unknown: {
  32. on: {
  33. "": [
  34. { target: "completed", cond: ctx => ctx.completed },
  35. { target: "pending" }
  36. ]
  37. }
  38. },
  39. pending: {},
  40. completed: {
  41. on: {
  42. TOGGLE_COMPLETE: {
  43. target: "pending",
  44. actions: [assign({ completed: false }), "notifyChanged"]
  45. }
  46. }
  47. },
  48. hist: {
  49. type: "history"
  50. }
  51. },
  52. on: {
  53. EDIT: {
  54. target: "editing",
  55. actions: "focusInput"
  56. }
  57. }
  58. },
  59. editing: {
  60. onEntry: assign({ prevTitle: ctx => ctx.title }),
  61. on: {
  62. CHANGE: {
  63. actions: assign({
  64. title: (ctx, e) => e.value
  65. })
  66. },
  67. COMMIT: [
  68. {
  69. target: "reading.hist",
  70. actions: "notifyChanged",
  71. cond: ctx => ctx.title.trim().length > 0
  72. },
  73. { target: "deleted" }
  74. ],
  75. BLUR: {
  76. target: "reading",
  77. actions: "notifyChanged"
  78. },
  79. CANCEL: {
  80. target: "reading",
  81. actions: assign({ title: ctx => ctx.prevTitle })
  82. }
  83. }
  84. },
  85. deleted: {
  86. onEntry: "notifyDeleted"
  87. }
  88. }
  89. });
  90.  
  91.  
  92. const fetchMachine = Machine({
  93. id: "todos",
  94. context: {
  95. todo: "", // new todo
  96. todos: []
  97. },
  98. initial: "all",
  99. states: {
  100. all: {},
  101. active: {},
  102. completed: {}
  103. },
  104. on: {
  105. "NEWTODO.CHANGE": {
  106. actions: assign({
  107. todo: (ctx, e) => e.value
  108. })
  109. },
  110. "NEWTODO.COMMIT": {
  111. actions: [
  112. assign({
  113. todo: "", // clear todo
  114. todos: (ctx, e) => ctx.todos.concat(createTodo(e.value.trim()))
  115. }),
  116. "persist"
  117. ],
  118. cond: (ctx, e) => e.value.trim().length
  119. },
  120. "TODO.COMMIT": {
  121. actions: [
  122. assign({
  123. todos: (ctx, e) =>
  124. ctx.todos.map(todo => (todo.id === e.todo.id ? e.todo : todo))
  125. }),
  126. "persist"
  127. ]
  128. },
  129. "TODO.DELETE": {
  130. actions: assign({
  131. todos: (ctx, e) => {
  132. return ctx.todos.filter(todo => todo.id !== e.id);
  133. }
  134. })
  135. },
  136. "SHOW.all": ".all",
  137. "SHOW.active": ".active",
  138. "SHOW.completed": ".completed",
  139. "MARK.completed": {
  140. actions: assign({
  141. todos: ctx => ctx.todos.map(todo => ({ ...todo, completed: true }))
  142. })
  143. },
  144. "MARK.active": {
  145. actions: assign({
  146. todos: ctx => ctx.todos.map(todo => ({ ...todo, completed: false }))
  147. })
  148. },
  149. CLEAR_COMPLETED: {
  150. actions: assign({
  151. todos: ctx => ctx.todos.filter(todo => !todo.completed)
  152. })
  153. }
  154. }
  155. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement