ApiKeysPage.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import React, { PureComponent } from 'react';
  2. import ReactDOMServer from 'react-dom/server';
  3. import { connect } from 'react-redux';
  4. import { hot } from 'react-hot-loader';
  5. import { NavModel, ApiKey, NewApiKey, OrgRole } from 'app/types';
  6. import { getNavModel } from 'app/core/selectors/navModel';
  7. import { getApiKeys, getApiKeysCount } from './state/selectors';
  8. import { loadApiKeys, deleteApiKey, setSearchQuery, addApiKey } from './state/actions';
  9. import Page from 'app/core/components/Page/Page';
  10. import SlideDown from 'app/core/components/Animations/SlideDown';
  11. import ApiKeysAddedModal from './ApiKeysAddedModal';
  12. import config from 'app/core/config';
  13. import appEvents from 'app/core/app_events';
  14. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  15. import { DeleteButton } from '@grafana/ui';
  16. import { RegExpSafeInput } from 'app/core/components/RegExpSafeInput/RegExpSafeInput';
  17. export interface Props {
  18. navModel: NavModel;
  19. apiKeys: ApiKey[];
  20. searchQuery: string;
  21. hasFetched: boolean;
  22. loadApiKeys: typeof loadApiKeys;
  23. deleteApiKey: typeof deleteApiKey;
  24. setSearchQuery: typeof setSearchQuery;
  25. addApiKey: typeof addApiKey;
  26. apiKeysCount: number;
  27. }
  28. export interface State {
  29. isAdding: boolean;
  30. newApiKey: NewApiKey;
  31. }
  32. enum ApiKeyStateProps {
  33. Name = 'name',
  34. Role = 'role',
  35. }
  36. const initialApiKeyState = {
  37. name: '',
  38. role: OrgRole.Viewer,
  39. };
  40. export class ApiKeysPage extends PureComponent<Props, any> {
  41. constructor(props) {
  42. super(props);
  43. this.state = { isAdding: false, newApiKey: initialApiKeyState };
  44. }
  45. componentDidMount() {
  46. this.fetchApiKeys();
  47. }
  48. async fetchApiKeys() {
  49. await this.props.loadApiKeys();
  50. }
  51. onDeleteApiKey(key: ApiKey) {
  52. this.props.deleteApiKey(key.id);
  53. }
  54. onSearchQueryChange = (value: string) => {
  55. this.props.setSearchQuery(value);
  56. };
  57. onToggleAdding = () => {
  58. this.setState({ isAdding: !this.state.isAdding });
  59. };
  60. onAddApiKey = async evt => {
  61. evt.preventDefault();
  62. const openModal = (apiKey: string) => {
  63. const rootPath = window.location.origin + config.appSubUrl;
  64. const modalTemplate = ReactDOMServer.renderToString(<ApiKeysAddedModal apiKey={apiKey} rootPath={rootPath} />);
  65. appEvents.emit('show-modal', {
  66. templateHtml: modalTemplate,
  67. });
  68. };
  69. this.props.addApiKey(this.state.newApiKey, openModal);
  70. this.setState((prevState: State) => {
  71. return {
  72. ...prevState,
  73. newApiKey: initialApiKeyState,
  74. isAdding: false,
  75. };
  76. });
  77. };
  78. onApiKeyStateUpdate = (evt, prop: string) => {
  79. const value = evt.currentTarget.value;
  80. this.setState((prevState: State) => {
  81. const newApiKey = {
  82. ...prevState.newApiKey,
  83. };
  84. newApiKey[prop] = value;
  85. return {
  86. ...prevState,
  87. newApiKey: newApiKey,
  88. };
  89. });
  90. };
  91. renderEmptyList() {
  92. const { isAdding } = this.state;
  93. return (
  94. <>
  95. {!isAdding && (
  96. <EmptyListCTA
  97. model={{
  98. title: "You haven't added any API Keys yet.",
  99. buttonIcon: 'fa fa-plus',
  100. buttonLink: '#',
  101. onClick: this.onToggleAdding,
  102. buttonTitle: ' New API Key',
  103. proTip: 'Remember you can provide view-only API access to other applications.',
  104. proTipLink: '',
  105. proTipLinkTitle: '',
  106. proTipTarget: '_blank',
  107. }}
  108. />
  109. )}
  110. {this.renderAddApiKeyForm()}
  111. </>
  112. );
  113. }
  114. renderAddApiKeyForm() {
  115. const { newApiKey, isAdding } = this.state;
  116. return (
  117. <SlideDown in={isAdding}>
  118. <div className="cta-form">
  119. <button className="cta-form__close btn btn-transparent" onClick={this.onToggleAdding}>
  120. <i className="fa fa-close" />
  121. </button>
  122. <h5>Add API Key</h5>
  123. <form className="gf-form-group" onSubmit={this.onAddApiKey}>
  124. <div className="gf-form-inline">
  125. <div className="gf-form max-width-21">
  126. <span className="gf-form-label">Key name</span>
  127. <input
  128. type="text"
  129. className="gf-form-input"
  130. value={newApiKey.name}
  131. placeholder="Name"
  132. onChange={evt => this.onApiKeyStateUpdate(evt, ApiKeyStateProps.Name)}
  133. />
  134. </div>
  135. <div className="gf-form">
  136. <span className="gf-form-label">Role</span>
  137. <span className="gf-form-select-wrapper">
  138. <select
  139. className="gf-form-input gf-size-auto"
  140. value={newApiKey.role}
  141. onChange={evt => this.onApiKeyStateUpdate(evt, ApiKeyStateProps.Role)}
  142. >
  143. {Object.keys(OrgRole).map(role => {
  144. return (
  145. <option key={role} label={role} value={role}>
  146. {role}
  147. </option>
  148. );
  149. })}
  150. </select>
  151. </span>
  152. </div>
  153. <div className="gf-form">
  154. <button className="btn gf-form-btn btn-primary">Add</button>
  155. </div>
  156. </div>
  157. </form>
  158. </div>
  159. </SlideDown>
  160. );
  161. }
  162. renderApiKeyList() {
  163. const { isAdding } = this.state;
  164. const { apiKeys, searchQuery } = this.props;
  165. return (
  166. <>
  167. <div className="page-action-bar">
  168. <div className="gf-form gf-form--grow">
  169. <label className="gf-form--has-input-icon gf-form--grow">
  170. <RegExpSafeInput
  171. className="gf-form-input"
  172. placeholder="Search keys"
  173. value={searchQuery}
  174. onChange={this.onSearchQueryChange}
  175. />
  176. <i className="gf-form-input-icon fa fa-search" />
  177. </label>
  178. </div>
  179. <div className="page-action-bar__spacer" />
  180. <button className="btn btn-primary pull-right" onClick={this.onToggleAdding} disabled={isAdding}>
  181. Add API Key
  182. </button>
  183. </div>
  184. {this.renderAddApiKeyForm()}
  185. <h3 className="page-heading">Existing Keys</h3>
  186. <table className="filter-table">
  187. <thead>
  188. <tr>
  189. <th>Name</th>
  190. <th>Role</th>
  191. <th style={{ width: '34px' }} />
  192. </tr>
  193. </thead>
  194. {apiKeys.length > 0 ? (
  195. <tbody>
  196. {apiKeys.map(key => {
  197. return (
  198. <tr key={key.id}>
  199. <td>{key.name}</td>
  200. <td>{key.role}</td>
  201. <td>
  202. <DeleteButton onConfirm={() => this.onDeleteApiKey(key)} />
  203. </td>
  204. </tr>
  205. );
  206. })}
  207. </tbody>
  208. ) : null}
  209. </table>
  210. </>
  211. );
  212. }
  213. render() {
  214. const { hasFetched, navModel, apiKeysCount } = this.props;
  215. return (
  216. <Page navModel={navModel}>
  217. <Page.Contents isLoading={!hasFetched}>
  218. {hasFetched && (apiKeysCount > 0 ? this.renderApiKeyList() : this.renderEmptyList())}
  219. </Page.Contents>
  220. </Page>
  221. );
  222. }
  223. }
  224. function mapStateToProps(state) {
  225. return {
  226. navModel: getNavModel(state.navIndex, 'apikeys'),
  227. apiKeys: getApiKeys(state.apiKeys),
  228. searchQuery: state.apiKeys.searchQuery,
  229. apiKeysCount: getApiKeysCount(state.apiKeys),
  230. hasFetched: state.apiKeys.hasFetched,
  231. };
  232. }
  233. const mapDispatchToProps = {
  234. loadApiKeys,
  235. deleteApiKey,
  236. setSearchQuery,
  237. addApiKey,
  238. };
  239. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(ApiKeysPage));