NotificationsStore.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 Reflux from 'reflux';
  15. import UserActions from '../../actions/UserActions';
  16. import NotificationActions from '../../actions/NotificationActions';
  17. import ConnectionsActions from '../../actions/ConnectionsActions';
  18. import MigrationActions from '../../actions/MigrationActions';
  19. import Location from '../../core/Location';
  20. class NotificationsStore extends Reflux.Store
  21. {
  22. constructor() {
  23. super()
  24. this.listenables = [UserActions, NotificationActions, ConnectionsActions, MigrationActions]
  25. this.state = {
  26. notifications: [],
  27. allNotifications: []
  28. }
  29. }
  30. onLoginScopeSuccess() {
  31. let notifications = [{
  32. message: "Signed in",
  33. type: 'success'
  34. }]
  35. this.setState({ notifications: notifications })
  36. }
  37. onTokenLoginFailed() {
  38. let notifications = [
  39. {
  40. message: "Session expired, please sign in",
  41. type: 'error'
  42. }
  43. ]
  44. this.setState({ notifications: notifications })
  45. }
  46. onLogout() {
  47. let notifications = [
  48. {
  49. message: "You have been signed out",
  50. type: 'info'
  51. }
  52. ]
  53. this.setState({ notifications: notifications })
  54. }
  55. onSaveEndpoint() {
  56. let notifications = [{
  57. title: "New Connection",
  58. message: "Connection added successfully",
  59. type: 'success',
  60. keep: true
  61. }]
  62. this.setState({ notifications: notifications })
  63. }
  64. onAddMigrationCompleted(response) {
  65. let notifications = null
  66. if (response.data.migration) {
  67. notifications = [{
  68. title: "New Migration",
  69. message: "Migration created successfully",
  70. type: 'success',
  71. keep: true,
  72. action: {
  73. label: "View Migration Status",
  74. callback: () => {
  75. Location.push("/migration/tasks/" + response.data.migration.id + "/")
  76. }
  77. }
  78. }]
  79. } else {
  80. notifications = [{
  81. title: "New Replica",
  82. message: "Replica created successfully",
  83. type: 'success',
  84. keep: true,
  85. action: {
  86. label: "View Replica Status",
  87. callback: () => {
  88. Location.push("/replica/executions/" + response.data.replica.id + "/")
  89. }
  90. }
  91. }]
  92. }
  93. this.setState({ notifications: notifications })
  94. }
  95. onDeleteConnection() {
  96. let notifications = [{
  97. message: "Connection deleted successfully",
  98. type: 'success'
  99. }]
  100. this.setState({ notifications: notifications })
  101. }
  102. onDeleteMigrationCompleted() {
  103. let notifications = [{
  104. message: "Migration deleted successfully",
  105. type: 'success'
  106. }]
  107. this.setState({ notifications: notifications })
  108. }
  109. onDeleteMigrationFailed() {
  110. let notifications = [{
  111. message: "Could not delete migration",
  112. type: 'error'
  113. }]
  114. this.setState({ notifications: notifications })
  115. }
  116. onDeleteReplicaCompleted() {
  117. let notifications = [{
  118. message: "Replica deleted successfully",
  119. type: 'success'
  120. }]
  121. this.setState({ notifications: notifications })
  122. }
  123. onDeleteReplicaFailed() {
  124. let notifications = [{
  125. message: "Could not delete replica",
  126. type: 'error'
  127. }]
  128. this.setState({ notifications: notifications })
  129. }
  130. onLoginFailed() {
  131. let notifications = [{
  132. message: "Login failed",
  133. type: 'error'
  134. }]
  135. this.setState({ notifications: notifications })
  136. }
  137. onExecuteReplicaCompleted() {
  138. let notifications = [{
  139. message: "Executing replica",
  140. type: 'info'
  141. }]
  142. this.setState({ notifications: notifications })
  143. }
  144. onDeleteReplicaExecutionCompleted() {
  145. let notifications = [{
  146. message: "Execution deleted",
  147. type: 'info'
  148. }]
  149. this.setState({ notifications: notifications })
  150. }
  151. onCancelMigrationCompleted(migration) {
  152. let message = "Canceled"
  153. if (migration.type == "migration") {
  154. message = "Migration canceled successful"
  155. }
  156. let notifications = [{
  157. message: message,
  158. type: 'success'
  159. }]
  160. this.setState({ notifications: notifications })
  161. }
  162. onCreateMigrationFromReplicaCompleted(response) {
  163. let notifications = [{
  164. title: "New Migration",
  165. message: "Migration successfully created from replica.",
  166. type: 'success',
  167. hideDelay: 10000,
  168. keep: true,
  169. action: {
  170. label: "View Migration Status",
  171. callback: () => {
  172. Location.push("/migration/tasks/" + response.data.migration.id + "/")
  173. }
  174. }
  175. }]
  176. this.setState({ notifications: notifications })
  177. }
  178. onLoadInstancesFailed() {
  179. let notifications = [{
  180. message: "Could not load instances.",
  181. type: 'error'
  182. }]
  183. this.setState({ notifications: notifications })
  184. }
  185. onNewEndpoint() {
  186. let notifications = [{
  187. message: "Saving endpoint...",
  188. type: 'info'
  189. }]
  190. this.setState({ notifications: notifications })
  191. }
  192. onEditEndpoint() {
  193. let notifications = [{
  194. message: "Saving endpoint...",
  195. type: 'info'
  196. }]
  197. this.setState({ notifications: notifications })
  198. }
  199. onEditEndpointFailed() {
  200. let notifications = [{
  201. message: "Could not save endpoint",
  202. type: 'error'
  203. }]
  204. this.setState({ notifications: notifications })
  205. }
  206. onSaveEditEndpointSuccess() {
  207. let notifications = [{
  208. message: "Endpoint saved!",
  209. type: 'success'
  210. }]
  211. this.setState({ notifications: notifications })
  212. }
  213. onSaveEditEndpointFailed() {
  214. let notifications = [{
  215. message: "Could not save endpoint",
  216. type: 'error'
  217. }]
  218. this.setState({ notifications: notifications })
  219. }
  220. onNotify(message, type = "info", title = null) {
  221. this.setState({
  222. notifications: [
  223. {
  224. message: message,
  225. type: type,
  226. title: title
  227. }
  228. ]
  229. })
  230. }
  231. onMarkAsRead() {
  232. let allNotifications = this.state.allNotifications
  233. allNotifications.forEach(notification => {
  234. notification.unread = false
  235. })
  236. this.setState({ allNotifications: allNotifications })
  237. }
  238. onKeepNotification(notification) {
  239. let allNotifications = this.state.allNotifications
  240. allNotifications.unshift(notification)
  241. this.setState({ allNotifications: allNotifications })
  242. }
  243. onRemoveNotification() {
  244. this.setState({ notifications: [] })
  245. }
  246. onSwitchProject() {
  247. let notifications = [{
  248. message: "Switching project",
  249. type: 'info'
  250. }]
  251. this.setState({ notifications: notifications })
  252. }
  253. }
  254. export default NotificationsStore;