WizardStore.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 WizardActions from '../actions/WizardActions'
  16. import { wizardConfig } from '../config'
  17. class WizardStore {
  18. constructor() {
  19. this.data = {}
  20. this.currentPage = wizardConfig.pages[0]
  21. this.createdItem = null
  22. this.creatingItem = false
  23. this.createdItems = null
  24. this.creatingItems = false
  25. this.bindListeners({
  26. handleUpdateData: WizardActions.UPDATE_DATA,
  27. handleClearData: WizardActions.CLEAR_DATA,
  28. handleSetCurrentPage: WizardActions.SET_CURRENT_PAGE,
  29. handleToggleInstanceSelection: WizardActions.TOGGLE_INSTANCE_SELECTION,
  30. handleUpdateOptions: WizardActions.UPDATE_OPTIONS,
  31. handleUpdateNetworks: WizardActions.UPDATE_NETWORKS,
  32. handleAddSchedule: WizardActions.ADD_SCHEDULE,
  33. handleUpdateSchedule: WizardActions.UPDATE_SCHEDULE,
  34. handleRemoveSchedule: WizardActions.REMOVE_SCHEDULE,
  35. handleCreate: WizardActions.CREATE,
  36. handleCreateSuccess: WizardActions.CREATE_SUCCESS,
  37. handleCreateFailed: WizardActions.CREATE_FAILED,
  38. handleCreateMultiple: WizardActions.CREATE_MULTIPLE,
  39. handleCreateMultipleSuccess: WizardActions.CREATE_MULTIPLE_SUCCESS,
  40. handleCreateMultipleFailed: WizardActions.CREATE_MULTIPLE_FAILED,
  41. handleGetDataFromPermalink: WizardActions.GET_DATA_FROM_PERMALINK,
  42. })
  43. }
  44. handleUpdateData(data) {
  45. this.data = {
  46. ...this.data,
  47. ...data,
  48. }
  49. }
  50. handleClearData() {
  51. this.data = {}
  52. this.currentPage = wizardConfig.pages[0]
  53. }
  54. handleSetCurrentPage(page) {
  55. this.currentPage = page
  56. }
  57. handleToggleInstanceSelection(instance) {
  58. if (!this.data.selectedInstances) {
  59. this.data.selectedInstances = [instance]
  60. return
  61. }
  62. if (this.data.selectedInstances.find(i => i.id === instance.id)) {
  63. this.data.selectedInstances = this.data.selectedInstances.filter(i => i.id !== instance.id)
  64. } else {
  65. this.data.selectedInstances = [...this.data.selectedInstances, instance]
  66. }
  67. }
  68. handleUpdateOptions({ field, value }) {
  69. this.data.options = {
  70. ...this.data.options,
  71. }
  72. this.data.options[field.name] = value
  73. }
  74. handleUpdateNetworks({ sourceNic, targetNetwork }) {
  75. if (!this.data.networks) {
  76. this.data.networks = []
  77. }
  78. this.data.networks = this.data.networks.filter(n => n.sourceNic.id !== sourceNic.id)
  79. this.data.networks.push({ sourceNic, targetNetwork })
  80. }
  81. handleAddSchedule(schedule) {
  82. if (!this.data.schedules) {
  83. this.data.schedules = []
  84. }
  85. this.data.schedules.push({ id: new Date().getTime(), schedule: schedule.schedule })
  86. }
  87. handleUpdateSchedule({ scheduleId, data }) {
  88. let schedule = this.data.schedules.find(s => s.id === scheduleId)
  89. if (data.schedule) {
  90. schedule.schedule = {
  91. ...schedule.schedule,
  92. ...data.schedule,
  93. }
  94. } else {
  95. schedule = {
  96. ...schedule,
  97. ...data,
  98. }
  99. }
  100. this.data.schedules = this.data.schedules.filter(s => s.id !== scheduleId)
  101. this.data.schedules.push(schedule)
  102. this.data.schedules.sort((a, b) => a.id > b.id)
  103. }
  104. handleRemoveSchedule(scheduleId) {
  105. this.data.schedules = this.data.schedules.filter(s => s.id !== scheduleId)
  106. }
  107. handleCreate() {
  108. this.creatingItem = true
  109. }
  110. handleCreateSuccess(item) {
  111. this.createdItem = item
  112. this.creatingItem = false
  113. }
  114. handleCreateFailed() {
  115. this.creatingItem = false
  116. }
  117. handleCreateMultiple() {
  118. this.creatingItems = true
  119. }
  120. handleCreateMultipleSuccess(items) {
  121. this.createdItems = items
  122. this.creatingItems = false
  123. }
  124. handleCreateMultipleFailed() {
  125. this.creatingItems = false
  126. }
  127. handleGetDataFromPermalink(data) {
  128. if (data === true) {
  129. return
  130. }
  131. this.data = {
  132. ...this.data,
  133. ...data,
  134. }
  135. }
  136. }
  137. export default alt.createStore(WizardStore)