InsightsConfig.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { PureComponent } from 'react';
  2. import { FormLabel, Button, Input } from '@grafana/ui';
  3. export interface Props {
  4. datasourceConfig: any;
  5. onDatasourceUpdate: (config: any) => void;
  6. }
  7. export interface State {
  8. config: any;
  9. }
  10. export class InsightsConfig extends PureComponent<Props, State> {
  11. constructor(props: Props) {
  12. super(props);
  13. const { datasourceConfig } = this.props;
  14. this.state = {
  15. config: datasourceConfig,
  16. };
  17. }
  18. static getDerivedStateFromProps(props: Props, state: State) {
  19. return {
  20. ...state,
  21. config: props.datasourceConfig,
  22. };
  23. }
  24. onAppInsightsAppIdChange = (appInsightsAppId: string) => {
  25. this.props.onDatasourceUpdate({
  26. ...this.state.config,
  27. editorJsonData: {
  28. ...this.state.config.editorJsonData,
  29. appInsightsAppId,
  30. },
  31. });
  32. };
  33. onAppInsightsApiKeyChange = (appInsightsApiKey: string) => {
  34. this.props.onDatasourceUpdate({
  35. ...this.state.config,
  36. editorSecureJsonData: {
  37. ...this.state.config.editorSecureJsonData,
  38. appInsightsApiKey,
  39. },
  40. });
  41. };
  42. onAppInsightsResetApiKey = () => {
  43. this.props.onDatasourceUpdate({
  44. ...this.state.config,
  45. version: this.state.config.version + 1,
  46. secureJsonFields: {
  47. ...this.state.config.secureJsonFields,
  48. appInsightsApiKey: false,
  49. },
  50. });
  51. };
  52. render() {
  53. const { config } = this.state;
  54. return (
  55. <>
  56. <h3 className="page-heading">Application Insights Details</h3>
  57. <div className="gf-form-group">
  58. {config.secureJsonFields.appInsightsApiKey ? (
  59. <div className="gf-form-inline">
  60. <div className="gf-form">
  61. <FormLabel className="width-12">API Key</FormLabel>
  62. <Input className="width-25" placeholder="configured" disabled={true} />
  63. </div>
  64. <div className="gf-form">
  65. <div className="max-width-30 gf-form-inline">
  66. <Button variant="secondary" type="button" onClick={this.onAppInsightsResetApiKey}>
  67. reset
  68. </Button>
  69. </div>
  70. </div>
  71. </div>
  72. ) : (
  73. <div className="gf-form-inline">
  74. <div className="gf-form">
  75. <FormLabel className="width-12">API Key</FormLabel>
  76. <div className="width-15">
  77. <Input
  78. className="width-30"
  79. placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  80. value={config.editorSecureJsonData.appInsightsApiKey}
  81. onChange={event => this.onAppInsightsApiKeyChange(event.target.value)}
  82. />
  83. </div>
  84. </div>
  85. </div>
  86. )}
  87. <div className="gf-form-inline">
  88. <div className="gf-form">
  89. <FormLabel className="width-12">Application ID</FormLabel>
  90. <div className="width-15">
  91. <Input
  92. className="width-30"
  93. value={config.editorJsonData.appInsightsAppId}
  94. onChange={event => this.onAppInsightsAppIdChange(event.target.value)}
  95. />
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </>
  101. );
  102. }
  103. }
  104. export default InsightsConfig;