UserProvider.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, { PureComponent } from 'react';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. import { User, Team } from 'app/types';
  4. import { config } from 'app/core/config';
  5. export interface UserAPI {
  6. changePassword: (changePassword: ChangePasswordFields) => void;
  7. updateUserProfile: (profile: ProfileUpdateFields) => void;
  8. loadUser: () => void;
  9. loadTeams: () => void;
  10. loadOrgs: () => void;
  11. setUserOrg: (org: UserOrg) => void;
  12. }
  13. interface LoadingStates {
  14. changePassword: boolean;
  15. loadUser: boolean;
  16. loadTeams: boolean;
  17. loadOrgs: boolean;
  18. updateUserProfile: boolean;
  19. updateUserOrg: boolean;
  20. }
  21. export interface ChangePasswordFields {
  22. oldPassword: string;
  23. newPassword: string;
  24. confirmNew: string;
  25. }
  26. export interface ProfileUpdateFields {
  27. name: string;
  28. email: string;
  29. login: string;
  30. }
  31. export interface UserOrg {
  32. orgId: number;
  33. name: string;
  34. role: string;
  35. }
  36. export interface Props {
  37. userId?: number; // passed, will load user on mount
  38. children: (api: UserAPI, states: LoadingStates, teams: Team[], orgs: UserOrg[], user?: User) => JSX.Element;
  39. }
  40. export interface State {
  41. user?: User;
  42. teams: Team[];
  43. orgs: UserOrg[];
  44. loadingStates: LoadingStates;
  45. }
  46. export class UserProvider extends PureComponent<Props, State> {
  47. state: State = {
  48. teams: [] as Team[],
  49. orgs: [] as UserOrg[],
  50. loadingStates: {
  51. changePassword: false,
  52. loadUser: true,
  53. loadTeams: false,
  54. loadOrgs: false,
  55. updateUserProfile: false,
  56. updateUserOrg: false,
  57. },
  58. };
  59. componentWillMount() {
  60. if (this.props.userId) {
  61. this.loadUser();
  62. }
  63. }
  64. changePassword = async (payload: ChangePasswordFields) => {
  65. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: true } });
  66. await getBackendSrv().put('/api/user/password', payload);
  67. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: false } });
  68. };
  69. loadUser = async () => {
  70. this.setState({
  71. loadingStates: { ...this.state.loadingStates, loadUser: true },
  72. });
  73. const user = await getBackendSrv().get('/api/user');
  74. this.setState({ user, loadingStates: { ...this.state.loadingStates, loadUser: Object.keys(user).length === 0 } });
  75. };
  76. loadTeams = async () => {
  77. this.setState({
  78. loadingStates: { ...this.state.loadingStates, loadTeams: true },
  79. });
  80. const teams = await getBackendSrv().get('/api/user/teams');
  81. this.setState({ teams, loadingStates: { ...this.state.loadingStates, loadTeams: false } });
  82. };
  83. loadOrgs = async () => {
  84. this.setState({
  85. loadingStates: { ...this.state.loadingStates, loadOrgs: true },
  86. });
  87. const orgs = await getBackendSrv().get('/api/user/orgs');
  88. this.setState({ orgs, loadingStates: { ...this.state.loadingStates, loadOrgs: false } });
  89. };
  90. setUserOrg = async (org: UserOrg) => {
  91. this.setState({
  92. loadingStates: { ...this.state.loadingStates, updateUserOrg: true },
  93. });
  94. await getBackendSrv()
  95. .post('/api/user/using/' + org.orgId, {})
  96. .then(() => {
  97. window.location.href = config.appSubUrl + '/profile';
  98. })
  99. .finally(() => {
  100. this.setState({ loadingStates: { ...this.state.loadingStates, updateUserOrg: false } });
  101. });
  102. };
  103. updateUserProfile = async (payload: ProfileUpdateFields) => {
  104. this.setState({ loadingStates: { ...this.state.loadingStates, updateUserProfile: true } });
  105. await getBackendSrv()
  106. .put('/api/user', payload)
  107. .then(() => {
  108. this.loadUser();
  109. })
  110. .catch(e => console.log(e))
  111. .finally(() => {
  112. this.setState({ loadingStates: { ...this.state.loadingStates, updateUserProfile: false } });
  113. });
  114. };
  115. render() {
  116. const { children } = this.props;
  117. const { loadingStates, teams, orgs, user } = this.state;
  118. const api = {
  119. changePassword: this.changePassword,
  120. loadUser: this.loadUser,
  121. loadTeams: this.loadTeams,
  122. loadOrgs: this.loadOrgs,
  123. updateUserProfile: this.updateUserProfile,
  124. setUserOrg: this.setUserOrg,
  125. };
  126. return <>{children(api, loadingStates, teams, orgs, user)}</>;
  127. }
  128. }
  129. export default UserProvider;