UserProvider.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React, { PureComponent } from 'react';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. export interface UserAPI {
  4. changePassword: (ChangePassword: ChangePasswordFields) => void;
  5. }
  6. interface LoadingStates {
  7. changePassword: boolean;
  8. }
  9. export interface ChangePasswordFields {
  10. oldPassword: string;
  11. newPassword: string;
  12. confirmNew: string;
  13. }
  14. export interface Props {
  15. children: (api: UserAPI, states: LoadingStates) => JSX.Element;
  16. }
  17. export interface State {
  18. loadingStates: LoadingStates;
  19. }
  20. export class UserProvider extends PureComponent<Props, State> {
  21. state: State = {
  22. loadingStates: {
  23. changePassword: false,
  24. },
  25. };
  26. changePassword = async (payload: ChangePasswordFields) => {
  27. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: true } });
  28. await getBackendSrv().put('/api/user/password', payload);
  29. this.setState({ loadingStates: { ...this.state.loadingStates, changePassword: false } });
  30. };
  31. render() {
  32. const { children } = this.props;
  33. const { loadingStates } = this.state;
  34. const api = {
  35. changePassword: this.changePassword,
  36. };
  37. return <>{children(api, loadingStates)}</>;
  38. }
  39. }
  40. export default UserProvider;