UserProvider.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { PureComponent } from 'react';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. import { User, Team } from 'app/types';
  4. export interface UserAPI {
  5. changePassword: (changePassword: ChangePasswordFields) => void;
  6. updateUserProfile: (profile: ProfileUpdateFields) => void;
  7. loadUser: () => void;
  8. loadTeams: () => void;
  9. }
  10. interface LoadingStates {
  11. changePassword: boolean;
  12. loadUser: boolean;
  13. loadTeams: boolean;
  14. updateUserProfile: boolean;
  15. }
  16. export interface ChangePasswordFields {
  17. oldPassword: string;
  18. newPassword: string;
  19. confirmNew: string;
  20. }
  21. export interface ProfileUpdateFields {
  22. name: string;
  23. email: string;
  24. login: string;
  25. }
  26. export interface Props {
  27. userId?: number; // passed, will load user on mount
  28. children: (api: UserAPI, states: LoadingStates, teams: Team[], user?: User) => JSX.Element;
  29. }
  30. export interface State {
  31. user?: User;
  32. teams: Team[];
  33. loadingStates: LoadingStates;
  34. }
  35. export class UserProvider extends PureComponent<Props, State> {
  36. state: State = {
  37. teams: [] as Team[],
  38. loadingStates: {
  39. changePassword: false,
  40. loadUser: true,
  41. loadTeams: false,
  42. updateUserProfile: false,
  43. },
  44. };
  45. componentWillMount() {
  46. if (this.props.userId) {
  47. this.loadUser();
  48. }
  49. }
  50. changePassword = async (payload: ChangePasswordFields) => {
  51. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: true } });
  52. await getBackendSrv().put('/api/user/password', payload);
  53. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: false } });
  54. };
  55. loadUser = async () => {
  56. this.setState({
  57. loadingStates: { ...this.state.loadingStates, loadUser: true },
  58. });
  59. const user = await getBackendSrv().get('/api/user');
  60. this.setState({ user, loadingStates: { ...this.state.loadingStates, loadUser: Object.keys(user).length === 0 } });
  61. };
  62. loadTeams = async () => {
  63. this.setState({
  64. loadingStates: { ...this.state.loadingStates, loadTeams: true },
  65. });
  66. const teams = await getBackendSrv().get('/api/user/teams');
  67. this.setState({ teams, loadingStates: { ...this.state.loadingStates, loadTeams: false } });
  68. };
  69. updateUserProfile = async (payload: ProfileUpdateFields) => {
  70. this.setState({ loadingStates: { ...this.state.loadingStates, updateUserProfile: true } });
  71. await getBackendSrv()
  72. .put('/api/user', payload)
  73. .then(() => {
  74. this.loadUser();
  75. })
  76. .catch(e => console.log(e))
  77. .finally(() => {
  78. this.setState({ loadingStates: { ...this.state.loadingStates, updateUserProfile: false } });
  79. });
  80. };
  81. render() {
  82. const { children } = this.props;
  83. const { loadingStates, teams, user } = this.state;
  84. const api = {
  85. changePassword: this.changePassword,
  86. loadUser: this.loadUser,
  87. loadTeams: this.loadTeams,
  88. updateUserProfile: this.updateUserProfile,
  89. };
  90. return <>{children(api, loadingStates, teams, user)}</>;
  91. }
  92. }
  93. export default UserProvider;