WizardStore.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // @flow
  15. import { observable, action } from 'mobx'
  16. import type { WizardData, WizardPage } from '../types/WizardData'
  17. import type { MainItem } from '../types/MainItem'
  18. import type { Instance } from '../types/Instance'
  19. import type { Field } from '../types/Field'
  20. import type { NetworkMap } from '../types/Network'
  21. import type { Schedule } from '../types/Schedule'
  22. import { wizardConfig } from '../config'
  23. import Source from '../sources/WizardSource'
  24. class WizardStore {
  25. @observable data: WizardData = { schedules: [] }
  26. @observable currentPage: WizardPage = wizardConfig.pages[0]
  27. @observable createdItem: ?MainItem = null
  28. @observable creatingItem: boolean = false
  29. @observable createdItems: ?MainItem[] = null
  30. @observable creatingItems: boolean = false
  31. @action updateData(data: WizardData) {
  32. this.data = { ...this.data, ...data }
  33. }
  34. @action toggleInstanceSelection(instance: Instance) {
  35. if (!this.data.selectedInstances) {
  36. this.data.selectedInstances = [instance]
  37. return
  38. }
  39. if (this.data.selectedInstances.find(i => i.id === instance.id)) {
  40. // $FlowIssue
  41. this.data.selectedInstances = this.data.selectedInstances.filter(i => i.id !== instance.id)
  42. } else {
  43. // $FlowIssue
  44. this.data.selectedInstances = [...this.data.selectedInstances, instance]
  45. }
  46. }
  47. @action clearData() {
  48. this.data = {}
  49. this.currentPage = wizardConfig.pages[0]
  50. }
  51. @action setCurrentPage(page: WizardPage) {
  52. this.currentPage = page
  53. }
  54. @action updateOptions(data: { field: Field, value: any }) {
  55. this.data.options = {
  56. ...this.data.options,
  57. }
  58. if (data.field.type === 'array') {
  59. let oldValues: string[] = this.data.options[data.field.name] || []
  60. if (oldValues.find(v => v === data.value)) {
  61. // $FlowIssue
  62. this.data.options[data.field.name] = oldValues.filter(v => v !== data.value)
  63. } else {
  64. // $FlowIssue
  65. this.data.options[data.field.name] = [...oldValues, data.value]
  66. }
  67. } else {
  68. this.data.options[data.field.name] = data.value
  69. }
  70. }
  71. @action updateNetworks(network: NetworkMap) {
  72. if (!this.data.networks) {
  73. this.data.networks = []
  74. }
  75. this.data.networks = this.data.networks.filter(n => n.sourceNic.network_name !== network.sourceNic.network_name)
  76. this.data.networks.push(network)
  77. }
  78. @action addSchedule(schedule: Schedule) {
  79. if (!this.data.schedules) {
  80. this.data.schedules = []
  81. }
  82. this.data.schedules.push({ id: new Date().getTime().toString(), schedule: schedule.schedule })
  83. }
  84. @action updateSchedule(scheduleId: string, data: Schedule) {
  85. if (!this.data.schedules) {
  86. return
  87. }
  88. this.data.schedules = this.data.schedules.map(schedule => {
  89. if (schedule.id !== scheduleId) {
  90. return schedule
  91. }
  92. if (data.schedule) {
  93. schedule.schedule = {
  94. ...schedule.schedule,
  95. ...data.schedule,
  96. }
  97. } else {
  98. schedule = {
  99. ...schedule,
  100. ...data,
  101. }
  102. }
  103. return schedule
  104. })
  105. }
  106. @action removeSchedule(scheduleId: string) {
  107. if (!this.data.schedules) {
  108. return
  109. }
  110. this.data.schedules = this.data.schedules.filter(s => s.id !== scheduleId)
  111. }
  112. @action create(type: string, data: WizardData): Promise<void> {
  113. this.creatingItem = true
  114. return Source.create(type, data).then((item: MainItem) => {
  115. this.createdItem = item
  116. this.creatingItem = false
  117. }).catch(() => {
  118. this.creatingItem = false
  119. })
  120. }
  121. @action createMultiple(type: string, data: WizardData): Promise<void> {
  122. this.creatingItems = true
  123. return Source.createMultiple(type, data).then((items: MainItem[]) => {
  124. this.createdItems = items
  125. this.creatingItems = false
  126. }).catch(() => {
  127. this.creatingItems = false
  128. })
  129. }
  130. @action setPermalink(data: WizardData) {
  131. Source.setPermalink(data)
  132. }
  133. @action getDataFromPermalink() {
  134. let data = Source.getDataFromPermalink()
  135. if (!data) {
  136. return
  137. }
  138. this.data = {
  139. ...this.data,
  140. ...data,
  141. }
  142. }
  143. }
  144. export default new WizardStore()