2018-10-13 22:14:46 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
class APIKeyForm extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { keyLabel: '' };
|
|
|
|
|
|
|
|
this.addKey = this.addKey.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
addKey(event) {
|
|
|
|
event.preventDefault();
|
2018-10-16 00:22:56 +02:00
|
|
|
document.getElementById('addKeyForm').reset();
|
2019-05-14 11:25:14 +02:00
|
|
|
this.props.createApiKey(this.state.keyLabel)
|
|
|
|
.then(newToken => this.setState({ newToken }));
|
2018-11-05 22:28:52 +01:00
|
|
|
this.state.keyLabel = '';
|
2018-10-13 22:14:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:22:56 +02:00
|
|
|
removeKey(keyId) {
|
|
|
|
this.props.removeApiKey(keyId);
|
2018-10-13 22:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-05-14 11:25:14 +02:00
|
|
|
const { newToken } = this.state;
|
|
|
|
|
|
|
|
const content = newToken ?
|
|
|
|
(
|
|
|
|
<div>
|
|
|
|
<p>Here is your new key. Copy it somewhere, you won't be able to see it later !</p>
|
|
|
|
<input type="text" readOnly value={newToken} />
|
|
|
|
<button>Copy to clipboard</button>
|
|
|
|
</div>) :
|
|
|
|
(<form id="addKeyForm" className="form" onSubmit={this.addKey}>
|
2018-10-13 22:14:46 +02:00
|
|
|
<h2 className="form__label">Key label</h2>
|
2019-05-14 11:25:14 +02:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="form__input"
|
|
|
|
placeholder="A name you will be able to recognize"
|
|
|
|
id="keyLabel"
|
|
|
|
onChange={(event) => { this.setState({ keyLabel: event.target.value }); }}
|
|
|
|
/><br />
|
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
value="Create new Key"
|
|
|
|
disabled={this.state.keyLabel === ''}
|
|
|
|
/>
|
2018-10-13 22:14:46 +02:00
|
|
|
</form>
|
2019-05-14 11:25:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{content}
|
2018-10-13 22:14:46 +02:00
|
|
|
<table className="form__table">
|
2018-11-05 22:28:52 +01:00
|
|
|
<tbody id="form__table_new_key"></tbody>
|
2018-10-13 22:14:46 +02:00
|
|
|
<tbody>
|
2018-10-16 00:22:56 +02:00
|
|
|
{this.props.apiKeys && this.props.apiKeys.map(v => (
|
2018-10-13 22:14:46 +02:00
|
|
|
<tr key={v.id}>
|
2018-10-16 00:22:56 +02:00
|
|
|
<td><b>{v.label}</b><br />Created on: {v.createdAt}</td>
|
|
|
|
<td>Last used on:<br /> {v.lastUsedAt}</td>
|
2018-11-05 22:28:52 +01:00
|
|
|
<td><button className="form__table-button-remove" onClick={() => this.removeKey(v.id)}>Delete</button></td>
|
2018-10-13 22:14:46 +02:00
|
|
|
</tr>))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APIKeyForm.propTypes = {
|
2019-05-14 11:25:14 +02:00
|
|
|
createApiKey: PropTypes.func.isRequired,
|
2018-10-16 00:22:56 +02:00
|
|
|
removeApiKey: PropTypes.func.isRequired,
|
|
|
|
apiKeys: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
id: PropTypes.object.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
createdAt: PropTypes.object.isRequired,
|
|
|
|
lastUsedAt: PropTypes.object.isRequired,
|
|
|
|
})).isRequired
|
2018-10-13 22:14:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default APIKeyForm;
|