Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 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 fetchMachine = Machine(
  13. {
  14. id: 'search',
  15. context: {
  16. searchFilters: {
  17. categoryIds: [],
  18. hierarchyPath: '',
  19. page: 1,
  20. sort: 'expectedValue',
  21. },
  22. isPriceSearch: true,
  23. results: {
  24. campsites: [],
  25. count: 0,
  26. facets: {},
  27. },
  28. },
  29. initial: 'idle',
  30. states: {
  31. idle: {
  32. on: {
  33. SEARCH: 'search',
  34. },
  35. },
  36. search: {
  37. initial: 'checking',
  38. onEntry: ['setFilters', 'startTimer'],
  39. on: {
  40. SEARCH: 'search',
  41. },
  42. states: {
  43. checking: {
  44. on: {
  45. '': [
  46. { target: 'price', cond: 'isPriceSearch' },
  47. { target: 'nonPrice' },
  48. ],
  49. },
  50. },
  51. price: {
  52. meta: {
  53. sortOptions: [
  54. 'expectedValue',
  55. 'ratingSort',
  56. 'priceSort',
  57. 'bestSellerSort',
  58. ],
  59. },
  60. initial: 'searching',
  61. states: {
  62. searching: {
  63. onExit: 'stopTimer',
  64. invoke: [
  65. {
  66. src: 'doPriceSearch',
  67. onDone: {
  68. target: '#results',
  69. actions: 'setResults',
  70. },
  71. onError: '#search.error',
  72. },
  73. {
  74. src: 'fetchFacetsCount',
  75. },
  76. ],
  77. },
  78. },
  79. },
  80. nonPrice: {
  81. meta: {
  82. sortOptions: ['expectedValue', 'ratingSort', 'bestSellerSort'],
  83. },
  84. initial: 'searching',
  85. states: {
  86. searching: {
  87. onExit: 'stopTimer',
  88. invoke: [
  89. {
  90. src: 'doNonPriceSearch',
  91. onDone: {
  92. target: '#results',
  93. actions: 'setResults',
  94. },
  95. onError: '#search.error',
  96. },
  97. {
  98. src: 'fetchFacetsCount',
  99. },
  100. ],
  101. },
  102.  
  103. },
  104. },
  105. },
  106. },
  107. results: {
  108. id: "results"
  109. },
  110. error: {
  111. onEntry: ['setError'],
  112. on: {
  113. SEARCH: 'search',
  114. },
  115. onExit: ['clearError'],
  116. },
  117. },
  118. },
  119. {
  120. actions: {
  121. clearError: assign(_ => {
  122. return { errorMessage: undefined }
  123. }),
  124. setError: assign((_, { data }) => {
  125. return { errorMessage: "data.toString()" }
  126. }),
  127. setFilters: assign((_, { searchFilters }) => {
  128. let isPrice = false
  129. const filters = { ...searchFilters }
  130. if (searchFilters) {
  131. isPrice = getIsPriceSearch(searchFilters)
  132. if (isPrice) {
  133. filters.party = getParty(filters.party)
  134. }
  135. }
  136. return { searchFilters: filters, isPriceSearch: isPrice }
  137. }),
  138. setResults: assign((_, { data }) => {
  139. return { results: data }
  140. }),
  141. startTimer: assign(_ => {
  142. return { startTime: performance.now() / 1000 }
  143. }),
  144. stopTimer: assign(_ => {
  145. return { endTime: performance.now() / 1000 }
  146. }),
  147. },
  148. guards: {
  149. isPriceSearch: ({ isPriceSearch }) => {
  150. return true
  151. },
  152. },
  153. }
  154. );
  155.  
  156. const getIsPriceSearch = (searchFilters) => {
  157. const range = searchFilters.range
  158. return Boolean(range && range.arrive && range.depart)
  159. }
  160.  
  161. const getParty = (party) => {
  162. return typeof party !== 'undefined'
  163. ? party
  164. : {
  165. adults: 2,
  166. childAges: [],
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement