InstanceStore.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import alt from '../alt'
  15. import InstanceActions from '../actions/InstanceActions'
  16. import { wizardConfig } from '../config'
  17. class InstanceStoreUtils {
  18. static hasNextPage(instances) {
  19. let result = false
  20. if (instances.length - 1 === wizardConfig.instancesItemsPerPage) {
  21. result = true
  22. instances.pop()
  23. }
  24. return result
  25. }
  26. static loadFromCache(cache, page) {
  27. let startIndex = wizardConfig.instancesItemsPerPage * (page - 1)
  28. let endIndex = startIndex + wizardConfig.instancesItemsPerPage
  29. return cache.filter((item, index) => {
  30. if (index >= startIndex && index < endIndex) {
  31. return true
  32. }
  33. return false
  34. })
  35. }
  36. }
  37. class InstanceStore {
  38. constructor() {
  39. this.instances = []
  40. this.instancesLoading = false
  41. this.searching = false
  42. this.searchNotFound = false
  43. this.loadingPage = false
  44. this.currentPage = 1
  45. this.hasNextPage = false
  46. this.cachedHasNextPage = false
  47. this.cachedInstances = []
  48. this.reloading = false
  49. this.instancesDetails = []
  50. this.loadingInstancesDetails = true
  51. this.bindListeners({
  52. handleLoadInstances: InstanceActions.LOAD_INSTANCES,
  53. handleLoadInstancesSuccess: InstanceActions.LOAD_INSTANCES_SUCCESS,
  54. handleLoadInstancesFailed: InstanceActions.LOAD_INSTANCES_FAILED,
  55. handleSearchInstances: InstanceActions.SEARCH_INSTANCES,
  56. handleSearchInstancesSuccess: InstanceActions.SEARCH_INSTANCES_SUCCESS,
  57. handleSearchInstancesFailed: InstanceActions.SEARCH_INSTANCES_FAILED,
  58. handleLoadNextPage: InstanceActions.LOAD_NEXT_PAGE,
  59. handleLoadNextPageSuccess: InstanceActions.LOAD_NEXT_PAGE_SUCCESS,
  60. handleLoadNextPageFailed: InstanceActions.LOAD_NEXT_PAGE_FAILED,
  61. handleLoadPreviousPage: InstanceActions.LOAD_PREVIOUS_PAGE,
  62. handleReloadInstances: InstanceActions.RELOAD_INSTANCES,
  63. handleReloadInstancesSuccess: InstanceActions.RELOAD_INSTANCES_SUCCESS,
  64. handleReloadInstancesFailed: InstanceActions.RELOAD_INSTANCES_FAILED,
  65. handleLoadInstancesDetails: InstanceActions.LOAD_INSTANCES_DETAILS,
  66. handleLoadInstanceDetailsSuccess: InstanceActions.LOAD_INSTANCE_DETAILS_SUCCESS,
  67. handleLoadInstanceDetailsFailed: InstanceActions.LOAD_INSTANCE_DETAILS_FAILED,
  68. })
  69. }
  70. handleLoadInstances(endpointId) {
  71. this.endpointId = endpointId
  72. this.instancesLoading = true
  73. this.searchNotFound = false
  74. }
  75. handleLoadInstancesSuccess({ endpointId, instances }) {
  76. if (endpointId !== this.endpointId) {
  77. return
  78. }
  79. this.currentPage = 1
  80. this.hasNextPage = InstanceStoreUtils.hasNextPage(instances)
  81. this.instances = instances
  82. this.cachedInstances = instances
  83. this.instancesLoading = false
  84. }
  85. handleLoadInstancesFailed({ endpointId }) {
  86. if (endpointId !== this.endpointId) {
  87. return
  88. }
  89. this.instancesLoading = false
  90. }
  91. handleSearchInstances() {
  92. this.searching = true
  93. }
  94. handleSearchInstancesSuccess({ instances, searchText }) {
  95. this.currentPage = 1
  96. this.hasNextPage = InstanceStoreUtils.hasNextPage(instances)
  97. this.instances = instances
  98. this.cachedInstances = instances
  99. this.searching = false
  100. this.searchNotFound = instances.length === 0 && searchText
  101. }
  102. handleSearchInstancesFailed() {
  103. this.searching = false
  104. this.searchNotFound = true
  105. }
  106. handleLoadNextPage({ fromCache }) {
  107. if (!fromCache) {
  108. this.loadingPage = true
  109. return
  110. }
  111. this.currentPage = this.currentPage + 1
  112. let numCachedPages = Math.ceil(this.cachedInstances.length / wizardConfig.instancesItemsPerPage)
  113. if (this.currentPage === numCachedPages) {
  114. this.hasNextPage = this.cachedHasNextPage
  115. } else {
  116. this.hasNextPage = true
  117. }
  118. this.instances = InstanceStoreUtils.loadFromCache(this.cachedInstances, this.currentPage)
  119. }
  120. handleLoadNextPageSuccess(instances) {
  121. this.hasNextPage = InstanceStoreUtils.hasNextPage(instances)
  122. this.cachedHasNextPage = this.hasNextPage
  123. this.cachedInstances = [...this.cachedInstances, ...instances]
  124. this.instances = instances
  125. this.loadingPage = false
  126. this.currentPage = this.currentPage + 1
  127. }
  128. handleLoadNextPageFailed() {
  129. this.loadingPage = false
  130. }
  131. handleLoadPreviousPage() {
  132. this.hasNextPage = true
  133. this.currentPage = this.currentPage - 1
  134. this.instances = InstanceStoreUtils.loadFromCache(this.cachedInstances, this.currentPage)
  135. }
  136. handleReloadInstances() {
  137. this.reloading = true
  138. this.searchNotFound = false
  139. }
  140. handleReloadInstancesSuccess({ instances, searchText }) {
  141. this.reloading = false
  142. this.currentPage = 1
  143. this.hasNextPage = InstanceStoreUtils.hasNextPage(instances)
  144. this.instances = instances
  145. this.cachedInstances = instances
  146. this.searching = false
  147. this.searchNotFound = instances.length === 0 && searchText
  148. }
  149. handleReloadInstancesFailed() {
  150. this.reloading = false
  151. this.searchNotFound = true
  152. }
  153. handleLoadInstancesDetails({ count }) {
  154. this.loadingInstancesDetails = true
  155. this.instancesDetailsCount = count
  156. this.instancesDetails = []
  157. }
  158. handleLoadInstanceDetailsSuccess(instance) {
  159. this.instancesDetailsCount -= 1
  160. this.loadingInstancesDetails = this.instancesDetailsCount > 0
  161. if (this.instancesDetails.find(i => i.id === instance.id)) {
  162. this.instancesDetails = this.instancesDetails.filter(i => i.id !== instance.id)
  163. }
  164. this.instancesDetails = [
  165. ...this.instancesDetails,
  166. instance,
  167. ]
  168. this.instancesDetails.sort((a, b) => a.instance_name.localeCompare(b.instance_name))
  169. }
  170. handleLoadInstanceDetailsFailed() {
  171. this.instancesDetailsCount -= 1
  172. this.loadingInstancesDetails = this.instancesDetailsCount > 0
  173. }
  174. }
  175. export default alt.createStore(InstanceStore)