profile_ctrl.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import {coreModule} from 'app/core/core';
  4. import _ from 'lodash';
  5. export class ProfileCtrl {
  6. user: any;
  7. old_theme: any;
  8. orgs: any = [];
  9. userForm: any;
  10. showOrgsList: boolean = false;
  11. /** @ngInject **/
  12. constructor(private backendSrv, private contextSrv, private $location) {
  13. this.getUser();
  14. this.getUserOrgs();
  15. }
  16. getUser() {
  17. this.backendSrv.get('/api/user').then(user => {
  18. this.user = user;
  19. this.user.theme = user.theme || 'dark';
  20. });
  21. }
  22. getUserOrgs() {
  23. this.backendSrv.get('/api/user/orgs').then(orgs => {
  24. this.orgs = orgs;
  25. this.showOrgsList = orgs.length > 1;
  26. });
  27. }
  28. setUsingOrg(org) {
  29. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  30. window.location.href = config.appSubUrl + '/profile';
  31. });
  32. }
  33. update() {
  34. if (!this.userForm.$valid) { return; }
  35. this.backendSrv.put('/api/user/', this.user).then(() => {
  36. this.contextSrv.user.name = this.user.name || this.user.login;
  37. if (this.old_theme !== this.user.theme) {
  38. window.location.href = config.appSubUrl + this.$location.path();
  39. }
  40. });
  41. }
  42. }
  43. coreModule.controller('ProfileCtrl', ProfileCtrl);