InstanceActions.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 InstanceSource from '../sources/InstanceSource'
  16. import InstanceStore from '../stores/InstanceStore'
  17. import { wizardConfig } from '../config'
  18. class InstanceActions {
  19. loadInstances(endpointId) {
  20. InstanceSource.loadInstances(endpointId).then(
  21. instances => { this.loadInstancesSuccess(endpointId, instances) },
  22. response => { this.loadInstancesFailed(endpointId, response) },
  23. )
  24. return endpointId
  25. }
  26. loadInstancesSuccess(endpointId, instances) {
  27. return { endpointId, instances }
  28. }
  29. loadInstancesFailed(endpointId, response) {
  30. return { endpointId, response: response || true }
  31. }
  32. searchInstances(endpointId, searchText) {
  33. InstanceSource.loadInstances(endpointId, searchText).then(
  34. instances => { this.searchInstancesSuccess(instances, searchText) },
  35. response => { this.searchInstancesFailed(response) },
  36. )
  37. return true
  38. }
  39. searchInstancesSuccess(instances, searchText) {
  40. return { instances, searchText }
  41. }
  42. searchInstancesFailed(response) {
  43. return response || true
  44. }
  45. loadNextPage(endpointId, searchText) {
  46. let instanceStore = InstanceStore.getState()
  47. if (instanceStore.cachedInstances.length > wizardConfig.instancesItemsPerPage * instanceStore.currentPage) {
  48. return { fromCache: true }
  49. }
  50. InstanceSource.loadInstances(
  51. endpointId,
  52. searchText,
  53. instanceStore.instances[instanceStore.instances.length - 1].id
  54. ).then(
  55. instances => { this.loadNextPageSuccess(instances) },
  56. response => { this.loadNextPageFailed(response) },
  57. )
  58. return { fromCache: false }
  59. }
  60. loadNextPageFromCache() {
  61. return true
  62. }
  63. loadNextPageSuccess(instances) {
  64. return instances
  65. }
  66. loadNextPageFailed(response) {
  67. return response || true
  68. }
  69. loadPreviousPage() {
  70. return true
  71. }
  72. reloadInstances(endpointId, searchText) {
  73. InstanceSource.loadInstances(endpointId, searchText).then(
  74. instances => { this.reloadInstancesSuccess(instances, searchText) },
  75. response => { this.reloadInstancesFailed(response) },
  76. )
  77. return true
  78. }
  79. reloadInstancesSuccess(instances, searchText) {
  80. return { instances, searchText }
  81. }
  82. reloadInstancesFailed(response) {
  83. return response || true
  84. }
  85. loadInstancesDetails(endpointId, instances) {
  86. instances.forEach(instance => {
  87. InstanceSource.loadInstanceDetails(endpointId, instance.instance_name).then(
  88. instance => { this.loadInstanceDetailsSuccess(instance) },
  89. response => { this.loadInstanceDetailsFailed(response) },
  90. )
  91. })
  92. return { count: instances.length }
  93. }
  94. loadInstanceDetails(endpointId, instanceName) {
  95. InstanceSource.loadInstanceDetails(endpointId, instanceName).then(
  96. instance => { this.loadInstanceDetailsSuccess(instance) },
  97. response => { this.loadInstanceDetailsFailed(response) },
  98. )
  99. return true
  100. }
  101. loadInstanceDetailsSuccess(instance) {
  102. return instance
  103. }
  104. loadInstanceDetailsFailed(response) {
  105. return response || true
  106. }
  107. }
  108. export default alt.createActions(InstanceActions)