AppConfigWrapper.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import cloneDeep from 'lodash/cloneDeep';
  4. import extend from 'lodash/extend';
  5. import { PluginMeta, AppPlugin, Button } from '@grafana/ui';
  6. import { deprecationWarning } from '@grafana/data';
  7. import { AngularComponent, getAngularLoader } from '@grafana/runtime';
  8. import { getBackendSrv } from 'app/core/services/backend_srv';
  9. import { css } from 'emotion';
  10. interface Props {
  11. app: AppPlugin;
  12. }
  13. interface State {
  14. angularCtrl: AngularComponent;
  15. refresh: number;
  16. }
  17. export class AppConfigCtrlWrapper extends PureComponent<Props, State> {
  18. element: HTMLElement; // for angular ctrl
  19. // Needed for angular scope
  20. preUpdateHook = () => Promise.resolve();
  21. postUpdateHook = () => Promise.resolve();
  22. model: PluginMeta;
  23. constructor(props: Props) {
  24. super(props);
  25. this.state = {
  26. angularCtrl: null,
  27. refresh: 0,
  28. };
  29. }
  30. componentDidMount() {
  31. // Force a reload after the first mount -- is there a better way to do this?
  32. setTimeout(() => {
  33. this.setState({ refresh: this.state.refresh + 1 });
  34. }, 5);
  35. }
  36. componentDidUpdate(prevProps: Props) {
  37. if (!this.element || this.state.angularCtrl) {
  38. return;
  39. }
  40. // Set a copy of the meta
  41. this.model = cloneDeep(this.props.app.meta);
  42. const loader = getAngularLoader();
  43. const template = '<plugin-component type="app-config-ctrl"></plugin-component>';
  44. const scopeProps = { ctrl: this };
  45. const angularCtrl = loader.load(this.element, scopeProps, template);
  46. this.setState({ angularCtrl });
  47. }
  48. render() {
  49. const model = this.model;
  50. const withRightMargin = css({ marginRight: '8px' });
  51. return (
  52. <div>
  53. <div ref={element => (this.element = element)} />
  54. <br />
  55. <br />
  56. {model && (
  57. <div className="gf-form">
  58. {!model.enabled && (
  59. <Button variant="primary" onClick={this.enable} className={withRightMargin}>
  60. Enable
  61. </Button>
  62. )}
  63. {model.enabled && (
  64. <Button variant="primary" onClick={this.update} className={withRightMargin}>
  65. Update
  66. </Button>
  67. )}
  68. {model.enabled && (
  69. <Button variant="danger" onClick={this.disable} className={withRightMargin}>
  70. Disable
  71. </Button>
  72. )}
  73. </div>
  74. )}
  75. </div>
  76. );
  77. }
  78. //-----------------------------------------------------------
  79. // Copied from plugin_edit_ctrl
  80. //-----------------------------------------------------------
  81. update = () => {
  82. const pluginId = this.model.id;
  83. this.preUpdateHook()
  84. .then(() => {
  85. const updateCmd = extend(
  86. {
  87. enabled: this.model.enabled,
  88. pinned: this.model.pinned,
  89. jsonData: this.model.jsonData,
  90. secureJsonData: this.model.secureJsonData,
  91. },
  92. {}
  93. );
  94. return getBackendSrv().post(`/api/plugins/${pluginId}/settings`, updateCmd);
  95. })
  96. .then(this.postUpdateHook)
  97. .then(res => {
  98. window.location.href = window.location.href;
  99. });
  100. };
  101. setPreUpdateHook = (callback: () => any) => {
  102. this.preUpdateHook = callback;
  103. };
  104. setPostUpdateHook = (callback: () => any) => {
  105. this.postUpdateHook = callback;
  106. };
  107. // Stub to avoid unknown function in legacy code
  108. importDashboards = (): Promise<void> => {
  109. deprecationWarning('AppConfig', 'importDashboards()');
  110. return Promise.resolve();
  111. };
  112. enable = () => {
  113. this.model.enabled = true;
  114. this.model.pinned = true;
  115. this.update();
  116. };
  117. disable = () => {
  118. this.model.enabled = false;
  119. this.model.pinned = false;
  120. this.update();
  121. };
  122. }