ApiKeysPage.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. export interface Props {
  17. navModel: NavModel;
  18. apiKeys: ApiKey[];
  19. searchQuery: string;
  20. hasFetched: boolean;
  21. loadApiKeys: typeof loadApiKeys;
  22. deleteApiKey: typeof deleteApiKey;
  23. setSearchQuery: typeof setSearchQuery;
  24. addApiKey: typeof addApiKey;
  25. apiKeysCount: number;
  26. }
  27. export interface State {
  28. isAdding: boolean;
  29. newApiKey: NewApiKey;
  30. }
  31. enum ApiKeyStateProps {
  32. Name = 'name',
  33. Role = 'role',
  34. }
  35. const initialApiKeyState = {
  36. name: '',
  37. role: OrgRole.Viewer,
  38. };
  39. export class ApiKeysPage extends PureComponent<Props, any> {
  40. constructor(props) {
  41. super(props);
  42. this.state = { isAdding: false, newApiKey: initialApiKeyState };
  43. }
  44. componentDidMount() {
  45. this.fetchApiKeys();
  46. }
  47. async fetchApiKeys() {
  48. await this.props.loadApiKeys();
  49. }
  50. onDeleteApiKey(key: ApiKey) {
  51. this.props.deleteApiKey(key.id);
  52. }
  53. onSearchQueryChange = evt => {
  54. this.props.setSearchQuery(evt.target.value);
  55. };
  56. onToggleAdding = () => {
  57. this.setState({ isAdding: !this.state.isAdding });
  58. };
  59. onAddApiKey = async evt => {
  60. evt.preventDefault();
  61. const openModal = (apiKey: string) => {
  62. const rootPath = window.location.origin + config.appSubUrl;
  63. const modalTemplate = ReactDOMServer.renderToString(<ApiKeysAddedModal apiKey={apiKey} rootPath={rootPath} />);
  64. appEvents.emit('show-modal', {
  65. templateHtml: modalTemplate,
  66. });
  67. };
  68. this.props.addApiKey(this.state.newApiKey, openModal);
  69. this.setState((prevState: State) => {
  70. return {
  71. ...prevState,
  72. newApiKey: initialApiKeyState,
  73. isAdding: false,
  74. };
  75. });
  76. };
  77. onApiKeyStateUpdate = (evt, prop: string) => {
  78. const value = evt.currentTarget.value;
  79. this.setState((prevState: State) => {
  80. const newApiKey = {
  81. ...prevState.newApiKey,
  82. };
  83. newApiKey[prop] = value;
  84. return {
  85. ...prevState,
  86. newApiKey: newApiKey,
  87. };
  88. });
  89. };
  90. renderEmptyList() {
  91. const { isAdding } = this.state;
  92. return (
  93. <div className="page-container page-body">
  94. {!isAdding && (
  95. <EmptyListCTA
  96. model={{
  97. title: "You haven't added any API Keys yet.",
  98. buttonIcon: 'fa fa-plus',
  99. buttonLink: '#',
  100. onClick: this.onToggleAdding,
  101. buttonTitle: ' New API Key',
  102. proTip: 'Remember you can provide view-only API access to other applications.',
  103. proTipLink: '',
  104. proTipLinkTitle: '',
  105. proTipTarget: '_blank',
  106. }}
  107. />
  108. )}
  109. {this.renderAddApiKeyForm()}
  110. </div>
  111. );
  112. }
  113. renderAddApiKeyForm() {
  114. const { newApiKey, isAdding } = this.state;
  115. return (
  116. <SlideDown in={isAdding}>
  117. <div className="cta-form">
  118. <button className="cta-form__close btn btn-transparent" onClick={this.onToggleAdding}>
  119. <i className="fa fa-close" />
  120. </button>
  121. <h5>Add API Key</h5>
  122. <form className="gf-form-group" onSubmit={this.onAddApiKey}>
  123. <div className="gf-form-inline">
  124. <div className="gf-form max-width-21">
  125. <span className="gf-form-label">Key name</span>
  126. <input
  127. type="text"
  128. className="gf-form-input"
  129. value={newApiKey.name}
  130. placeholder="Name"
  131. onChange={evt => this.onApiKeyStateUpdate(evt, ApiKeyStateProps.Name)}
  132. />
  133. </div>
  134. <div className="gf-form">
  135. <span className="gf-form-label">Role</span>
  136. <span className="gf-form-select-wrapper">
  137. <select
  138. className="gf-form-input gf-size-auto"
  139. value={newApiKey.role}
  140. onChange={evt => this.onApiKeyStateUpdate(evt, ApiKeyStateProps.Role)}
  141. >
  142. {Object.keys(OrgRole).map(role => {
  143. return (
  144. <option key={role} label={role} value={role}>
  145. {role}
  146. </option>
  147. );
  148. })}
  149. </select>
  150. </span>
  151. </div>
  152. <div className="gf-form">
  153. <button className="btn gf-form-btn btn-primary">Add</button>
  154. </div>
  155. </div>
  156. </form>
  157. </div>
  158. </SlideDown>
  159. );
  160. }
  161. renderApiKeyList() {
  162. const { isAdding } = this.state;
  163. const { apiKeys, searchQuery } = this.props;
  164. return (
  165. <div className="page-container page-body">
  166. <div className="page-action-bar">
  167. <div className="gf-form gf-form--grow">
  168. <label className="gf-form--has-input-icon gf-form--grow">
  169. <input
  170. type="text"
  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. <i className="fa fa-plus" /> 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. </div>
  211. );
  212. }
  213. render() {
  214. const { hasFetched, navModel, apiKeysCount } = this.props;
  215. return (
  216. <Page navModel={navModel}>
  217. <Page.Contents isLoading={!hasFetched}>
  218. {hasFetched && (
  219. apiKeysCount > 0 ? (
  220. this.renderApiKeyList()
  221. ) : (
  222. this.renderEmptyList()
  223. )
  224. )}
  225. </Page.Contents>
  226. </Page>
  227. );
  228. }
  229. }
  230. function mapStateToProps(state) {
  231. return {
  232. navModel: getNavModel(state.navIndex, 'apikeys'),
  233. apiKeys: getApiKeys(state.apiKeys),
  234. searchQuery: state.apiKeys.searchQuery,
  235. apiKeysCount: getApiKeysCount(state.apiKeys),
  236. hasFetched: state.apiKeys.hasFetched,
  237. };
  238. }
  239. const mapDispatchToProps = {
  240. loadApiKeys,
  241. deleteApiKey,
  242. setSearchQuery,
  243. addApiKey,
  244. };
  245. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(ApiKeysPage));