SharedPreferences.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { PureComponent } from 'react';
  2. import { Label } from 'app/core/components/Label/Label';
  3. import SimplePicker from 'app/core/components/Picker/SimplePicker';
  4. import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
  5. import { DashboardSearchHit } from 'app/types';
  6. export interface Props {
  7. resourceUri: string;
  8. }
  9. export interface State {
  10. homeDashboardId: number;
  11. theme: string;
  12. timezone: string;
  13. dashboards: DashboardSearchHit[];
  14. }
  15. const themes = [{ value: '', text: 'Default' }, { value: 'dark', text: 'Dark' }, { value: 'light', text: 'Light' }];
  16. const timezones = [
  17. { value: '', text: 'Default' },
  18. { value: 'browser', text: 'Local browser time' },
  19. { value: 'utc', text: 'UTC' },
  20. ];
  21. export class SharedPreferences extends PureComponent<Props, State> {
  22. backendSrv: BackendSrv = getBackendSrv();
  23. constructor(props) {
  24. super(props);
  25. console.log('props', props);
  26. this.state = {
  27. homeDashboardId: 0,
  28. theme: '',
  29. timezone: '',
  30. dashboards: [],
  31. };
  32. }
  33. async componentDidMount() {
  34. const prefs = await this.backendSrv.get(`/api/${this.props.resourceUri}/preferences`);
  35. const dashboards = await this.backendSrv.search({ starred: true });
  36. this.setState({
  37. homeDashboardId: prefs.homeDashboardId,
  38. theme: prefs.theme,
  39. timezone: prefs.timezone,
  40. dashboards: [{ id: 0, title: 'Default', tags: [], type: '', uid: '', uri: '', url: '' }, ...dashboards],
  41. });
  42. }
  43. onSubmitForm = async event => {
  44. event.preventDefault();
  45. const { homeDashboardId, theme, timezone } = this.state;
  46. await this.backendSrv.put(`/api/${this.props.resourceUri}/preferences`, {
  47. homeDashboardId,
  48. theme,
  49. timezone,
  50. });
  51. };
  52. onThemeChanged = (theme: string) => {
  53. this.setState({ theme });
  54. };
  55. onTimeZoneChanged = (timezone: string) => {
  56. this.setState({ timezone });
  57. };
  58. onHomeDashboardChanged = (dashboardId: number) => {
  59. this.setState({ homeDashboardId: dashboardId });
  60. };
  61. render() {
  62. const { theme, timezone, homeDashboardId, dashboards } = this.state;
  63. return (
  64. <form className="section gf-form-group" onSubmit={this.onSubmitForm}>
  65. <h3 className="page-heading">Preferences</h3>
  66. <div className="gf-form">
  67. <span className="gf-form-label width-11">UI Theme</span>
  68. <SimplePicker
  69. value={themes.find(item => item.value === theme)}
  70. options={themes}
  71. getOptionValue={i => i.value}
  72. getOptionLabel={i => i.text}
  73. onSelected={theme => this.onThemeChanged(theme.value)}
  74. width={20}
  75. />
  76. </div>
  77. <div className="gf-form">
  78. <Label
  79. width={11}
  80. tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
  81. >
  82. Home Dashboard
  83. </Label>
  84. <SimplePicker
  85. value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
  86. getOptionValue={i => i.id}
  87. getOptionLabel={i => i.title}
  88. onSelected={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
  89. options={dashboards}
  90. placeholder="Chose default dashboard"
  91. width={20}
  92. />
  93. </div>
  94. <div className="gf-form">
  95. <label className="gf-form-label width-11">Timezone</label>
  96. <SimplePicker
  97. value={timezones.find(item => item.value === timezone)}
  98. getOptionValue={i => i.value}
  99. getOptionLabel={i => i.text}
  100. onSelected={timezone => this.onTimeZoneChanged(timezone.value)}
  101. options={timezones}
  102. width={20}
  103. />
  104. </div>
  105. <div className="gf-form-button-row">
  106. <button type="submit" className="btn btn-success">
  107. Save
  108. </button>
  109. </div>
  110. </form>
  111. );
  112. }
  113. }
  114. export default SharedPreferences;