user-details.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-undef,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. define(['base', 'libs/algolia-2.5.2'], function(App, algolia) {
  16. App.factory('Institutions', () =>
  17. new AlgoliaSearch(
  18. window.algolia.institutions.app_id,
  19. window.algolia.institutions.api_key
  20. ).initIndex('institutions')
  21. )
  22. App.controller('UserProfileController', function($scope, $modal, $http) {
  23. $scope.institutions = []
  24. $http.get('/user/personal_info').then(function(response) {
  25. const { data } = response
  26. return ($scope.userInfoForm = {
  27. first_name: data.first_name || '',
  28. last_name: data.last_name || '',
  29. role: data.role || '',
  30. institution: data.institution || '',
  31. _csrf: window.csrfToken
  32. })
  33. })
  34. $scope.showForm = function() {
  35. GrooveWidget.toggle()
  36. return ($scope.formVisable = true)
  37. }
  38. $scope.getPercentComplete = function() {
  39. const results = _.filter(
  40. $scope.userInfoForm,
  41. value =>
  42. value == null || (value != null ? value.length : undefined) !== 0
  43. )
  44. return results.length * 20
  45. }
  46. $scope.$watch(
  47. 'userInfoForm',
  48. function(value) {
  49. if (value != null) {
  50. return ($scope.percentComplete = $scope.getPercentComplete())
  51. }
  52. },
  53. true
  54. )
  55. return ($scope.openUserProfileModal = () =>
  56. $modal.open({
  57. templateUrl: 'userProfileModalTemplate',
  58. controller: 'UserProfileModalController',
  59. scope: $scope
  60. }))
  61. })
  62. return App.controller('UserProfileModalController', function(
  63. $scope,
  64. $modalInstance,
  65. $http,
  66. Institutions
  67. ) {
  68. $scope.roles = [
  69. 'Student',
  70. 'Post-graduate student',
  71. 'Post-doctoral researcher',
  72. 'Lecturer',
  73. 'Professor'
  74. ]
  75. $modalInstance.result.finally(() => sendUpdate())
  76. var sendUpdate = function() {
  77. const request = $http.post('/user/settings', $scope.userInfoForm)
  78. request.then(function() {})
  79. return request.catch(() => console.log('the request failed'))
  80. }
  81. $scope.updateInstitutionsList = function(inputVal) {
  82. const query = $scope.userInfoForm.institution
  83. if ((query != null ? query.length : undefined) <= 3) {
  84. return // saves us algolia searches
  85. }
  86. return Institutions.search(
  87. $scope.userInfoForm.institution,
  88. (err, response) =>
  89. ($scope.institutions = _.map(
  90. response.hits,
  91. institution => `${institution.name} (${institution.domain})`
  92. ))
  93. )
  94. }
  95. return ($scope.done = () => $modalInstance.close())
  96. })
  97. })