Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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 invoiceMachine = Machine({
  13. id: 'invoice',
  14. initial: 'draft',
  15. context: {
  16. openBalance: 10,
  17. paymentMade: 10,
  18. },
  19. states: {
  20. draft: {
  21. on: {
  22. SEND_EMAIL: {
  23. target: 'emailed', actions: ['sendInvoiceEmail']
  24. },
  25. VOID: 'voided',
  26. }
  27. },
  28. emailed: {
  29. on: {
  30. APPROVE: {
  31. target: 'approved', actions: ['saveInvoiceState']
  32. },
  33. AUTO_APPROVE: {
  34. target: 'approved', actions: ['saveInvoiceState']
  35. },
  36. REJECT: {
  37. target: 'approved', actions: ['saveInvoiceState']
  38. },
  39. }
  40. },
  41. rejected: {
  42. on: {
  43. APPROVE: {
  44. target: 'approved', actions: ['saveInvoiceState']
  45. },
  46. }
  47. },
  48. approved: {
  49. on: {
  50. '': { target: 'unpaid', cond: context => context && context.paymentMade === 0, actions: ['saveInvoiceState'] },
  51. PAY: [
  52. { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
  53. { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
  54. ],
  55. GEN_NUMBER: {
  56. target: 'approved', actions: ['generateInvoiceNumber']
  57. },
  58. MOVE_TO_WALLET: {
  59. target: 'paid', actions: ['moveToWallet']
  60. },
  61. }
  62. },
  63. paid: {
  64. on: {
  65. BOUNCE: {
  66. target: 'bounced',
  67. actions: ['bounceInvoice']
  68. },
  69. REGEN: 'checking',
  70. },
  71. },
  72. bounced: {
  73. on: {
  74. PAY: [
  75. { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
  76. { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
  77. ],
  78. MOVE_TO_WALLET: {
  79. target: 'paid', actions: ['moveToWallet']
  80. },
  81. }
  82. },
  83. checking: {
  84. on: {
  85. '': [
  86. { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
  87. { target: 'partial', cond: context => context && context.openBalance > 0 , actions: ['payInvoice'] },
  88. { target: 'unpaid', cond: context => context && context.paymentMade === 0, actions: ['payInvoice'] },
  89. ]
  90. }
  91. },
  92.  
  93. partial: {
  94. on: {
  95. PAY: [
  96. { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
  97. { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
  98. ],
  99. BOUNCE: {
  100. target: 'bounced',
  101. actions: ['bounceInvoice']
  102. },
  103. GEN_NUMBER: {
  104. target: 'partial', actions: ['generateInvoiceNumber']
  105. },
  106. MOVE_TO_WALLET: {
  107. target: 'paid', actions: ['moveToWallet']
  108. },
  109. }
  110. },
  111. unpaid: {
  112. on: {
  113. PAY: [
  114. { target: 'paid', cond: context => context && context.openBalance === 0, actions: ['payInvoice'] },
  115. { target: 'partial', cond: context => context && context.openBalance > 0, actions: ['payInvoice'] },
  116. ],
  117. GEN_NUMBER: {
  118. target: 'unpaid', actions: ['generateInvoiceNumber']
  119. },
  120. MOVE_TO_WALLET: {
  121. target: 'paid', actions: ['moveToWallet']
  122. },
  123. }
  124. },
  125. voided: {
  126. type: 'final',
  127. }
  128. }
  129. }, {
  130. actions: {
  131. bounceInvoice: () => {
  132. console.log('bounce Invoice')
  133. },
  134. sendInvoiceEmail: () => {
  135. console.log('Invoice Email')
  136. },
  137. saveInvoiceState: (_1, _2, { state }) => {
  138. console.log('Save Invoice', state)
  139. },
  140. payInvoice: () => {
  141. console.log('Pay Invoice');
  142. },
  143. generateInvoiceNumber: () => {
  144. console.log('Gen Invoice Number');
  145. },
  146. moveToWallet: () => {
  147. console.log('Move to wallet');
  148. },
  149. }
  150. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement