UserActions.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 alt from '../alt'
  16. import UserSource from '../sources/UserSource'
  17. import ProjectActions from './ProjectActions'
  18. import ProjectStore from '../stores/ProjectStore'
  19. import NotificationActions from './NotificationActions'
  20. class UserActions {
  21. login(data) {
  22. UserSource.login(data).then(this.loginSuccess, this.loginFailed)
  23. return data
  24. }
  25. loginSuccess() {
  26. this.loginScoped()
  27. return true
  28. }
  29. loginFailed(response) {
  30. return response || true
  31. }
  32. loginScoped(projectId) {
  33. let projectStore = ProjectStore.getState()
  34. if (projectStore.projects && projectStore.projects.length) {
  35. UserSource.loginScoped(projectId || projectStore.projects[0].id)
  36. .then(this.loginScopedSuccess, this.loginScopedFailed)
  37. } else {
  38. ProjectActions.getProjects().promise.then(() => {
  39. UserSource.loginScoped(projectId || ProjectStore.getState().projects[0].id)
  40. .then(this.loginScopedSuccess, this.loginScopedFailed)
  41. })
  42. }
  43. return projectId || true
  44. }
  45. loginScopedSuccess(response) {
  46. this.getUserInfo(response)
  47. NotificationActions.notify('Signed in', 'success')
  48. return response || true
  49. }
  50. loginScopedFailed(response) {
  51. return response || true
  52. }
  53. tokenLogin() {
  54. UserSource.tokenLogin().then(this.tokenLoginSuccess, this.tokenLoginFailed)
  55. return true
  56. }
  57. tokenLoginSuccess(response) {
  58. NotificationActions.notify('Signed in', 'success')
  59. this.getUserInfo(response)
  60. return response || true
  61. }
  62. tokenLoginFailed(response) {
  63. return response || true
  64. }
  65. switchProject(projectId) {
  66. NotificationActions.notify('Switching projects')
  67. UserSource.switchProject().then(
  68. () => { this.switchProjectSuccess(projectId) },
  69. response => { this.switchProjectFailed(response) }
  70. )
  71. return projectId || true
  72. }
  73. switchProjectSuccess(projectId) {
  74. this.loginScoped(projectId)
  75. return projectId || true
  76. }
  77. switchProjectFailed(response) {
  78. this.logout()
  79. return response || true
  80. }
  81. logout() {
  82. UserSource.logout().then(() => { this.logoutSuccess() }, () => { this.logoutFailed() })
  83. return true
  84. }
  85. logoutSuccess() {
  86. return true
  87. }
  88. logoutFailed() {
  89. return true
  90. }
  91. getUserInfo(user) {
  92. UserSource.getUserInfo(user).then(
  93. response => { this.getUserInfoSuccess(response) },
  94. response => { this.getUserInfoFailed(response) }
  95. )
  96. return user || true
  97. }
  98. getUserInfoSuccess(response) {
  99. return response || true
  100. }
  101. getUserInfoFailed(response) {
  102. return response || true
  103. }
  104. }
  105. export default alt.createActions(UserActions)