LaunchpadController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* global io */
  2. /* eslint-disable
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import App from '../../../../../../frontend/js/base'
  15. export default App.controller(
  16. 'LaunchpadController',
  17. function ($scope, $http, $timeout) {
  18. $scope.adminUserExists = window.data.adminUserExists
  19. $scope.ideJsPath = window.data.ideJsPath
  20. $scope.authMethod = window.data.authMethod
  21. $scope.createAdminSuccess = null
  22. $scope.createAdminError = null
  23. $scope.statusChecks = {
  24. ideJs: { status: 'inflight', error: null },
  25. websocket: { status: 'inflight', error: null },
  26. healthCheck: { status: 'inflight', error: null },
  27. }
  28. $scope.testEmail = {
  29. emailAddress: '',
  30. inflight: false,
  31. status: null, // | 'ok' | 'success'
  32. }
  33. $scope.shouldShowAdminForm = () => !$scope.adminUserExists
  34. $scope.onCreateAdminSuccess = function (response) {
  35. const { status } = response
  36. if (status >= 200 && status < 300) {
  37. return ($scope.createAdminSuccess = true)
  38. }
  39. }
  40. $scope.onCreateAdminError = () => ($scope.createAdminError = true)
  41. $scope.sendTestEmail = function () {
  42. $scope.testEmail.inflight = true
  43. $scope.testEmail.status = null
  44. return $http
  45. .post('/launchpad/send_test_email', {
  46. email: $scope.testEmail.emailAddress,
  47. _csrf: window.csrfToken,
  48. })
  49. .then(function (response) {
  50. const { status } = response
  51. $scope.testEmail.inflight = false
  52. if (status >= 200 && status < 300) {
  53. return ($scope.testEmail.status = 'ok')
  54. }
  55. })
  56. .catch(function () {
  57. $scope.testEmail.inflight = false
  58. return ($scope.testEmail.status = 'error')
  59. })
  60. }
  61. $scope.tryFetchIdeJs = function () {
  62. $scope.statusChecks.ideJs.status = 'inflight'
  63. return $timeout(
  64. () =>
  65. $http
  66. .get($scope.ideJsPath)
  67. .then(function (response) {
  68. const { status } = response
  69. if (status >= 200 && status < 300) {
  70. return ($scope.statusChecks.ideJs.status = 'ok')
  71. }
  72. })
  73. .catch(function (response) {
  74. const { status } = response
  75. $scope.statusChecks.ideJs.status = 'error'
  76. return ($scope.statusChecks.ideJs.error = new Error(
  77. `Http status: ${status}`
  78. ))
  79. }),
  80. 1000
  81. )
  82. }
  83. $scope.tryOpenWebSocket = function () {
  84. $scope.statusChecks.websocket.status = 'inflight'
  85. return $timeout(function () {
  86. if (typeof io === 'undefined' || io === null) {
  87. $scope.statusChecks.websocket.status = 'error'
  88. $scope.statusChecks.websocket.error = 'socket.io not loaded'
  89. return
  90. }
  91. const socket = io.connect(null, {
  92. reconnect: false,
  93. 'connect timeout': 30 * 1000,
  94. 'force new connection': true,
  95. })
  96. socket.on('connectionAccepted', function () {
  97. $scope.statusChecks.websocket.status = 'ok'
  98. return $scope.$apply(function () {})
  99. })
  100. socket.on('connectionRejected', function (err) {
  101. $scope.statusChecks.websocket.status = 'error'
  102. $scope.statusChecks.websocket.error = err
  103. return $scope.$apply(function () {})
  104. })
  105. return socket.on('connect_failed', function (err) {
  106. $scope.statusChecks.websocket.status = 'error'
  107. $scope.statusChecks.websocket.error = err
  108. return $scope.$apply(function () {})
  109. })
  110. }, 1000)
  111. }
  112. $scope.tryHealthCheck = function () {
  113. $scope.statusChecks.healthCheck.status = 'inflight'
  114. return $http
  115. .get('/health_check')
  116. .then(function (response) {
  117. const { status } = response
  118. if (status >= 200 && status < 300) {
  119. return ($scope.statusChecks.healthCheck.status = 'ok')
  120. }
  121. })
  122. .catch(function (response) {
  123. const { status } = response
  124. $scope.statusChecks.healthCheck.status = 'error'
  125. return ($scope.statusChecks.healthCheck.error = new Error(
  126. `Http status: ${status}`
  127. ))
  128. })
  129. }
  130. $scope.runStatusChecks = function () {
  131. $timeout(() => $scope.tryFetchIdeJs(), 1000)
  132. return $timeout(() => $scope.tryOpenWebSocket(), 2000)
  133. }
  134. // kick off the status checks on load
  135. if ($scope.adminUserExists) {
  136. return $scope.runStatusChecks()
  137. }
  138. }
  139. )